最简单的表单提交没有重定向?

最简单的表单提交没有重定向?

问题描述:

So I have been killing myself on this project, maybe because I tried doing it the hard way :) I have been trying to convert the following dynamic PHP form to be submitting the same info, but via AJAX which I have no experience in, but I wanted to submit without leaving the page.

While researching "Things Every PHP Coder Should Know" (http://terrychay.com/article/php-coders.shtml) while preparing to phone interview a potential programmer, I was introduced a different method of accomplishing the same task using remote scripting and an iframe. Realizing there could be dozens of ways of doing this, what would be the "best" way to take the following form, and submit it without leaving the page? Since I know PHP best, is there one that would be better suited to me or should I keep going with AJAX?

<form action="functions.php?do=save" method="POST" id="saveSettings">
<table width=100%>
<tr><th>Setting</th><th>Value</th><th>Active</th></tr>
<?
include 'db.php';

$i=0;
    foreach($db->query('SELECT * from settings') as $row) {
    echo ($i % 2)?'<tr class="odd">':'<tr class="even">';
        print_r("<td width=200>" . $row[1] . "</td>
        <td><input type=\"textbox\" name=\"" . $row[1] . "[]\" value=\"" . $row[2] . "\"></td> 
        <td><input type=\"hidden\" name=\"" . $row[1] . "[1]\" value=\"INACTIVE\"> "); //set value so it never passes NULL
            if ($row[3] == "ACTIVE"){
            print_r("<input type=\"checkbox\" name=\"" . $row[1] . "[1]\" value=\"ACTIVE\" checked=\"true\"></td></tr>");
            }
            else{
            print_r("<input type=\"checkbox\" name=\"" . $row[1] . "[1]\" value=\"ACTIVE\"></td></tr>");

            }
    $i++;
    }
?>
</tbody>
</table>
<input type="submit" value="Save Settings">
</form>

所以我一直在这个项目上自杀,也许是因为我试过这么做的方式:)我一直在 尝试转换以下动态PHP表单以提交相同的信息,但通过我没有经验的AJAX,但我想提交而不离开页面。 p>

在研究“每个PHP编码器应该知道的事情”时( http ://terrychay.com/article/php-coders.shtml )在准备电话采访潜在的程序员时,我介绍了一种使用远程脚本和iframe完成相同任务的不同方法。 意识到可能有很多方法可以做到这一点,采用以下形式的“最佳”方式是什么,并在不离开页面的情况下提交它? 既然我最了解PHP,是否有一个更适合我或者我应该继续使用AJAX? p>

 &lt; form action =“functions.php?do = 保存“method =”POST“id =”saveSettings“&gt; 
&lt; table width = 100%&gt; 
&lt; tr&gt;&lt; th&gt;设置&lt; / th&gt;&lt; th&gt;值&lt; / th&gt;&lt; th&gt;  ;有效&lt; / th&gt;&lt; / tr&gt; 
&lt;?
include'db.php'; 
 
 $ i = 0; 
 foreach($ db-&gt; query('SELECT * from settings') 如$ row){
 echo($ i%2)?'&lt; tr class =“odd”&gt;':'&lt; tr class =“even”&gt;'; 
 print_r(“&lt; td width  = 200&gt;“。$ row [1]。”&lt; / td&gt; 
&lt; td&gt;&lt; input type = \“textbox \”name = \“”。$ row [1]。“[] \”  value = \“”。$ row [2]。“\”&gt;&lt; / td&gt; 
&lt; td&gt;&lt; input type = \“hidden \”name = \“”。$ row [1]。  “[1] \”value = \“INACTIVE \”&gt;“);  //设置值,所以它永远不会传递NULL 
 if($ row [3] ==“ACTIVE”){
 print_r(“&lt; input type = \”checkbox \“name = \”“。$ row [1  ]。“[1] \”value = \“ACTIVE \”checked = \“true \”&gt;&lt; / td&gt;&lt; / tr&gt;“); 
} 
其他{
 print_r(”&lt;  ; input type = \“checkbox \”name = \“”。$ row [1]。“[1] \”value = \“ACTIVE \”&gt;&lt; / td&gt;&lt; / tr&gt;“); \  n 
} 
 $ i ++; 
} 
?&gt; 
&lt; / tbody&gt; 
&lt; / table&gt; 
&lt; input type =“submit”value =“保存设置”&gt; 
&lt; / 表格&gt; 
  code>  pre> 
  div>

It's quite simple, really, all you need to do is the following jQuery:

$('form').on('submit', function(){
    var $this = $(this);
    $.ajax({
        type: $this.prop('method'),
        url: $this.prop('action'),
        data: $this.serialize(),
        success: function(data){
          //do something with the return of the php script
        }
    });
    return false; //prevent the form from causing the page to refresh
});

$.serialize() is used to turn the form into a text string that is URL encoded. This makes it simple to get the values in the php script as nothing should change given the structure of your form above.