0

After searching for internet and even here for 5 hours, i am still stuck at getting value of a local variable in a function and sending it to PHP.

I have tried different syntaxes but none of them seems to be working. This code below, takes an input field from PHP and assign some value to it (having problem send this value back to PHP)

$(document).ready(function() {  

//select all the a tag with name equal to modal
$('form[name=modal]').click(function(e) {
    //Cancel the link behavior
    e.preventDefault();

    //Dont go until value is not 7
    console.log('i am Now waiting for input');
    var s = $('#serial').val();
    console.log('searching for ' + s);
    while(s.length != 7) return;


    //When code value reaches 7

    var code = $('#serial').val();
    console.log('Value is reached ' + s);
  });

});

In PHP

echo "<script>document.write(code);</script>";

Uncaught ReferenceError: code is not defined

please help

4

2 回答 2

0

好的,我几乎测试了所有可能的解决方案,但都是徒劳的......所以我离开并尝试使用一些正常的方法而不是 javascript。感谢您的所有回答。

于 2013-05-02T10:53:30.710 回答
0

当 $('#serial') 是您的输入字段时,您应该在 javascript 中执行类似的操作。

$.ajax({'url':'your_php_file_url', 'type':'post', 'data':{'str':$('#serial').val()}}).done(function(data){
console.log(data.code);
});

并在 php

$output = Array('code' => 'not set');
if (isset($_POST['str']))
{
//do your search and assigne value to output
$output['code'] = 'some value';
}
echo json_encode($output);

简而言之 - 您通过 ajax 将您的搜索字符串发送到 php 并进行搜索。然后你将它作为 json 回显,然后 ajax 会将它作为一个对象读取,你可以使用你想要的那些值。

于 2013-05-01T20:51:41.463 回答