简体   繁体   English

我想在提交之前使用javascript从表单中获取价值

[英]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.

相关问题 我想提交一份表格但在提交之前我想从模态表格中取一个值 - i want to submit a form but before submit i want to take a value from modal form 提交前如何从表格中获取价值 - how to get value from a form before submit 如何使用JavaScript在表单提交中获取具有相同名称的字段的值 - How to get value of fields with same name on form submit using javascript 如何在不使用表单的情况下从JavaScript向服务器提交获取请求 - How do I submit get request from JavaScript to server without using a form 从javascript提交表单后,如何通过提交按钮保持php $ _GET值? - How to keep php $_GET value from a submit button after form submit from javascript? 我已经在javascript中得到ajax响应但现在我想提交表单并希望ajax respone也在我的POST数组中 - i have get the ajax responce in javascript but now i want submit the form and want that ajax respone also in my POST array Ajax 表单提交我想通过 id 更改表单元素的值,以便脚本从 php 中的返回值进行下一次调用 - Ajax form submit I want to change the value of a form element by id for the next call by the script from return value in php 从链接提交表单并获取 $_Post 值 - Submit a form from a link and get $_Post value 如何使用javascript从数据库中获取价值,我正在使用jquery自动完成功能,并且我只想从数据库中搜索值 - how to get value from database with javascript, i am using jquery autocomplete, and i want to search values from database only 我如何获得表单以在提交之前删除链接的一部分? - how do i get a form to remove part of a link before submit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM