Jump to content

[SOLVED] php/javascript help please


realjumper

Recommended Posts

Hi,

 

I hope someone can shed some light on this problem....it seems a simple task but I just can't figure it out. Here's my code:

 


<script type="text/javascript">
function showSelected(val){
       document.getElementById
       ('selectedResult').innerHTML = "" 
       + val;
}
</script>

<?php

$query12 = mysql_query("Select id,ringi_num,ringi_count FROM ringi_details WHERE sid = $sid")

echo "<select name='test' onChange='showSelected(this.value)'>";
echo "<option value=''>Ringi Not Required</option>";
           while ($r = mysql_fetch_array($query))
           {
           $ringi_num = $r["ringi_num"];
           $ringi_count = $r["ringi_count"];
           $ringi_num = "$ringi_num $ringi_count";
           $id = $r["id"];
           echo "<option value='$id'>$ringi_num</option>";
           }
           echo "</select>";

echo "<div id='selectedResult'></div>";
?>

 

Now that all works just fine....when I echo '<div id='selectedResult'></div>' it returns the value of the selected item in the drop-down....perfect!

 

But, how do I get the value into a variable?

 

For example, when I select an option from the drop-down it might return (say) 267 when I echo '<div id='selectedResult'></div>'. What I want is to have the returned value available to use as a variable in another query. In laymans terms I want:

 

$number = '<div id='selectedResult'></div>';     // where '<div id='selectedResult'></div>' = (say) 267

 

If that is possible, then I can use $number in the WHERE condition of my next query.

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/
Share on other sites

You could make your ajax functions by yourself, but try using a javascript library like jquery.

Other libraries include (google) mootools / prototype

Once you download/include the jquery library do something like this:

<script>
function myFunction(id){
 $.get("page.php", { drink: id },
 function(data){
   alert("Data Loaded: " + data);
 });

}
</script>
<option onchange="myFunction(this.value)">
<select value="1">water</option>
<select value="2">soda</option>
<select value="3">beer</option>
</option>
<?
#page.php
switch($_GET['drink']){
 case 1:
  echo "water is okay";
 break;
 case 2:
  echo "yay for soda";
 break;
 case 3:
  echo "nice";
 break;
}
?>

This wasn't tested, but should give you the idea.

Okay....over the weekend I had a play with Ajax and in the end I got a 'test' page to do exactly what I was seeking. I found 'prototype.js' worked really well, so now I will apply this to my original problem. If anyone is interested I can post the completed code....once I have completed it!! Thanks to those who helped :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.