0

i am facing an problem in sending data from ajax to php i would need some help

$.ajax({
            type: "post",
            url: "/raffle.php",
            dataType: "json",
            data: {
                "postraffle": "true",
                "title": $("#rtitle").val(),
                "message": $("#mess").val(),
                "maxentry": $("#maxentry").val(),
                "duration": $("#durr").val(),
                "filter": $("#reffil").val(),
                "split": $("input[name=split]:checked").val(),
                "pub": $("input[name=rafflepub]:checked").val(),
                "stype": $("input[name=stype]:checked").val(),
                "invo": $("input[name=invo]:checked").val(),
                "items[]": itms,
                "games[]": gmes,
                },
            success: function(data){
                if(data.status == "fail")
                {   
                alert(data.message);
                $("#rafBut").removeAttr("disabled");
                $("#rafBut").attr("value", "Raffle it!");
                }
                else if(data.status == "ok")
                {
                alert(data.message);
                }

            }
        });

and the php script is here

    <?php

    // getting data from AJAX 
    $raffle_title = $_POST['title'];
    $raffle_message = $_POST['message'];
    $raffle_maxentry = $_POST['maxentry'];
    $raffle_duration = $_POST['duration'];
    $raffle_filter = $_POST['filter'];
    $raffle_split = $_POST['split'];
  $raffle_pub = $_POST['pub'];
  $raffle_stype = $_POST['stype'];


  $done = false;

  $data = array(
      'status' => 'ok',
      'message' => 'saved! redirecting you!',
      'datakey' => 'HALLEYO!',
  );

  $host ="localhost"; // enter your host.
  $pass =""; // enter your password.
  $db = "test"; // Enter your database..
  $user ="4"; // enter your username.

  # MYSQL Connection

  $con=mysqli_connect($host,$user,$pass,$db);
  if (mysqli_connect_errno($con))
  {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  foreach($_POST['items'] as $item){
    $query = "INSERT INTO table (heading,content,items) VALUES ('".$_POST['title']."', '".$_POST['message']."','".$item."')";
    // this should also be done for each item
    if (!mysqli_query($con, $query)) {
      printf("Error: %s\n", mysqli_error($con));
    }
  }

  echo $data;

  ?>

Now the function of the above script is to get the data from ajax and upload it to mysql database and send an response to the ajax script back which currently doesnt work. i think there may be problem with my mysql query (php mysqli parameterized queries) Some help would be really appreciated. Thanks!

Karan
  • 13
  • 4
  • Have you checked the contenty type of the php page sending the ajax response it should be 'application/json' http://stackoverflow.com/questions/19155192/jquery-ajax-call-to-php-script-with-json-return – Sorter Feb 05 '14 at 08:55

2 Answers2

4

Try replacing

echo $data;

with

echo json_encode($data);

echoing data will give just "Array" string, not anything JSON encoded

  • Hello! thanks for the reply but it doesn't upload in mysql and the message is not transmitted in ajax – Karan Feb 06 '14 at 08:08
0

You can not print arrays!

You must chenge it echo $data; to echo json_encode($data);.

Mohammad
  • 492
  • 5
  • 4
  • Hello! thanks for the reply but it doesn't upload in mysql and the message is not transmitted in ajax – Karan Feb 06 '14 at 08:09