|
 |
|
|
04-27-2006, 07:09 AM
|
php in javascript?
|
Posts: 1
|
Hello,
I've got a problem with my javascript function. I'd like to make sql query from it and pass its result back to function. What more I need to pass javascript variable to php select. Function must be in javascript bacause it's called by onchange event.
function foo(my_var)
{
<?php
$MyVar = "?>document.write(my_var);<?php";
$MyVar = str_replace("?>", "", $MyVar2);
mysql_connect('localhost', 'user', 'pass');
mysql_select_db('database');
$result = mysql_query("SELECT * FROM tableA WHERE id = $MyVar")
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$var2 = $row[0]."=".$row[1];
}
mysql_free_result($result);
echo $var2;
?>
}
Can I do sthg like that or is it totally wrong? How can I make it work?
Thanks in advance.
|
|
|
|
04-27-2006, 03:31 PM
|
Re: php in javascript?
|
Posts: 3,110
Location: Toronto, Ontario
|
You can't get values from Javascript and use them directly in PHP. They are executed in two different places and absolutely no interacting with each other. PHP is executed on the server, and Javascript is executed within the clients browser.
To get values from JS, then you will need to pass it some other way. Using a query string (page.php?my_var=value) is probably easiest.
|
|
|
|
05-04-2006, 10:33 AM
|
Re: php in javascript?
|
Posts: 145
|
Chroder I am having the same problem but I am not using database.
Could you please tell me how I can pass the value of a javascript in to a php?
Thank you ,
Xenia
|
|
|
|
05-04-2006, 10:48 AM
|
Re: php in javascript?
|
Posts: 1,626
Location: Guildford, UK
|
PHP Scripts are executed between the point at which the request for the page is made to the server, and the response is sent to the browser. Therefore, PHP has access to information about the request that was made, and is able to manipulate the response to the browser.
Javascript executes after the response has been sent to the browser, by which point the PHP script will have already executed - therefore Javascript has no interaction with PHP.
Browser Requests Page -> Request Information is sent to Server -> PHP script executes and creates response -> Response is sent to browser -> Browser displays response & executes javascript
What you need to do is create another request to the server somehow to pass a variable to the PHP Script. As Chroder said, you can call a page with a query string, the query string will be passed to PHP, and then you can do with it what you want from there.
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"
Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
05-04-2006, 11:28 AM
|
Re: php in javascript?
|
Posts: 145
|
Could you please give me an example of how I can do that?
|
|
|
|
05-04-2006, 01:11 PM
|
Re: php in javascript?
|
Posts: 1,626
Location: Guildford, UK
|
Unfortunately I can't, because (ironicly) I'm neither a JavaScript or PHP programmer
I'm sure someone else could though? -pokes Chroder-
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"
Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
05-04-2006, 01:12 PM
|
Re: php in javascript?
|
Posts: 3,110
Location: Toronto, Ontario
|
Code:
window.location = 'myscript.php?myvar=' + myvar;
Perhaps something like that.
|
|
|
|
05-18-2006, 11:11 AM
|
Re: php in javascript?
|
Posts: 17
|
hey hey Soleil_m!
i've encountered the very same problem last nite when i was trying to make a javascript validation function to check whether the username entered by a user already exists in my database.
at first, i was trying hard to get the value of entered username in javascript, pass it to php and check with connection to mysql, but it turned out that there is no way to pass value from javascript to php.
eventually, i tried the other way around and it works!
what i did was access mysql in php, echo each single one of the username in my database using a hidden input (each hidden input has an id), use document.getElementById to retrieve these usernames in javascript, and do the checking in javascript.
i think this is the most satisfying way and probably the only way of solving this problem. although these hidden inputs can be seen in the source code, so try not to pass anything confidential.
|
|
|
|
05-18-2006, 11:43 AM
|
Re: php in javascript?
|
Posts: 1,626
Location: Guildford, UK
|
Quote:
eventually, i tried the other way around and it works!
what i did was access mysql in php, echo each single one of the username in my database using a hidden input (each hidden input has an id), use document.getElementById to retrieve these usernames in javascript, and do the checking in javascript.
|
That's an incredibly bad way of doing it. You're chucking a huge amount of redundant data to the client, which is a huge waste of bandwidth - specially once your site grows to have thousands of users. From a security point of view, this makes it incredibly easy for anyone to pull of a list of your users and then they're half way into breaking into your site without even trying.
Much better to use a postback to the server, or evn better, AJAX.
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"
Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
05-18-2006, 12:57 PM
|
Re: php in javascript?
|
Posts: 17
|
Thanks for your criticism, I have a realization of that too.
Can you be specific or even put up an example of how you think this task should be done.
|
|
|
|
05-18-2006, 01:30 PM
|
Re: php in javascript?
|
Posts: 1,626
Location: Guildford, UK
|
I code in ASP.NET so you'll have to emulate this behaviour for PHP...
Basically, you have your form on your page which posts back to itself - therefore you can keep making round trips to the server and the page can update itself based on the user input. For example, when you hit submit, the page posts back to itself. The server side code on that page handles the post back, and checks the entered username against the database. If it's already taken, it simply re-loads the page, but then you could add a message telling the user that their username is already taken (Have the message hidden by default, and use an IF statement to display it or not based on if the username is taken or not.)
If the username isn't taken, the server side code could then execute some SQL to add the user, then redirect them off to another page to tell them their signup was successful.
Using AJAX, you'd catch the button click before the page was submitted (or have a sepearte 'check username' button or link). When this is clicked, a JavaScript function would create an XMLHTTP Object to make a call to the server with the entered username. (You'd have to have a seperate server side page to check this username and return an XML document containing the result.) Once the XML Document comes back from the server, another javascript function would be called which would either display a message if the username is already taken, or submit the form if it's not.
Quite a lot of the AJAX stuff can be done with some of the pre-written AJAX libraries that are out there, such as ATLAS. With ATLAS, I'd just create a server side web service function called CheckUsername(string Username) which would return a boolean value if the username exists or not. On my page, I'd create a reference to that web service, then I can directly call that server side function from my JavaScript code, almost completely transparently.
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"
Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
05-18-2006, 02:38 PM
|
Re: php in javascript?
|
Posts: 17
|
Thanks for your reply.
Another question, how to pass values from main window to a popup window as php variables?
|
|
|
|
05-18-2006, 03:21 PM
|
Re: php in javascript?
|
Posts: 17
|
Never mind, I found the answer to this myself.
|
|
|
|
08-20-2006, 04:32 PM
|
Re: php in javascript?
|
Posts: 843
Name: Mike
Location: United Kingdom
|
WHat is it?
__________________
My Blog/Site: Please login or register to view this content. Registration is FREE
|
|
|
|
08-21-2006, 12:29 PM
|
Re: php in javascript?
|
Posts: 256
Location: Auckland, New Zealand
|
AJAX will help you out in this situation probably better but what about a fallback method for those who disable Javascript?
I can see what you're doing, and all you're trying to do is avoid a submission/page load when they change a select option. I could write you a very simple means for doing this, with page reload, otherwise I will have to write you a very complex way to do this, using AJAX technology to avoid the page loading. The AJAX way is probably the better option security wise.
I'll see what I can come up with, I may or may not get it done today, but I'll get it done soon enough.
Cheers,
MC
__________________
#------------------------------ signature---------------------------------------------------------------------------------#
Quote:
|
I am well recognised for what I don't do than what I do. Chores are just one of those things.
|
|
|
|
|
08-21-2006, 01:15 PM
|
Re: php in javascript?
|
Posts: 256
Location: Auckland, New Zealand
|
Here's the example:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Javascript to PHP</title>
<script type="text/javascript">
//<![CDATA[
function callphp(val) {
window.location.href = "<?php echo $_SERVER['PHP_SELF']; ?>?gender="+encodeURIComponent(val);
}
//]]>
</script>
</head>
<body>
<?php
if(isset($_REQUEST['gender'])) {
// This is where you could execute some PHP functions to output information from a database, etc.
if(strcmp($_REQUEST['gender'], 'male') === 0 || strcmp($_REQUEST['gender'],'female') === 0 || strcmp($_REQUEST['gender'],'alien') === 0) {
$selected = $_REQUEST['gender'];
echo ' <p>Your selected gender is '.$selected.'</p>';
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Simple Form</legend>
<select id="gender" name="gender" onchange="callphp(this.value);">
<option label="Male" value="male"<?php if(isset($selected) && strcmp($selected,'male') === 0) echo ' selected="selected"'; ?>>Male</option>
<option label="Female" value="female"<?php if(isset($selected) && strcmp($selected,'female') === 0) echo ' selected="selected"'; ?>>Female</option>
<option label="Alien" value="alien"<?php if(isset($selected) && strcmp($selected,'alien') === 0) echo ' selected="selected"'; ?>>Alien</option>
</select>
<input id="submit" name="submit" type="submit" value="Send" />
</fieldset>
</form>
</body>
</html>
Basically, when an onchange event or submission happens, I am able to execute the PHP script by verifying if $_POST (fallback method) or $_GET (javascript method) had been set, in this case I'm using $_REQUEST so I did not repeat the code twice. By using the $_GET method we're able to execute a PHP function, but there was a page reload.
This is why learning AJAX would be better, as you can eliminate the page reload, and can send data via POST so that it's not going to be displayed in the address bar.
No matter what data it is, your own or someone elses, you must treat it as unsafe. In this case, I know what values I'm expecting so I will only work with those values, if the values are going to be unknown, then you've got more work ahead of yourself to make sure that the data will be acceptable.
Cheers,
MC
__________________
#------------------------------ signature---------------------------------------------------------------------------------#
Quote:
|
I am well recognised for what I don't do than what I do. Chores are just one of those things.
|
|
|
|
|
08-25-2006, 07:25 PM
|
Re: php in javascript?
|
Posts: 186
Location: Hollywood, CA
|
Again it isn't impossible!! all you need is HIDDEN fields and generate your last string off of the hidden fields by adding them in the order that you want them and storing them in another hidden field you will use the onOnFocus() or onBlur() i think its called.... to make all the hidden fields get their values everytime something new is inputed.
|
|
|
|
08-27-2006, 02:30 PM
|
Re: php in javascript?
|
Posts: 18
|
yes, the technology here is AJAX. i have an ebook on it, although itd probably be illegal to share. you could do the same thing without ajax, but with page refreshes and so on. you can hide variables by using cURL or using javascript to write to a POST form and auto-submit
|
|
|
|
|
« Reply to php in javascript?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|