简体   繁体   English

通过 AJAX 将表单数据传递给 mySQL

[英]passing form data to mySQL through AJAX

I am using JavaScript and AJAX to send form data to a php processing file to then populate an SQL database without refreshing the initial form page. I am using JavaScript and AJAX to send form data to a php processing file to then populate an SQL database without refreshing the initial form page.

The php/SQL connection is working, but the form data is not being sent correctly. php/SQL 连接正常,但未正确发送表单数据。

There are two fields: a radio group called "choice", and a text field called "comments".有两个字段:一个称为“选择”的广播组和一个称为“评论”的文本字段。 When the DB is populated, the choice field is always "yes" (the first of the 3 radio buttons), and the comments field is always blank.填充数据库时,选择字段始终为“是”(3 个单选按钮中的第一个),注释字段始终为空白。 I think the issue must be the way that the datastring is being constructed, but hours of tinkering have brought no success.我认为问题一定是构建数据字符串的方式,但是数小时的修补并没有带来成功。

Here is the html form这是 html 表格

<form id="paPoll">
  <label><input type="radio" name="RadioGroup1" value="yes" id="choice1">Yes</label>
  <label><input type="radio" name="RadioGroup1" value="no" id="choice2">No</label>
  <label><input type="radio" name="RadioGroup1" value="dont_know" id="choice3">I Don't Know</label>
  <label for="comments">Comments?</label><input name="comments" type="text" id="comments" size="25" maxlength="60">   
  <input type="button" name="form_process" id="form_process" value="submit" onClick="$.fn.removeMbMenu($.mbMenu.options.actualOpenedMenu,true);" />
</form>

here is the Javascript/AJAX function这是 Javascript/AJAX function

<script type="text/javascript">
    $(function() {
      $("#form_process").click(function() {
        var choice = $("#choice").val();
        var comments = $("#comments").val();
        var dataString = 'choice='+ choice + '&comments=' + comments;

        $.ajax({
          type: "POST",
          url: "**ABSOLUTE URL TO PROCESSOR**",
          data: dataString
        });
      });
    });
</script>

here is the php processor这是 php 处理器

<?php
$choice = $_POST ['choice'];
$comments = $_POST ['comments'];
//perform insert
    $query = mysql_query("INSERT INTO paPoll 
    (choice, comments)      
    VALUES  
    ('$choice', '$comments')");         
        if (!query) {
           die("Database query failed: " . mysql_error());
        }
?>

Thoughts or suggestions?想法或建议?

I found a very similar question here, but the thread is 3 years old.我在这里发现了一个非常相似的问题,但该线程已有 3 年历史。 A user suggested this一位用户建议了这个

//Instead of using
data: dataString
//use
data : {param: value, param2: value2}

but I'm not sure how, or in what format he means to get the param values.但我不确定他如何或以什么格式获取参数值。

Thank you谢谢
-syllable -音节

Were it me i would just make this easier on myself by using the Form plugin .如果是我,我会通过使用表单插件让自己更轻松。 Its already got an ajaxSubmit set of functions to make ajaxify your form with little effort.它已经有了一组ajaxSubmit函数,可以毫不费力地对表单进行 ajaxify。 IT also has the handy formSerialize function which will serialize a form for ajax submission or to append to a query string for a link.它还有方便的formSerialize function 它将为 ajax 提交或 append 的表单序列化为链接的查询字符串。 :-) :-)

That said an easier way without the plugin and utilizing your existing code:这就是说没有插件并利用现有代码的更简单方法:

$("#form_process").click(function() {

  $.ajax({
    type: "POST",
    url: "**ABSOLUTE URL TO PROCESSOR**",
    data: {
      'choice': $("input[@name=RadioGroup1]:checked").val(), 
      'comments':  $("#comments").val()
    }
  });

});

Either way though, you also need to change all your radio inputs to have unique ID's (or no ID at all)... you cant use the same one as its required that all id attributes have unique values within the document.不过,无论哪种方式,您还需要将所有无线电输入更改为具有唯一 ID(或根本没有 ID)......您不能使用与要求所有 id 属性在文档中具有唯一值相同的那个。

several week ago i have the same problem, and i just try using the old way like this几周前我有同样的问题,我只是尝试使用这样的旧方法

<script type="text/javascript">
    $(function() {
      $("#form_process").click(function() {
        //$("#choice").val(); //you cannot use the same id for more than 1 tag
        var choice = 0; 
             if(document.getElementById("choice1").checked) choice='Yes';
        else if(document.getElementById("choice2").checked) choice='No';
        else if(document.getElementById("choice3").checked) choice='Dont Know';

        var comments = document.getElementById('comments').value; //$("#comments").val();
        var dataString = 'choice='+ choice + '&comments=' + comments;

        $.ajax({
          type: "POST",
          url: "**ABSOLUTE URL TO PROCESSOR**",
          data: dataString
        });
     });
   });
</script>

really i don't know, i think this is not the best way but this work for me:)真的我不知道,我认为这不是最好的方法,但这对我有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM