AJAX和PHP代码无法正常工作

时间:2018-03-21 18:32:11

标签: php ajax forms

我通过AJAX发送表单数据但由于某种原因我的PHP脚本没有运行。我的PHP脚本中使用的测试回应没有显示。 window.alert("成功")确实显示但是HIDE和SHOW form1和form2也不起作用。

以下是代码:

$('#mainform').on('submit', function(event) {
      //test for empty fields
      //test for Bots
      //insert data into DB
      //pass t_code on to next form
      //create a page number for tabs

      event.preventDefault(); //stops form on submit
      var a = document.forms["mainform"]["hidden"].value;
      if (a === ""){
          var formData = {};
          $.each($("#mainform").serializeArray(), function (i, field) {
            formData[field.name] = field.value;
          });
          $.ajax({
          url: 'insert_tut_description.php',
          data: formData,
          method:'POST',
          success: function(response) { 
              window.alert("success");
              pnum = 1;
              t_code = form.elements["t_code"].value;
              $("#form1").hide();
              $("#form2").show();
              document.getElementById("pnum").innerHTML = pnum;
           }
    });
    };
});    


<?php

echo "php running";

require 'config/config.php';

$t_title    = $conn->real_escape_string($_POST['t_title']);
$t_code     = $conn->real_escape_string($_POST['t_code']);
$t_image    = $conn->real_escape_string($_POST['t_image']);
$hidden     = $conn->real_escape_string($_POST['hidden']);
$t_desc     = $conn->real_escape_string($_POST['t_desc']);
$t_url      = "something.php";

echo $t_title;
echo t_url;

if(empty($hidden)){
    echo "hidden is empty";
    $query = "INSERT into tutorial_list (title, description, t_code, t_url, image,) VALUES('" . $t_title . "','" . $t_desc . "','" . $t_code . "','" . $t_url . "','" . $t_image . "')";

    $success = $conn->query($query);

    if (!$success) {
        die("Couldn't enter data: ".$conn->error);
    } else {
        $conn->close();
    }
}

?>

0 个答案:

没有答案