473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Setting a PHP value in JS for SQL

LuiePL
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
3 1701
Banfa
9,065 Expert Mod 8TB
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
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
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

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

Similar topics

6
by: Niyazi | last post by:
Hi, We have and IBM AS400 and I belive the reional setting is Turkish. The IBM Client-Access for Windows that install in our PC (WIN XP SP2) set to Turkish characters. Now my PC has English...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
0
by: Shravan | last post by:
Hi, I am setting a row value in my grid using ((DataRowView)grid.ListManager) = value; if the given rowNo is not the current position in the currencymanager, setting some value in the row is...
3
by: Patrick | last post by:
I am dynamically creating TextArea and drop-down lists in ASP.NET using something like HtmlTextArea eachTextArea = new HtmlTextArea(); I tried to set the "name" of these TextAreas, etc. (e.g....
3
by: Patient Guy | last post by:
Subject line would seem to say it all: How does one trigger the execution of a method within an object or any other code/function with the setting of an object property? More elaboration for...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
6
by: DippyDog | last post by:
This is an old old post that I'm referencing regarding what happens when you set an integer variable to Nothing. It gets set to zero, not "Nothing." ...
8
by: Andrus | last post by:
..NET 2 Winforms application. How to create new setting and set it default value in userSettings section of app.config file or overwrite existing setting value ? I found code below in this list...
7
by: PetterL | last post by:
I have a setting called My.settings.firstrun set to True, set in the setting manager. When i read this in the first form form_Load in a IF sentence it always come out as false. I have tried to...
2
by: BobRoyAce | last post by:
I am using VB.NET and I have a variable that is defined as Nullable(Of Single) as follows: Dim fMyNullableSingle as Nullable(Of Single) Then, I have another variable as follows: Dim...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.