javascript - 使用 Ajax 和 PHP 将数据发送到 MySQL 但没有任何反应

标签 javascript php mysql ajax

我想发送一个简单的数据,如名称、代码、日期...等到我的数据库,我使用 JavaScript 获取数据,并使用 Ajax 将它发送到我的 PHP 文件,但没有任何反应,我也没有知道发生了什么,但 Ajax 什么都不做!! 所以,请任何人都可以查看代码,并说明发生了什么...... 谢谢

附言我的脚本在我的 HTML 文件的末尾,就在结束标记之前。

<script type="text/javascript">
debugger;
window.onload = function() {
    var platform = $("input:text[id=platform]").val();
    var source = $("input:text[id=source]").val();
    var code = $("input:text[id=code]").val();
    var thedate = $("input:text[id=date]").val();
    var payment_type = $("input:text[id=payment_type]").val();
    var change_for = $("input:text[id=change_for]").val();
    var order_comment = $("#order_comment").val();
    var decline_reason = $("input:text[id=decline_reason]").val();
    var order_reference_by_vendor = $("input:text[id=order_reference_by_vendor]").val();
    var deliveryprovider_title = $("input:text[id=deliveryprovider_title]").val();
    var social_reason = $("input:text[id=social_reason]").val();
    var government_identification = $("input:text[id=government_identification]").val();
    
      if(platform == ""){platform = "NULL"}
      if(source == ""){source = "NULL"}
      if(code == ""){code = "NULL"}
      if(thedate == ""){thedate = "NULL"}
      if(payment_type == ""){payment_type = "NULL"}
      if(change_for == ""){change_for = "NULL"}
      if(order_comment == ""){order_comment = "NULL"}
      if(decline_reason == ""){decline_reason = "NULL"}
      if(order_reference_by_vendor == ""){order_reference_by_vendor = "NULL"}
      if(deliveryprovider_title == ""){deliveryprovider_title = "NULL"}
      if(social_reason == ""){social_reason = "NULL"}
      if(government_identification == ""){government_identification = "NULL"}

    alert ("before ajax");

      $.ajax({
         type:"post",
         url:"savedata.php",
         data:{"platform": platform,
          "source": source,
          "code": code,
          "thedate": thedate,
          "payment_type": payment_type,
          "change_for": change_for,
          "order_comment": order_comment,
          "decline_reason": decline_reason,
          "order_reference_by_vendor": order_reference_by_vendor,
          "deliveryprovider_title": deliveryprovider_title,
          "social_reason": social_reason,
          "government_identification": government_identification},
         success: function(msg){
        alert("Success Insert data");
         }
      });

    alert ("after ajax");

      }
</script>

and here's the PHP code

<?php

 function db_connect()
{
	$servername = "localhost";
	$username = "root";
	$password = "";
	$dbname = "integrationdb";

    @mysql_connect($servername,$username,$password) or die ("error in host connection");
    @mysql_select_db($dbname) or die("error in db connection");
}

db_connect();

$platform = $_POST['platform'];
$source = $_POST['source'];
$code = $_POST['code'];
$thedate = $_POST['thedate'];
$payment_type = $_POST['payment_type'];
$change_for = $_POST['change_for'];
$order_comment = $_POST['order_comment'];
$decline_reason = $_POST['decline_reason'];
$order_reference_by_vendor = $_POST['order_reference_by_vendor'];
$deliveryprovider_title = $_POST['deliveryprovider_title'];
$social_reason = $_POST['social_reason'];
$government_identification = $_POST['government_identification'];

$qry = "INSERT INTO orderinformation VALUES ('$platform','$source','$code','$thedate','$payment_type','$change_for','$order_comment','$decline_reason','$order_reference_by_vendor','$deliveryprovider_title','$social_reason','$government_identification')";

$result=mysql_query($qry);
if(isset($result)) {
   echo "YES";
} else {
   echo "NO";
}
?>

  • 编辑 对不起,如果我问...如果我在 JSON 中使用 ( ' ) 而不是 ( ") 会有什么不同吗??!

最佳答案

尝试通过chrome toolbar查看网络它可以帮助您跟踪请求,读取其 header ,还可以检查可能包含错误消息的响应数据

关于javascript - 使用 Ajax 和 PHP 将数据发送到 MySQL 但没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45258631/

相关文章:

javascript - AngularJs ng-消息无法正常工作

javascript - 使用 DOMParser 解析的脚本不执行

javascript - 创建新元素时仅拼接一个元素

java - 程序拒绝从 MySQL 数据库获取字符串

Javascript(回到基础)数组?

php - 使用 gmail api 发送电子邮件时等号丢失

php - 使用 php Simple XML 获取节点的文本部分

PHP 循环巨大的文本文件非常慢,你能改进吗?

mysql - 克隆表 MYSQL 并添加唯一 ID?

php - 将自定义值从 parentOrderController 传递到 order-payment.tpl