How to pass data from Javascript to PHP and vice versa?
Passing data from PHP is easy, you can generate JavaScript with it. The other way is a bit harder - you have to invoke the PHP script by a Javascript request.
An example (using traditional event registration model for simplicity):
<!-- headers etc. omitted -->
<script>
function callPHP(params) {
var httpc = new XMLHttpRequest(); // simplified for clarity
var url = "get_data.php";
httpc.open("POST", url, true); // sending as POST
httpc.onreadystatechange = function() { //Call a function when the state changes.
if(httpc.readyState == 4 && httpc.status == 200) { // complete and no errors
alert(httpc.responseText); // some processing here, or whatever you want to do with the response
}
};
httpc.send(params);
}
</script>
<a href="#" onclick="callPHP('lorem=ipsum&foo=bar')">call PHP script</a>
<!-- rest of document omitted -->
Whatever get_data.php
produces, that will appear in httpc.responseText. Error handling, event registration and cross-browser XMLHttpRequest compatibility are left as simple exercises to the reader ;)
See also Mozilla's documentation for further examples
How to I send data from JavaScript to PHP and vice versa?
If you wish to submit this data via a form, you don't need to create the form with Javascript. Simply create an invisible form with HTML, populate the hidden field with Javascript, and automatically submit whenever you're ready.
<form method="post" action="process.php">
<input type="hidden" name="data" id="data" />
</form>
document.getElementById("data").value = "foo";
If you want to send this in an ajax-style fashion, I would suggest implementing jQuery, which makes this extremely easy. Note the previous case converted to a jQuery solution:
$.post("process.php", {data:"foo"}, function(results){
// the output of the response is now handled via a variable call 'results'
alert(results);
});
How do I pass JavaScript variables to PHP?
You cannot pass variable values from the current page JavaScript code to the current page PHP code... PHP code runs at the server side, and it doesn't know anything about what is going on on the client side.
You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.
<DOCTYPE html>
<html>
<head>
<title>My Test Form</title>
</head>
<body>
<form method="POST">
<p>Please, choose the salary id to proceed result:</p>
<p>
<label for="salarieids">SalarieID:</label>
<?php
$query = "SELECT * FROM salarie";
$result = mysql_query($query);
if ($result) :
?>
<select id="salarieids" name="salarieid">
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="', $row['salaried'], '">', $row['salaried'], '</option>'; //between <option></option> tags you can output something more human-friendly (like $row['name'], if table "salaried" have one)
}
?>
</select>
<?php endif ?>
</p>
<p>
<input type="submit" value="Sumbit my choice"/>
</p>
</form>
<?php if isset($_POST['salaried']) : ?>
<?php
$query = "SELECT * FROM salarie WHERE salarieid = " . $_POST['salarieid'];
$result = mysql_query($query);
if ($result) :
?>
<table>
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>', $row['salaried'], '</td><td>', $row['bla-bla-bla'], '</td>' ...; // and others
echo '</tr>';
}
?>
</table>
<?php endif?>
<?php endif ?>
</body>
</html>
passing PHP variable in Javascript and vice versa
You need to quote the string in the onclick:
echo "<td><a href='#' onclick='disp_confirm(\"{$row->university}\")'>Delete</a></td>";
Pass Data from PHP to Javascript and Vice Versa
The jQuery Javascript library provides a bunch of great ways for handling Ajax. JSON is a wonderful format for client/server communication, even as-is. XML is also good.
If you are talking about long polling, you may also want to look into COMET
Pass javascript variable to php code?
You can't directly do that, since JavaScript runs on the client-side and PHP gets executed on the server-side.
You would need to execute the JavaScript first and then send the result to the server via a FORM or AJAX call.
Here's what that might look like:
PHP
$parameter = "this is a php variable";
echo "var myval = foo(" . parameter . ");";
JavaScript
var myval = foo("this is a php variable"); // generated by PHP
$.ajax({
type: 'POST',
url: 'yourphpfile.php',
data: {'variable': myval},
});
Receiving PHP (yourphpfile.php)
$myval = $_POST['variable'];
// do something
How to pass the value of javascript into a PHP variable and stored into mysql database?
In order to save JS value to a PHP variable you need to make a server call.
You can easily do the vice versa like this:
var jsVariable = '<?= $php_variable ?>';
Have an ajax call, send the data to server side and store it in a variable.
Related Topics
PHP Connection Failed: Sqlstate[Hy000] [2002] Connection Refused
Need to Write At Beginning of File With PHP
PHP Json_Encode Class Private Members
Posting Raw Image Data as Multipart/Form-Data in Curl
PHP: Access Array Value on the Fly
Insert Multiple Rows With One Query MySQL
Simple PHP Strpos Function Not Working, Why
PHP Error: Cannot Modify Header Information - Headers Already Sent
How to Get the Query Executed in Laravel 5? Db::Getquerylog() Returning Empty Array
Difference Between If () { } and If (): Endif;
Best Way to Test For a Variable'S Existence in PHP; Isset() Is Clearly Broken
What Exactly Are Late Static Bindings in PHP
How to Get Real Ip from Visitor
What Is Function Overloading and Overriding in PHP