问题描述
我有一个脚本,该脚本使用ajaxRequest.open来调用php脚本.我想知道如何将变量发送到php文件.更具体地说,我有一个表单文本字段,希望能够发送到php文件.
I have a script which uses ajaxRequest.open to call a php script. I'm wondering how I could send a variable to the php file. More specifically, I've got a form text field I'd like to be able to send to the php file.
ajaxRequest工作正常,我只需要知道如何发送变量以及php如何读取它.
The ajaxRequest is working fine, I just need to know how I'd send a variable and how php could read it.
这是(非常简短的)脚本,正在调用我的php文件...
Here is the (very abbreviated) script which is calling my php file...
}
ajaxRequest.open("GET", "../../ajaxphp.php", true);
ajaxRequest.send(null);
}
推荐答案
首先,您需要获取要发送到* .php的变量witch的值.您可以通过以下方式做到这一点:
First you need to obtain the value of variable witch you are going to send to *.phpYou can do it by this ways:
var value = document.getElementById("someID").value;
或jQuery方式:
var value = $("#someID").val();
然后,您需要将变量放入ajax请求中:
Then, you need to put the variable to your ajax request:
ajaxRequest.open("GET", "../../ajaxphp.php?variable="+value, true);
//The last argument in this line (witch is set a "true") want to say, that is a asynchronous request
ajaxRequest.send(null);
//null want to say, that you not sending any parameter, really is automatically sent in the first line of code
然后,当您在代码*中拾取变量的值时.php,可以做下一个:
Then, when you pick up the value of the variable in the code *. php, can do the next:
<?php
$myVariable = $_GET["variable"];
echo $myVariable;
?>
或者像这样:
<?
$myVariable = $_REQUEST["variable"];
echo $$myVariable;
?>
这篇关于使用ajaxRequest.open将变量发送到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!