[英]i want to get value from the form using javascript before submit
I want to get value from input text form and send it to php before submit. 我想从输入文本表单中获取价值,并在提交之前将其发送给php。
Ex: PHP FILE: 例如: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: 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 />
So, with this example I want to get $do=$_POST['search'] before clicking to submit botton so I can select the result from mySQL to view it in the next page. 因此,在此示例中,我想在单击以提交botton之前获取$ do = $ _ POST ['search'],以便可以从mySQL中选择结果以在下一页中进行查看。
Thanks 谢谢
You can catch the form's submit event, cancel it, grab the form value you want, make your PHP post, and when that's done, go back and submit your form. 你能赶上表单的提交事件,取消它,抓住你想要的形式价值,使你的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();
};
<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 />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.