PSN PSN - 2 years ago 67
PHP Question

Sending data from javascript to php using via Ajax using jQuery. How to view the data back?

I know this question has been asked a lot as I have been googling since morning but I just can't think what's going on in my code.

I have a index.php (which is my website) that uses forms to receive information from user, then invokes a javascript file separately which in turn invokes another backend php file (this backend php file queries a database using mysql and does some stuff).

The problem is I am not sure what information is getting passed to the backend php from my js. I have just learnt jQuery and Ajax so I really don't know what is that small mistake I am making.

From my understanding, the backend php does its stuff and passes the value to javascript to be displayed on the web page. But my data is not getting sent / displayed. I am getting a error 500 internal server error.

Here are the pieces of code that are currently is question:


var data1 = {week:week, group:grp_name};

dataType: "json",
type: "POST",
url : "php/remindUsers.php",
success : function(response){
alert ("success !");
error : function(response){


PHP backend (remindUsers.php):


if (isset($_POST['week'])) {
$week = $_POST['week'];

if (isset($_POST['group'])) {
$group_name = $_POST['group'];

echo $week;

I am ommiting out the sql code pieces because they work fine.

Edit: Now my status code is 200, response text is also ok . My response text shows a weird "enter' sign next to the actual response text expected. Is this normal ? And it is still going into the error block , not the success block of code.

Answer Source

I can not fully answer your question because I need more debug information about whats going on but theres 2-3 things about your code bugging me a little that might fix your bug.

First, use isset in your backend like this:

if (isset($_GET['your_input_name'])) {
    $someData = $_GET['your_input_name'];

The isset part is very important here. Set it up and try it again. If you stop having a 500 error. Its probably because your data was never send to your backend or because your not checking the good input name.

Second, About input name. I can see in your code that you send:

var data1 = {week:week, group:grp_name};

So in your backend you should use the name of the value like this to retrieve your data:

$week = $_POST("week");

Third, I am not a json pro but maybe your json is not valid. Even if he is ok I suggest you build a "cleaner" one like this:

var data = [
        { 'name' : 'week', 'value' : week}

And finally, if you are using forms to send data to php then you can use something like that :

 var myForm = $("#myForm").serializeArray();

  url: 'yourUrl',
  type: "GET",
  data: myForm,
  dataType: 'json', 
  success: function(res){
      //your success code

  error: function(){
    //your error code

I hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download