如何在不使用表单或提交按钮的情况下获取php变量中的值

时间:2014-02-11 13:56:24

标签: javascript php

我使用的代码如下:

 <input type="text" 
         id="dp1"  
         class="startvalue" 
         name="star" 
         placeholder="start date" 
         onChange="getval();">

我想在php变量中获取此输入字段的值,而不使用提交按钮或表单标记。

2 个答案:

答案 0 :(得分:1)

使用onkeyup事件

并在其中,检查输入值...

http://api.jquery.com/keyup/

这会对你有帮助.....

另外,请检查以下答案:

get text box value when i type the value by every press

在js中收到参数后,

使用

$.post("script.php", { "star": value_param_recieved_in_js });

在js文件中。

在script.php中,

的形式访问它
$my_var = $_POST['star'];

答案 1 :(得分:0)

将来请更好地描述您的问题!我在猜你的意思。

这可以通过Jquery和Ajax解决。 Step by Stept:

使用Jquery,您可以捕获用户输入,也许使用Jquery blur()

$('#yourinput').on('blur', function() {
insert other code here
)};

文档位于:http://api.jquery.com/blur/另请参阅:http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onblur

除了捕获事件之外,您还可以通过以下方式捕获变量中的输入:

var input = $(this).attr('input');

下一步是将这些信息发送到php脚本。此时添加某种用户验证是明智的,因为否则ajax脚本可能不安全。 首先我们构建请求,然后处理ajax调用。最后一步是创建被调用的php文件并提供我们期望的反馈(如果反馈是“假”,我将测试。(文本中)。

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
}

最后让我们看一下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";
}