Pass data from Javascript to php?

by 9 replies
12
Is it possible to pass a data from JavaScript to PHP? Can anyone give the code for that?
#programming #data #javascript #pass #php
  • You've asked a rather generic question, so I could, in theory, just give you a generic answer, say do this with AJAX, but it would be better for me to ask you, how would passing data from JavaScript to PHP be useful to you? What is your idea?
  • AJAX, JSON can be used to pass data to php
  • It is worth to mention that Javascript is Client Side Script which is
    run on client computer and PHP is Server Side Script which is run on
    Server computer.

    So all you need to do is to pass your data through Session or Query strings.
    This makes the browser refresh. To avoid this problem, use AJAX as
    wayfarer pointed out.
  • Yes, it is. I made a calculator once that passed data generated by JavaScript to a PHP script to be crunched. I used AJAX for that.
  • Use AJAX for this purpose
  • -Use AJAX for interactive, stay on the same page usage.
    -Have JavaScript create GET style url's for letting the user choose one of many (with small amounts of data e.g. http://myDomain.com/myPhpScript.php?arg1=abc&arg2=xyz
    -Have JavaScript create a POST style request to the server when there is more data to send, but pretty much only one point of interaction on the page (and when you want the viewer taken to a new page.)

    Harrison
  • If you don't want to spend time learning how to make AJAX post (USEFUL and you should learn), here is a simpler way to do this:

    Have your javascript output a value for a hidden field in a form (I assume you want this information passed into a form?)

    So depending on your needs either write .onclick() or .onload() event that will return the variable to the hidden field. Make sure your hidden field is IN your form.


    Maybe you can give us example of what you're doing and we can help you with more specific code sample.
    • [1] reply
    • you should use AJAX.. it uses xml http request to communicate to the server without redirecting the page
  • everyone has said AJAX which is right, but no one has said the magic jQuery word...i highly recommend using jQuery to do this is a simple 1-3 line command. Read here:

    jQuery.post() – jQuery API

    Ex:

    $("#myButton").click(function()
    {
    $.post("sendemail.php", { contactname: "John", contactemail: "a@a.com" }, function(data) {
    alert("Success: " + data);
    }); );
    });


    I do this to asynchronously send emails via a contact form. The function(data) is the callback function that is run once the script you post data to (in this case sendemail.php) executes the code and responds back. You will need to have a script that handles this in your post url, here is an example of how to get posted data in this script and respond back to the original page's request (my sendemail.php):

    $contactname=$_POST['contactname'];
    $contactemail=$_POST['contactemail'];

    $emailTo = 'myemail@a.com';
    $body = "email message";
    $headers = 'From: <'.$contactemail.'>' . "\r\n" . 'Reply-To: ' . $contactemail;
    mail($emailTo, "mail subject", $body, $headers);
    $emailSent = true;

    if($emailSent == true){ echo "true"; }
    else{ echo "false"; }

    In this case if the email sends successfully, you will get a message box with true, if not, it will say false.

    Hope this helps.

Next Topics on Trending Feed