开发者

How do I pass form data from JavaScript to PHP and back again?

开发者 https://www.devze.com 2023-02-28 06:45 出处:网络
I have a button like so: <a href=\"#\" onclick=\"do_something()\">Click Me</a> And a image field like this (or a YouTube embed, or something where a value needs to be changed):

I have a button like so:

<a href="#" onclick="do_something()">Click Me</a>

And a image field like this (or a YouTube embed, or something where a value needs to be changed):

<img src="<?php get_src(); ?>" />

When a user clicks the button, do_something() nee开发者_开发百科ds to send data from a PHP file, which processes an array (in this case JSON, but that's irrelevant), and sends back the next value.

Basically, when you open the page, get_src() contains the first item of the array, and clicking do_something() sends information to a PHP form to receive another item in the array, which in turn sends data back to the file, and updates get_src().

Do I have something conceptually wrong? I really don't know if this is how it works.


Long answer: use AJAX (tutorial here)


Using jQuery, you can bind an Ajax post to a PHP file as such:

$('#myLink').click(function() {
    var myData = 'myData';

    /* post JSON */ 
      $.post('myAjax.php', {data: myData},
            /* Callback function */                      
            function(data){
            }
      , 
      'json');  

});

In myAjax.php, you can have something like:

$myData = $_POST['data'];
// do something with data
echo json_encode($jsonResult);

When myAjax.php echo's the response, the jQuery callback function will be triggered, where you can run some execution.


Use AJAX, as kpower has suggested. Or even better, try JQuery Ajax, though later basically provides an easy and fun way to do ajax calls.

Here is a nice tutorial from sitepoint on jquery ajax.

Edit

At server side, to return some data to the calling AJAX method, you can use json_encode function. It generates a string from your data in JSON format. Then you can just echo that string to pass it back to the ajax callback function.

Remember, returning data as an response to ajax is just like printing data into a webpage using PHP, you just have to echo/print it.

Also be careful for these scenarios -

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>First Tag library Example - EL Function</title>
</head>
<body>
<?php echo $your_json_response_string; ?>

</body>
</html>

If you echo the string like above, then the entire content, including all the HTML tags and others will be sent as an response of the ajax call. If you just need to pass some data, say, some transaction amounts from database based on some request, do this instead -

<?php
    // do your processing, but print/echo nothing
    ..........
    // Convert your result into a json_string
    ..........

    echo $your_json_response_string;
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消