By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,385 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,385 IT Pros & Developers. It's quick & easy.

Setting a PHP value in JS for SQL

LuiePL
P: 24
I'm looking for a method of using the value of a variable from Javascript to use it as the "WHERE" clause in my SQL search which is built in PHP within the JS function.

I got it to display the value that was selected from the drop-down that calls the function properly, so I know it's passing it correctly, I just can't figure out how to get it to use the value.

You can find what I'm working on here. It's the one on the right under "Group 3".

This is my function now:
[HTML]<SCRIPT language="JavaScript">
<!--
function nameChange(Name){

<?php

$dbh = mysql_connect ("localhost", "name", "password") or die ('Database Connection Error: ' . mysql_error());
mysql_select_db ("database");

$query = "SELECT * FROM Users WHERE Number='$number'";
$sql = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($sql)
?>

var memberName = Name;
var strName = "The name is: "+memberName+"<br />";
strName = strName+"First Name:<input type='text' name='fname' size='20' value='<?php $row['FirstName'] ?>'>";
strName = strName+"<br>Last Name:<input type='text' name='lname' size='20' value='<?php $row['LastName'] ?>'>";

document.getElementById("divName").innerHTML = strName;
}
// -->
</SCRIPT>[/HTML]
Sep 12 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
What your attempting has no chance of ever working.

The problem is that javascript runs client side and PHP runs server side. By the time your page has reached the client (browser) and is running the Javascript the php has been run already and no longer exists in the file (try doing a view source and looking at your nameChange function).

To do what you are trying to do you need to resubmit the data to the server so that it may run another php script to obtain the new data. Or you have to obtain all the data from MySQL when the page loads using php and embedd all that data into the page as Javascript data that it can select values from (although is is, of course,not at all secure).
Sep 12 '06 #2

LuiePL
P: 24
I guess that makes sense. Would I have to use another function called from the first to run the SQL to get the result? I.E. Call the function to update the value, and call the function to run the SQL and get the info based on the updated field?

Something along the lines of:

[HTML]function changeNum() {
getName(First, Last, Number)
etc...
}

getName(First, Last) {
SELECT * FROM table WHERE Number=Number
etc...
}[/HTML]

If so, how would I pass the Number field properly from the javascript to the PHP/SQL code?
Sep 12 '06 #3

LuiePL
P: 24
I guess one example of what I'm trying to do is when you do a survey, and you get an instant result without the page reloading (ie use of AJAX). So I'm just trying to figure out how to use AJAX in this scenario.
Sep 13 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.