PHP从AJAX发布数据并过滤它

时间:2017-11-12 18:23:59

标签: php jquery mysql ajax post

我有一个表单,我想与服务器通信抛出一个AJAX帖子。 我做的查询说这是查询错误但我没有看到问题。我尝试了不同的AJAX代码,但似乎帖子有效,因为它传递了if($ _ POST)。

$('#submit').click(function(e) {

    var url = "handler/dForm.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           contentType: 'application/json; charset=utf-8',
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               console.log(data); // show response from the php script.
           },
           error: function(xhr,textStatus,err)
            {
                console.log("readyState: " + xhr.readyState);
                console.log("responseText: "+ xhr.responseText);
                console.log("status: " + xhr.status);
                console.log("text status: " + textStatus);
                console.log("error: " + err);
           }                    
         });

    e.preventDefault(); // avoid to execute the actual submit of the form.
});

服务器部分

require_once 'connect.php';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$subject = isset($_POST['Subject']) ? $_POST['Subject'] : '';
$message = isset($_POST['Message']) ? $_POST['Message'] : '';
$email = isset($_POST['Email']) ? $_POST['Email'] : '';

if (isset($_POST)) {

 $email = $DBcon->real_escape_string($email);
 $message = $DBcon->real_escape_string($message);
 $subject = $DBcon->real_escape_string($subject);
 $name = $DBcon->real_escape_string($name);

 $query = "INSERT INTO customer(name,messaje,subject,email) VALUES('$name','$message','$subject','$email')";

  if ($DBcon->query($query)) {
   $msg = "<div class='alert alert-success'>
      <span class='glyphicon glyphicon-info-sign'></span> &nbsp; successfully registered !
     </div>";
  }else {
   $msg = "<div class='alert alert-danger'>
      <span class='glyphicon glyphicon-info-sign'></span> &nbsp; error while registering !
     </div>";
  }
 echo  $msg;


 $DBcon->close();
}else{
    echo "err";
}

HTML表单:

<form id="idForm">
                        <input type="text" class="col-md-6 col-xs-12 name" name='name' placeholder='Nume *'/>
                        <input type="text" class="col-md-6 col-xs-12 Email" name='Email' placeholder='Email *'/>
                        <input type="text" class="col-md-12 col-xs-12 Subject" name='Subject' placeholder='Subiect'/>
                        <textarea type="text" class="col-md-12 col-xs-12 Message" name='Message' placeholder='Mesaj *'></textarea>
                        <div class="cBtn col-xs-12">
                            <ul>
                                <li class="clear"><a href="javascript:void(0)"><i class="fa fa-times"></i>Sterge campurile</a></li>
                                <li class="send"><a href="javascript:void(0)" id="submit"><i class="fa fa-share"></i>Trimite</a>
                                </li>
                            </ul>
                        </div>
                    </form>

1 个答案:

答案 0 :(得分:2)

我看到的第一个问题是if (isset($_POST))它总是TRUE。你必须检查是否设置了特定字段ie。 if (isset($_POST['name']))

其次是那一行: $query = "INSERT INTO customer(name,messaje,subject,email) VALUES('$name','$message','$subject','$email')";

我认为您错误输入了消息列。你有messaje。

如果echo $DBcon->error;返回FALSE,则作为调试提示使用query()