1

What I'm trying to accomplish is the following. I have a php page which loads some variables from an mysql db. Then I want to send these variables using GET to another page without opening the page where the variables are being send to.

At first I really didn't know how to do this until I came across the following:

            $( document ).ready(function()
            {
                $.ajax({
                  url: 'the_url',
                  type: 'GET',
                  data: {Gender:Male,DateOfBirth:1968-07-21},
                  success: function(data) {
                    //called when successful
                    alert("Succes");
                  },
                  error: function(e) {
                    //called when there is an error
                    console.log(e.message);
                    alert("Failed");
                  }
                }); 
            }

The $test and $test1 are php variables which I would like to send to the other page. But apparently i'm doing something wrong. Not even the alerts are being triggered so there is probaly something wrong with my syntax.

I'm using the following jquery version: jquery-2.1.4.min.js

If there is something wrong in the way I asked this question please let me know and give me the help to update the question.

jh314
  • 27,144
  • 16
  • 62
  • 82
Graham
  • 1,850
  • 4
  • 21
  • 41
  • 2
    you dont have an ending quote for your `data` option, is that a simple typo from typing the question? – Patrick Evans May 13 '15 at 13:18
  • http://stackoverflow.com/questions/18701282/what-is-content-type-and-datatype-in-an-ajax-request – Saty May 13 '15 at 13:20
  • also you cannot send php variable like this. – Alive to die - Anant May 13 '15 at 13:20
  • @PatrickEvans That was a typo from typing the question. – Graham May 13 '15 at 13:22
  • @saty That is for using POST – Graham May 13 '15 at 13:23
  • @anantkumarsingh What would you think is the best way to achieve what I want? – Graham May 13 '15 at 13:23
  • Whenever you are doing something with js, it's useful to have developer console open. – VeeeneX May 13 '15 at 13:25
  • I've updated the part of my question to the new code. This still doesn't give me an alert. What would be the best way to debug this problem? – Graham May 13 '15 at 13:33
  • found the java console. Now it has problem with the number which is also in my updated question. Should I put this between qoutes? – Graham May 13 '15 at 13:35
  • I've solved the problem with the number. I'm also trying to send an email address and a zip code which gets the following error: Uncaught SyntaxError: Unexpected token ILLEGAL – Graham May 13 '15 at 13:50
  • If you already have the GET variables on page load and you are doing nothing to modify them client-side (i.e. in javascript), why would you not simply do whatever processing this second PHP file does at initial page load and do away with AJAX call altogether? – Mike Brant May 13 '15 at 13:54
  • Could you explain that a bit more? I have a page that loads for example 3 rows. Then each row needs to be send to another page without reloading the page. – Graham May 13 '15 at 14:00

1 Answers1

1

Just assign PHP variables to JS variables and also change the data sending part as well there is no need of ' for data sending.

$( document ).ready(function()
{
    var test = '<?php echo $test; ?>';
    var test1 = '<?php echo $test1; ?>';
    $.ajax({
            url: 'the_url',
            type: 'POST',   //change type to POST rather than GET because this POST method of sending data
            data: {test:test,test1:test1},
            success: function(data) {
                 //called when successful
                 $('#ajaxphp-results').html(data);
                 alert("succes");
            },
            error: function(e) {
                 //called when there is an error
                 console.log(e.message);
                 alert("failed");
            }
    }); 
 }
Sunil Pachlangia
  • 2,033
  • 2
  • 15
  • 25
  • This will cause the script to error out if `$test` or `$test1` happen to be strings and not contain quotes – Patrick Evans May 13 '15 at 13:26
  • Could you look at my updated question above with the new code? Because now I get the following error: Uncaught SyntaxError: Unexpected number – Graham May 13 '15 at 13:39