getting a php variable from a hidden input value , to use in mysql query
I created a modal window, and was able to send an ID value to a hidden input field that opens up in the modal window. I need to transfer the value of that ID so that I can use it in a mysql query, so I can grab more info from the database...
$member_query = mysql_query("SELECT name, job FROM members WHERE (member_id='$d_id')");
while($row = mysql_fetch_array($member_query)){
$member_name = $row["name"];
$member_job = $row["job"];
}
I was thinking to use some sort of javascript when the modal window loads like "onload getelementbyid"... so basically I need to somehow transfer the hidden input value (123), into a php variable $d_id. Is there an elegant way to do that?
There are a number of ways by which you could achieve this result. A simple method would be to submit the form onLoad and have PHP handle it as a $_GET or $_POST.
Another method might be to use an AJAX/PHP request, which would require use of some simple HTTP request code in JavaScript along the lines of:
Code:
window.addEventListener("load", send_to_php, false);
function send_to_php()
{
var value= document.getElementById("some_element").value;
var xmlhttp = "";
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
// Handle HTTP response here.
}
}
xmlhttp.open("GET", "some_page.php?some_param=" + somevalue, true);
xmlhttp.send();
}
This tutorial on AJAX & PHP, while not directly related to your question, could be of help to you here since it explains fully how to send JavaScript data to PHP and uses MySQL to query a database.
Bookmarks