0
<script type="text/javascript">
$(document).ready(function(){
$("#message").hide();
$("#please_wait_box").hide();
$("#addnotification").submit(function(e){
    $("#message").hide();
    $("#please_wait_box").show();
    e.preventDefault();
    dataString=$("#addnotification").serialize();
    $.ajax({
        type: "POST",
        url: "menu.php?addnotification=yes",
        cache: false,
        data: dataString, 
        success: function(res){
            $("#please_wait_box").hide();
            $("#message").html(res);
            $('#message').fadeIn('slow');
            $('.overlay').fadeOut();
            if(res.indexOf("success")!=-1)
            {
                window.location.href = res.substr(8);
            }
        }
    });
});
});
</script>

i am trying to run this ajax code to POST data to menu.php page from a submitted form

on menu.php i have

if($_GET["addnotification"] == 'yes') {
    //do stuff here
    echo 'form submitted';
}

but i cannot see the text form submitted

UPDATE

i have changed to this code:

<script type="text/javascript">
$(document).ready(function(){
$("#message").hide();
$("#please_wait_box").hide();
$("#addnotification").submit(function(e){
    $("#message").hide();
    $("#please_wait_box").show();
    e.preventDefault();
    dataString=$("#addnotification").serialize();
    $.ajax({
        type: "POST",
        url: "addnotification.php",
        cache: false,
        data: dataString, 
        success: function(res){
            $("#please_wait_box").hide();
            $("#message").html(res);
            $('#message').fadeIn('slow');
            if(res.indexOf("success")!=-1)
            {
                window.location.href = res.substr(8);
            }
        }
    });
});
});
</script>

and put the SQL queries on a different page, but now the please_wait_box shows and does not hide, the queries are not running at all

2
  • your GET key should be addnotification? Commented Jan 20, 2014 at 19:02
  • try alert(res); do you get form submitted text in alert box? Commented Jan 20, 2014 at 19:06

3 Answers 3

1

In your url you've got no blank, but in your php document there is a blank.

menu.php?addnotification=yes

vs

$_GET["add notification"]
Sign up to request clarification or add additional context in comments.

1 Comment

made a mistake when typing :)
0

You are sending a POST request and then reading the $_GET response, try changing the

type: "POST",

for

type: "GET",

in your ajax call.

and also read addnotification and not add notification (probably a typo)

1 Comment

OP is sending addnotification as GET paramater so it is not required to change POST to GET.
0

Check your this part of code:

if(res.indexOf("success")!=-1)
{
  window.location.href = res.substr(8);
}

You are searching for success in res whereas i think your res will be a string "form submitted". You can try calling alert(res); or console.log(res) to see what is being returned in your response.

Additional debugging process: change your code for ajax to below:

var request = $.ajax({
    type: "POST",
    url: "addnotification.php",
    cache: false,
    data: dataString
});
request.done(function( msg ) {
  $("#please_wait_box").hide();
  $("#message").html(msg);
  $('#message').fadeIn('slow');
  if(msg.indexOf("success")!=-1)
  {
    window.location.href = msg.substr(8);
  }
});

request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});

2 Comments

What do you get when you do alert(res);?
Try edited code for debugging your code. Make sure your php file addnotification.php is accessible. you can use developer tools in chrome to see what actually is being sent in request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.