Pass data from Javascript to php?

9 replies
Is it possible to pass a data from JavaScript to PHP? Can anyone give the code for that?
#data #javascript #pass #php
  • Profile picture of the author wayfarer
    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?
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[4267217].message }}
  • Profile picture of the author phpwolf99
    AJAX, JSON can be used to pass data to php
    {{ DiscussionBoard.errors[4270460].message }}
  • Profile picture of the author rahmanpaidar
    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.
    {{ DiscussionBoard.errors[4271048].message }}
  • Profile picture of the author dominicyordz
    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.
    {{ DiscussionBoard.errors[4274255].message }}
  • Profile picture of the author phpdev
    Use AJAX for this purpose
    Signature
    Ali Usman
    PHP, MySql, WordPress, API Programming, E-Commerce Site, Payment Integration, Twilio, SMS Marketing, Custom Development, Wordpress, Joomla, Interspire, BigCommerce, Volusion, 3dCart and many more...
    sales@bluecomp.net
    {{ DiscussionBoard.errors[4305943].message }}
  • Profile picture of the author Harrison_Uhl
    -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
    {{ DiscussionBoard.errors[4308105].message }}
  • Profile picture of the author TrueStory
    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.
    Signature

    Your business matters only to people that matter to your business[/U][/B] - Reach them?

    {{ DiscussionBoard.errors[4328580].message }}
    • Profile picture of the author JohnnyS
      you should use AJAX.. it uses xml http request to communicate to the server without redirecting the page
      {{ DiscussionBoard.errors[4332265].message }}
  • Profile picture of the author KabanaSoft
    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.
    {{ DiscussionBoard.errors[4333441].message }}

Trending Topics