Running a php/sql query from javascript...is it possible?

Hey, so I have a form and I need it to do this:

When A select box is changed, it will run a query based on what it is changed too. Im not quite sure how to do it tho...as I have it right now:

<script type="text/javascript">

function ProjectUpdate(){

<?php

$ProjectID=$_POST['ProjectID'];


$queryProjects = "SELECT * FROM `pdem_projects`.`tblProjects` WHERE ProjectID = '$ProjectID'";
$resultProjects = mysql_query($queryProjects);

$projArray = mysql_fetch_assoc($resultProjects);

?>

document.form1.ProjectName.value = "<?php echo $projArray['ProjectName'];?>"
document.form1.Client.value = "<?php echo $projArray['Client'];?>"

}

</script>

Hopefully that will give you an idea of what I am trying to do..I think it is fairly straight forward...Its just, when the javascript function runs, it just skips right over the php code.

Hope there is a solution
thanks
 
I did something similar to what you're trying to do I think, I ended up using an iframe which had it's contents changed by using the OnChange event of the select item to call a javascript function that changed the source of the iframe. Yeah, so if you can avoid it, it's best to do so.
 
No that won't work.

You're trying to run server-side code parallel to client-side code.

What you're going to have to do is read into AJAX. There's probably a few tutorials if you google around :)
 
You can't do what you're trying to do without a page refresh, that is without using AJAX. However, if you wanted to do this with a page refresh I suppose you could...

<script type="text/javacript" src="scripts.php"></script>

Notice the ".php" file extension...

As long as the php script is returning JavaScript code, I see no reason why this wouldn't work.

Here's a little example:
--
<?php
// You may need to add a content-type header:
header("Content-type: application/x-javascript");
// This must be done before the script outputs any data.

$output_string="Hello There<br/>";
echo "document.write('" . $output_string . "');";
?>
--
I suppose you can also return images, content, mysql data, and other stuff that you might do in PHP in this way.

I honestly don't think this is practical, but I was thinking outside of the box.
 
Back
Top