0

I am trying to call a javascript function from php code, which it will receive a php variable from an input form. Once Javascript function received this variable or value it will call a php (search.php file) to make a query and other jobs.

PHP code:

<?
I need to call search function in php passing the $_POST Global variable value :
something like this: <script> search($_POST) ; </script>
?>

Javascript file name: call_search.js:

function search($_POST or variable) {
    var search_query = $(this).val();

    $.post('search.php', {search_query : search_query}, function(searchq) {
        $('#search_query').html(searchq);
    });
}

Nota: search.php is another php file doing mysql query and others jobs which it send back the result: MySQL query back to Javascript function.

Any help ..Please.!! Thank you very much.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
Victor
  • 3
  • 1

6 Answers6

3

Something like this should work:

<script type="text/javascript">
   search(<?php echo $_POST; ?>);
</script>

However, note that it's probably a bad way to pass variables - try looking into Javascript frameworks and retrieving any information you need through a RESTful API on the backend.

Jeroen
  • 13,056
  • 4
  • 42
  • 63
1

For JS arguments you can send Object.. Use json_encode in PHP

echo '<script>';
echo 'search('.json_encode($_POST).')';
echo '</script>';

in JS:

function search(obj) {
    console.log(obj);
}
0

You are doing it wrong or I did not understand what exactly you are doing.

Give the search field an id (searchInput) and get the value in jQuery and from there send it to php to recive an response.

function search() { 
var search_query = $('#searchInput').val();

$.post('search.php', {search_query : search_query}, function(searchq) {
    $('#search_query').html(searchq);
});

}

Hope this helps. Good luck young padawan and may the source be with you.

mondjunge
  • 1,219
  • 14
  • 24
  • I have not bee available to make it work, I have tried each of suggestions in forum. How can I pass the id(search Input) from index.php to file2.php . I do know $_GET and $POST, but I dont konw how tell file2.php check the value in search field id: (searchinput) – Victor May 31 '13 at 07:28
  • You are messing things up here! Good for you the http Protocol is quite easy. It works this way: OneClient Request -> One Server Response. – mondjunge Jun 14 '13 at 13:58
  • You are messing things up here! The (Javascript) code you see here runs in your browser (Clientside), it can look up a value in your HTML and submit a POST Request to the server (here: to the file search.php) in search.php you can process the POST vars with . You do not need to "tell file2 to check value in search field", in fact, the server can't do that cause your input field sits in your Browser and have to be submited to an php file one way or another, in order to be processed. – mondjunge Jun 14 '13 at 14:04
0

If you add the java script to your code

echo "<script src=\"call_search.js\"></script>";

and echo this as an event in your php it will work:

echo "<body onLoad=\"search($param)\">";

or

echo "<script> search($param) </script>";

it will load when the page is loaded.

orezvani
  • 3,595
  • 8
  • 43
  • 57
0

If i have understand your question, the form must have a simple button rather than submit button, and juste use serialize() to get all form data by id

$.post('search.php', $('#IDform').serialize())

You Will find more details here

Community
  • 1
  • 1
chakroun yesser
  • 1,407
  • 1
  • 12
  • 18
0

You could also take a look at SpiderMonkey. Its a javascript engine for PHP which has the facility to let you access PHP variables and classes from Javascript. I agree with Jeroen, though. Its probably not the best solution in the long run.

Padawan
  • 370
  • 1
  • 10