如何使用ajax将javascript变量解析为php变量 [英] How to parse javascript variable into php variable using ajax

查看:58
本文介绍了如何使用ajax将javascript变量解析为php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将javascript变量解析为php变量,但我无法做到。我不知道我的代码有什么问题。



这是我的代码



test.php



< input id =r1type =radiovalue =文章名称=无线电已检查> 
< input id =r2type =radiovalue = Book name =radios>
< input id =r3type =radiovalue = InBook name =radios>
< input id =r4type =radiovalue =Phd Thesisname =radios>
< input id =r5type =radiovalue =Master's Thesisname =radios>
< input id =r6type =radiovalue =其他名称=无线电>
< button type = button onclick =radio();>提交< / button>





< script> 
var art;

函数radio(){
if(document.getElementById('r1')。checked){
art = document.getElementById('r1')。value;
}
if(document.getElementById('r2')。checked){
art = document.getElementById('r2')。value;
}
if(document.getElementById('r3')。checked){
art = document.getElementById('r3')。value;
}
if(document.getElementById('r4')。checked){
art = document.getElementById('r4')。value;
}
if(document.getElementById('r5')。checked){
art = document.getElementById('r5')。value;
}
if(document.getElementById('r6')。checked){
art = document.getElementById('r6')。value;}

sendart (艺术);
}
函数sendart(t)
{
var xmlHttp;
try
{
// Firefox,Opera 8.0 +,Safari
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
// Internet Explorer
try
{
xmlHttp = new ActiveXObject(Msxml2.XMLHTTP );
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject(Microsoft.XMLHTTP);
}
catch(e)
{
alert(你的浏览器不支持AJAX!);
返回;
}
}
}

xmlHttp.open(GET,test.php?art =+ t,true);
xmlHttp.send();
}



< / script>
<?php
if(isset($ _ GET ['art'])){
$ s = $ _ GET ['art'];
echo $ s;
?>









我不知道是什么这里错了。我的php没有得到javascript变量艺术。提前谢谢

解决方案

_GET ['art'])){


S =

_GET [ '艺术'];
echo


I have to parse javascript variable into php variable but I couldn't to do tat. I dont know what's wrong in my code.

Here is my code

test.php

   <input id="r1" type="radio" value=Article name="radios" checked>
   <input id="r2" type="radio" value=Book name="radios">
<input id="r3" type="radio"value=InBook name="radios">
  <input id="r4" type="radio" value="Phd Thesis" name="radios">
  <input id="r5" type="radio" value="Master's Thesis" name="radios">
   <input id="r6" type="radio" value=Miscellaneous name="radios">
 <button type=button onclick="radio();">Submit</button>



<script>
var art;

function radio(){
   if (document.getElementById('r1').checked) {
  art = document.getElementById('r1').value;
}
if (document.getElementById('r2').checked) {
   art = document.getElementById('r2').value;
}
if (document.getElementById('r3').checked) {
  art = document.getElementById('r3').value;
}
if (document.getElementById('r4').checked) {
  art = document.getElementById('r4').value;
}
if (document.getElementById('r5').checked) {
  art = document.getElementById('r5').value;
}
if (document.getElementById('r6').checked) {
  art = document.getElementById('r6').value;}

     sendart(art);
  }
function sendart(t)
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return;
      }
    }
  }

  xmlHttp.open("GET","test.php?art=" + t, true);
  xmlHttp.send();
}


   
   </script>
   <?php
   if(isset($_GET['art'])){
   $s=$_GET['art'];
   echo $s;
?>





I dont know what's wrong here. Am not getting javascript variable art in my php.Thanks in advance

解决方案

_GET['art'])){


s=


_GET['art']; echo


这篇关于如何使用ajax将javascript变量解析为php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆