0

what I want to do is whenever a user fills his address in text field input I want to grab it on blur with javascript which I am able to do .. now how to pass this variable in a php variable so that i can use it for some operations ? the javascript file is called inside php file ..

Prince Singh
  • 11
  • 3
  • 9

2 Answers2

1

PHP is a server-side language so after the page is called from the client's computer, there is no more PHP on the page but only converted to HTML. If you want to do something with the input in PHP on blur then you have to POST (easily done with jQuery) the data to a new instance of a PHP file so it can be compiled on the server.

Page with the form

$.post('name_of_php_file_to_send_data_to.php', {name: value}/* <-- data to pass to the PHP file*/,function(output) {
    // do something with the returned output of the PHP file
});
Noah Passalacqua
  • 792
  • 2
  • 8
  • 24
1

Pull the data from the form elements first

var dataObject = {};
// #search is the form with search params
$.each($('#form name').serializeArray(), function(i, field) {
dataObject[field.name] = field.value;
});

then POST the data to the php

    $.ajax({  
         type: "POST",  
         url: "./api.php",  
        data: dataObject,
        success: function(dataout) {
            //dataout is what is returned from php
            //process it how you like from here
        }
    });

in php do something with POST data

<?php
   $element1 = $_POST["form_element_name1"];
   $element2 = $_POST["form_element_name2"];
   ...do something
   print $result;
?>

the printed result is output to dataout in the ajax function.. process from there

  • @PrinceSingh geez I gave you a more complete example, you said it helped but marked the other answer as correct.. geez thnx .. not even an upvote.. – Patrick McWilliams Apr 26 '13 at 04:42
  • 1
    seems a bit more complex i gave a simple and complete answer to what he asked. great way to do it though! – Noah Passalacqua Apr 26 '13 at 05:24
  • 1
    @NoahPassalacqua lol... my bad, I didnt mean to sound like I was marginalizing your answer.. Yours was a very good answer as well, I was just being a smart@#! – Patrick McWilliams Apr 26 '13 at 07:49