Javascript 如何在不使用表单或提交按钮的情况下获取php变量中的值
我使用的代码如下:Javascript 如何在不使用表单或提交按钮的情况下获取php变量中的值,javascript,php,Javascript,Php,我使用的代码如下: <input type="text" id="dp1" class="startvalue" name="star" placeholder="start date" onChange="getval();"> 我希望在php变量中获取此输入字段的值,而不使用submit按钮或表单标记。使用onkeyup事件 在它里面,检查输入值 这将帮助你 此外,请检
<input type="text"
id="dp1"
class="startvalue"
name="star"
placeholder="start date"
onChange="getval();">
我希望在php变量中获取此输入字段的值,而不使用submit按钮或表单标记。使用onkeyup事件
在它里面,检查输入值
这将帮助你
此外,请检查以下答案:
在js中接收到参数后
使用
在js文件本身中
在script.php中
访问它作为
$my_var = $_POST['star'];
今后请更好地描述您的问题!我在猜你的意思 这可以通过Jquery和Ajax解决。逐步地: 使用Jquery,您可以在中捕获用户输入,也许可以使用Jquery blur() 文档位于:另请检查: 除了捕获事件外,还可以通过以下方式捕获变量中的输入:
var input = $(this).attr('input');
下一步是将这些信息发送到php脚本。此时添加某种用户验证是明智的,因为否则ajax脚本可能不安全。
首先构建请求,然后处理ajax调用。最后一步是创建调用的php文件,并给出我们期望的反馈(我将测试反馈是否为“false”(文本)
最后,让我们看看php脚本input_script.php:
if(isset($_GET['action']) and $_GET['action'] == 'inputajax_tester' and isset($_GET['input']) and $_GET['input'] != '') {
// do whatever you have to do
// on success do
echo "success";
// else
echo "fail";
}
事实上,我只看到一个输入字段错误,我会写两个,但只有一个PLS给出解决方案,谢谢。我得到了这些输入值的值,但是我希望这个值在PHP变量中不使用Windows .Load。您必须使用JavaScript的Ajax调用,并将数据发布到PHP文件中,并在PHP文件中检查Par Par。请你给我一个例子,链接上面的答案,并附上一个样本。请接受它作为答案,如果这对你有效的话……)
var input = $(this).attr('input');
var more_data = 'inputajax_tester'; // just example for giving the php script mroe arguments
var request = 'input=' + input + "&action=" + more_data;
$.ajax({
type: "GET",
url: "input_script.php", // this script usually lies at the relative root of the file in your webapp that you hvae opened now. now in the sub-directory where your javascript might live.
data: request,
success: function(msg) {
if(msg == 'false') {
alert('error');
} else {
//success
}
if(isset($_GET['action']) and $_GET['action'] == 'inputajax_tester' and isset($_GET['input']) and $_GET['input'] != '') {
// do whatever you have to do
// on success do
echo "success";
// else
echo "fail";
}