2

I want to send a simple data, like name, code, date... etc to my Database, I got the data using JavaScript, and sent it to my PHP file using Ajax, but nothing happens and I don't know what's happening, but Ajax do nothing!! So, please can anyone review the code, and tell what's going on.. thanks

P.S. my script in the END of my HTML file, right before the ending tag.

<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";
}
?>
  • EDIT Excuse me if I'm asking... will it do difference if I use ( ' ) instead of ( " ) in JSON??!
Hossam
  • 62
  • 1
  • 1
  • 8
  • do you see this alert("Success Insert data"); being displayed ? – Abhishek Ranjan Jul 22 '17 at 19:59
  • are you getting any error in console? – RAUSHAN KUMAR Jul 22 '17 at 20:01
  • 1
    Please see https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php and stop use these mysql funtions ins php. – BenRoob Jul 22 '17 at 20:02
  • Let me introduce you to [.serialize](https://api.jquery.com/serialize/). He is your new friend. – Mikey Jul 22 '17 at 20:03
  • Let me give you a hint. The PHP `$_POST` variable is only populated by HTTP requests with a content type of `application/x-www-form-urlencoded`. You must read from the `php://input` stream to get the body of HTTP requests with any other content type. – Joshua Jones Jul 22 '17 at 20:13
  • thanks all, after checking the Network tab in DevTool via Chrome toolbar, I found that an Error in jQuery.. """ xhr.send( ( options.hasContent && options.data ) || null ); """ Err, Failed to load resource: the server responded with a status of 404 (Not Found), Note that I'm using jQ version 3.2.1 , So which version of jQ should I use? – Hossam Jul 22 '17 at 20:45
  • after some search, I found that I've to add dataType: 'JSONP', in the Ajax formula, but after that, the error changed to 404 Not Found, and my Request URL changed too, Request URL:http://localhost/Orders/savedata.php?callback=jQuery11110775105993469337_1500756629983 – Hossam Jul 22 '17 at 20:54
  • now I found another solution by adding contentType: "application/json; charset=utf-8", instead of JSONP,, I went back to the old problem!! – Hossam Jul 22 '17 at 21:03
  • I got the problem, I wasn't calling the right CDN of jQuery, now it's Run successfully.. using `contentType: "application/json; charset=utf-8"`, **savadata.php, 200, xhr, jquery.min.js:4, 15.5 KB, 19.76 s**, BUT there's no data add in MySQL DB!!!!!!!!!!!!! – Hossam Jul 24 '17 at 02:45
  • @AbhishekRanjan, Now I saw the `alert("Success Insert data");` but no data added to DB – Hossam Jul 24 '17 at 02:52
  • I did not figure out what is going on, So.. I used this technique and it worked.. [jquery send data to php without ajax](https://stackoverflow.com/a/8360810/2080202) – Hossam Jul 24 '17 at 04:11
  • remove the qoutes in your query: $qry = "INSERT INTO orderinformation VALUES ($platform, $source, ... ) – BorisS Jul 24 '17 at 06:26

1 Answers1

0

Try to view the network via chrome toolbar it can help you trace the request, read its header, also check the response data which might contain the error message

  • 1
    I got the problem, I wasn't calling the right CDN of jQuery, now it's Run successfully.. using `contentType: "application/json; charset=utf-8"`, **savadata.php, 200, xhr, jquery.min.js:4, 15.5 KB, 19.76 s**, BUT there's no data add in MySQL DB!!!!!!!!!!!!! – Hossam Jul 24 '17 at 02:50