JavaScript: Post to PHP (AJAX)

abell12 · 180K views · 2012 · 11:29

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it as a beginner-friendly walkthrough of posting JavaScript values to PHP with AJAX, both in answers and by askers who followed it.

Recommended by 1 people across 1 different months, 2016 to 2020.

What it is

A short 2012 screencast by abell12 showing how to send JavaScript variables to a PHP script using an AJAX POST request, with jQuery-style code. It walks through a basic client-to-server example for beginners.

What people said

Recommends Sascha · 2016-07 · Stack Overflow · on “How to get a variable from php to JS and back”

You should use AJAX if possible it is more comfortable and with jQuery not hard to use/understand. But if this topic is new for you, you can try to send it via a window.location.href. I looked for some good examples for you: PHP to JS [LINK] Without AJAX [LINK] Video - JS to PHP with jQuery POST [this video] To save PHP variables from PHP to Javascript you often just print them. If you're more familar with Javascript and PHP, you should try to work with JSON-Strings. But I think that would be to much for your first try. Hope that helps.

Cites Jake James · 2018-01 · Stack Overflow · on “Why wouldn't my ajax run? No errors being thrown”

I am trying to get the id of the button when its clicked process it through AJAX like shown in index.php and just echoing it out simply. Howevr when I do that PHP tells me that postid is an undefined index . What's the problem here please? As I am new to AJAX I am following this [this video] . Index.php: <button type='submit' id=8 onclick='addToCart(this.id)'></button> function addToCart($id) { var idStored = $id; //When the button is clicked it passes the id. Even .val() wouldn't work $.post('../Cart.phtml',{postid:idStored}, function(data){ window.alert(data); //Just for testing and conforming the code runs. }); } CartPage.php: <?php $name = $_POST['postid']; echo $name; ?> Many thanks, any hel

Cites computerscientist · 2020-04 · Stack Overflow · on “JQuery AJAX - passing data from form to PHP script”

My task is triggering php script from HTML form, which is updating JSON file from which HTML is generated. Here is how i am trying to do that. HTML Form: <form method="post"> <label for="board">Board to update:</label><br> <input type="text" id="board" name="board"><br><br> <input type="button" value="Submit" onclick="update()"> </form> Javascript: function update() { var board=$('#board').val(); $.post('update.php',{boardname:board}, function() { alert("update successfull"); }) } and PHP: <?php $q=$_POST['boardname']; echo ($q); shell_exec('curl [LINK] ' . $q . '/?num=1000 > posts.json'); ?> There is no error after clicking button, but website is not updating. Can you tell me why is that? W