PDA

View Full Version : filling a php variable from a javascript function



soloWebDev
06-20-2007, 05:08 PM
I am having trouble mixing php and javascript together. What I want to happpen is when a user selects from the drop down the javascript function is run, to grab the new email id. Thanks in advance for the assistance

<?php
$selected = "";
echo '<select name="email_type" size="1" onchange="emailType(this.value)">';
echo '<option value="0"> Select...</option>';

$query = "SELECT id, subject FROM table WHERE category = 'Templated Email' ORDER BY `category` ASC";
$result = mysql_db_query($dbase,$query,$id);
$number = mysql_num_rows($result);
$i = 0;
while($i<$number):

$email_id = mysql_result($result,$i,0);
$subject = mysql_result($result,$i,1);
echo '<option $selected value='.$email_id.'>'.$subject.'</option>';

$i++;
endwhile;
?>
<script type="text/javascript">
function emailType(){
if (document.form1.email_type.value == 1)
{
<? $email_id = 1; ?>
}else{
<? $email_id ?> == document.form1.email_type.value;
}
}
</script>

alexjewell
06-20-2007, 05:53 PM
In the JavaScript, you'll want to echo PHP variables. By setting them within the JavaScript code, you aren't doing anything to the JavaScript itself. You can only do that by echoing something. Make sense?

soloWebDev
06-20-2007, 06:20 PM
<script type="text/javascript">
function emailType(){
if (document.form1.email_type.value == 0)
{
<?=$email_id = 1; ?>
}else{
<?=$email_id; ?> == document.form1.email_type.value == <?=$selected; ?>;
}
}
</script>
(above) Doesnt work
Is this what you were mentioning? If not please give example

alexjewell
06-20-2007, 06:26 PM
Ok, now what you posted there is different thing than what you originally posted. Now you're actually echoing something. I'm curious about the first one, though, where you set $email_id to 1...are you trying to echo $email_id.'=1'; or are you trying to set $email_id to 1 in the actual PHP?

soloWebDev
06-20-2007, 06:30 PM
Good question.
I am setting ($email_id = 1) if the user doesnt select from a drop down of templates

alexjewell
06-20-2007, 06:34 PM
Ok, so that is totally server side...

Hmmm, I'm not entirely sure this is possible. See, PHP doesn't read the JavaScript, it's just going to see $email_id = 1...so $email_id will always = 1. See what I'm saying? The JavaScript if/else statements are just seen as text to be echoed, not actual code to be rendered on the server. So you'll have to find some PHP if/else statement to figure out if they didn't select from a drop down of templates.

Make sense?

soloWebDev
06-20-2007, 06:54 PM
ya that is what I figured, I was just seeing if it was possible another way.

alexjewell
06-22-2007, 01:21 PM
No, because the JavaScript is client side and PHP is server side, they can't "talk" to each other. However, there is server side JavaScript, but I've never messed with it.

http://en.wikipedia.org/wiki/Server-side_JavaScript

Twey
06-22-2007, 01:49 PM
Yes, but they still have to communicate via HTTP as PHP would.

alexjewell
06-22-2007, 02:06 PM
Yes, that's true. So no, this isn't really possible.