2012-01-16 46 views
1

我希望从输入文本表单获得价值并在提交之前将其发送到php。我想在提交之前使用javascript从表单中获取价值

例: PHP文件:

$do="FORM VALUE HERE";  //HERE IS $_POST['search']; 
$result = mysql_query("SELECT * FROM table WHERE name='$do'"); 
while($row = mysql_fetch_array($result)) 
    { 
    $code=$row['code_number']; 
    } 

HTML:

<script > 
HERE WHAT I WANT TO DO 
<script /> 

FORM:

<form action="view_results.php?code=<? echo $code ?>" method="post"> 
<input type="text" name="search" /><br /> 
<input type="submit" value="search" /> 
<form /> 

所以,用这个例子,我想获得$ DO = $ _ POST [ 'search'],然后单击提交botton,以便我可以从mySQL中选择结果以在下一页中查看它。

感谢

+1

没有必要对此处的JavaScript - 刚刚摆脱'$ _ POST [“搜索”]'值和下一显示在页面上。除非我错过了一些东西...... – DaveRandom 2012-01-16 18:25:49

+0

但是请使用'mysql_rea_escape_string'逃避你的数据! – ComFreek 2012-01-16 18:35:28

回答

0

你能赶上表单的提交事件,取消它,抓住你想要的形式价值,使你的PHP后,当完成,回去提交表单。

<form id="form1" action="view_results.php?code=<? echo $code ?>" method="post"> 

document.forms["form1"].onsubmit = function(e) { 
    e.preventDefault(); //cancel form submit 

    var searchValue = this.search.value; 

    var self = this; 
    //post searchValue to your PHP page using your favorite JS library, 
    //then in the call back, submit your form with self.submit(); 
}; 
+0

我没有在javascript中的expariance,你能告诉我$ do = ....在你的代码上,谢谢你的帮助 – rixlinux 2012-01-16 19:57:57

0
<form action="view_results.php?code=<? echo $code ?>" method="post" onsubmit="beforeSubmit(event)"> 
    <input type="text" name="search" /><br /> 
    <input type="submit" value="search" /> 
<form /> 

<script > 
    //HERE WHAT I WANT TO DO 

    function beforeSubmit(event){ 

     event.preventDefault(); 

     var form = event.currentTarget; 

     // form.search.value will give the text in search 

    } 
<script /> 
相关问题