Forum Moderators: coopster
ID :
Description :
Price :
what i want this code do is, if the user enters the ID and click the display button, the description and price details appears without reloading the page.
I've already load the table and store them temporary in the arrays. So i think what i need to do now is to Retrieve those Data.
My friend says that, i need javascript to do this, but unfortunately, i dont have any knowledge about java script, plus im also a newbie in php =(
can someone kindly solve my problem =(
Thank you for your help guys =)
<html>
<body>
<?
include ("databaseconfig.php");
$query="SELECT * FROM bahan ";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
////Stores the records in an Array
$i=0;
$x=0;
while ($i < $num)
{
$ID[$x]=mysql_result($result,$i,"ID");
$Deskripsi[$x]=mysql_result($result,$i,"Deskripsi");
$Harga[$x]=mysql_result($result,$i,"Harga");
$i++;
$x++;
}
$a = "";/// <--- The Value of a is based on the user input.. but i have no idea how to do that.. is using javascript possible?
echo"
<form name='form1' method='post' action=''>
<p>ID
<input type='text' name='ID' value ='$ID[$a]'>
</p>
<p>Harga
<input type='text' name='Harga' value='$Deskripsi[$a]'>
</p>
<p>Deskripsi
<input type='text' name='Deskripsi' value='$Harga[$a]'>
</p>
<p>
<input type='button' name='Submit' value='Display' onClick='Display()'>
</p>
</form>"
?>
</body>
</html>
<script language="javascript">
<!--
function Ambil() ///<---- i got no idea about the java script....
{
var ID = document.getElementById("ID");
}
-->
what i want this code do is, if the user enters the ID and click the display button, the description and price details appears without reloading the page.
Your friend is correct. PHP runs on the server. The POST data needs to be returned to the server for processing and then sent back to the browser to be displayed.
Without a java script or AJAX solution, there will need to be a page reload.
i use the java script to send the row number...
is it possible to create this?
<script>
var string1 = <? echo $string1;?>
</script>
the thing is, with arrays, you will need to write a loop or loops that goes through the php array and the js array. so its get more complicated since you wont know the lenght of the php array and you wont be able to pass the current array location to the js array.
actually, this may work, but i havent tried it.
<script>
<?
for ($i = 0; $i < sizeof($myArray); $i++){
echo "jsArr[$i] = $myArray[$i];\n";
}
?>
</script>
i really think you're better off using ajax. its not that hard to learn. and with it, problems like this will be cake. plus do you really want people to see the data when they click on view source?
anyway
is there any way to load PHP array inside the javascript?