0

when I use this code nothing and call the ajax nothing happens and I was looking at some online forums saying I need to use json encode to pass variables over ajax but it just echos it at the top of the webpage.

AjaxTesting

    <html>
    <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
    $("#test").click(function(){
        $.ajax({
            event.preventDefault(),
            url:'ajaxUser.php',
            type:'POST',
            dataType: 'json',
            success:function(data){
                alert(data.id);
            }
        )};
    });
    </script>
    </head>
  <body>
    <form method="post">
      <input name="userName">
      <input name="submit" type="button" value="Submit" id="test">
    </form>
  </body>
</html>

ajaxUser

    <?php
    echo json_encode(array('id' => 123, 'messageid' => "test"));
    ?>
11
  • you can get the those data in the ajaxTesting.php like $_POST['time'] etc Commented Jan 16, 2017 at 5:00
  • @guradio sorry I didnt come off right I mean like I want ajax to alert() something from php this is just a small example that I dumbed down Commented Jan 16, 2017 at 5:02
  • what do you get in alert? Commented Jan 16, 2017 at 5:03
  • nothing it doesn't work at all Commented Jan 16, 2017 at 5:04
  • 1
    event.preventDefault(), what is this?remove this Commented Jan 16, 2017 at 5:27

2 Answers 2

1

You don't have event parameter called in click function. Don't call preventDefault() within ajax function ,also you need to code your js within document.ready() if you want to write in header element !!

   <html>
    <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>       
    </head>
  <body>
    <form method="post">
      <input name="userName">
      <input name="submit" type="button" value="Submit" id="test">
    </form>
    <script>
    $("#test").click(function(event){
        event.preventDefault();
        $.ajax({
            url:'ajaxUser.php',
            type:'POST',
            dataType: 'json',
            success:function(data){
                alert(data.id);
            }
        });
    });
    </script>
  </body>
</html>

Should work

Sign up to request clarification or add additional context in comments.

1 Comment

Now that I can retrieve data thanks to you and some others is there a way to send data such as userName ?
0

The php code needs to be in a different file named AjaxTesting.php and you never call the function post() in the javascript. So we can call it inside a form submit event handler

Try adding:

$(function(){
   $('form').submit(function(event){
       event.preventDefault()
       post();
   })
});

Also note that the html page needs to be running on the same server and not from local file for ajax to work

7 Comments

Updating my question to what I now have but still not displaying anything.
Look in browser console dev tools network tab to see if request is made and what it;s status is
it navigated to the ajaxTesting.php and shows Uncaught SyntaxError: Unexpected token .
what's the rest of the message? Those error messages are meaningful and important and will also tell you where in the code the problem is
note that the echo needs a space after php opening tag. Inspecting the request in dev tools network probably shows a 500 server error status
|

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.