0

I have this code which uses jquery and ajax to send a request to the other page i.e.

<html>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

 <input type="button" id="butt"  value="button11111111111111" >
 </html>
 <script>

    $("#butt").on('click',function(e)
    {
        $.ajax(
            {
                type:'POST',
                url:'testmysql.php',
                data:
                {
                    product_type:"cake";
                }

            });

    });


</script>

When i click the above button it should send the request and data to the other file testmysql.php

<?php

session_start();

$_SESSION['type']=$_POST['product_type'];
echo $_SESSION['type'];
?>

However , when i refresh the other page after clicking the button i do not see any kind of echo and it gives me a notice stating

Undefined index: product_type

Since, i am new to ajax and jquery is there anything i am missing ?if yes,then what should i do to make this work ?

Thanks!

Note: Both of them are in the same directory.

2
  • why you are refreshing the page. you are sending a ajax request to the testmysql.php page. Commented Feb 14, 2016 at 7:16
  • i have sent a request to the other page which will set the data.So in order to view that i should refresh the page right ? Commented Feb 14, 2016 at 7:23

2 Answers 2

1

First of all, remove the semicolon(;) from this statement,

product_type:"cake";
                   ^

otherwise it will give you syntax error. And now comes to your issue.

when i refresh the other page after clicking the button i do not see any kind of echo and it gives me a notice stating Undefined index: product_type

That because when you refresh testmysql.php page, the $_POST array would be empty, and there would be no index named product_type in $_POST array. You can verify it using var_dump($_POST);.

On testmysql.php page you can check whether $_POST['product_type'] is set or not like this:

<?php
    session_start();

    if(isset($_POST['product_type'])){
        $_SESSION['type']=$_POST['product_type'];
        echo $_SESSION['type'];
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

@user5871514 On testmysql.php page you can do something like this: if(isset($_SESSION['type'])){ echo $_SESSION['type']; }
1
  • you added the ; at the end of the data object. remove it and try.
  • and you are sending a ajax request to the testmysql.php page, the $_POST['product_type'] will available when you are send the request. when you refresh the page the you are not sending any post request to testmysql.php page.

Comments

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.