JavaScript: Post to PHP (AJAX)




A video tutorial showing you how to post JavaScript variables to PHP using AJAX

http://www.abell12.com
http://www.twitter.com/abell12youtube
http://www.facebook.com/pages/Abell12/265102633605437

Original source


20 responses to “JavaScript: Post to PHP (AJAX)”

  1. Great video!!

    I have a question, if I wanted to use this on a link instead of a button like the video, how could I get it to work? I've tried embedding the $.post function from your video into a click function for the link so far and it works by getting the elementID (a unique id for that anchor tag) and storing it in a variable. This part works. However, getting it to post to my php file thus far has been unsuccessful . It's for a project in college so any help at all would be appreciated!!

  2. Hi even if I copy your code 100% except for <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script&gt;

    It does nothing any advice?

    <head>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script&gt;

    </head>

    <body>

    <form>

    <input type="text" id="name" placeholder="name">

    <input type="text" id="age" placeholder="age">

    <input type="button" value="submit" onclick="post();">

    </form>

    <div id="result"></div>

    <script type="text/javascript">

    function post()

    {

    var name =$('#name').val();

    var age =$('#age').val();

    alert('name s = ' + name);

    $.post('validate.php',{postname:name,postage:age},

    funtion(data)

    {

    if(data=="1")

    {

    $('#result').html('You are over 18');

    }

    if(data=="0")

    {

    $('#result').html('You are under 18');

    }

    });

    }

    </script>

    //validate.php
    <?php

    $name = $_POST['postname'];

    $age = $_POST['postage'];

    if ($age >=18)

    {

    echo "1";

    }

    else

    {

    echo "0";

    }

    ?>

Leave a Reply