Jump to content

[SOLVED] Ajax / PHP switch


pets2soul

Recommended Posts

HELLO all,

 

I'm taking my baby step toward ajax interaction with PHP, this is my first experiment, and it's driving me crazy! What I'm trying to do here is...I have a html form with a "product" field, which is a drop down list of products, by selecting the product, I want it to go through a php to grab the price of the product from the database, and then to throw that price back to the "price" text field on the html page. Below is my ajax and php code...it'd be really appreciated if anyone would give me some guidance!!!

 

Ajax

<script language="javascript" type="text/javascript">
<!--
// Get the HTTP Object

function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}

// Change the value of output field
function setOutput(){
if(httpObject.readyState == 4){
document.getElementById('price').value = httpObject.responseText;
}

}

// Implement business logic
function doWork(){
httpObject = getHTTPObject();
if (httpObject != null) {
httpObject.open("GET", "test1.php?price="+document.getElementById('price').value, true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}

var httpObject = null;

//-->
</script>

 

PHP

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($result))
{

switch ($_GET['product']) {
case "Product1":
echo $_GET['price'] = $row['product_price1'];
break;
case "Product2":
echo $_GET['price'] = $row['product_price2'];
break;
case "Product3":
echo $_GET['price'] = $row['product_price3'];
break;
}

}
?>

Link to comment
https://forums.phpfreaks.com/topic/181212-solved-ajax-php-switch/
Share on other sites

Hi mrMarcus,

 

Thanks for the reply, I'm listing the HTML, AJAX and PHP below:

 

HTML FORM

<form name="theform">
<table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100" height="24"><font class="t2">Product:</font></td>
<td width="100"><font class="t2">Price:</font></td>
</tr>
<tr>
<td height="24"><select name="product" id="product" onclick="doWork();" style="width: 85px; border: 1px solid #CCCCCC;">
<option value="Product1">Product1</option>
<option value="Product2">Product2</option>
<option value="Product3">Product3</option></select></td>
<td><input name="price" id="price" type="text" style="width: 85px; border: 1px solid #CCCCCC;" /></td>
</tr>
</table>
</form>

 

AJAX

<script language="javascript" type="text/javascript">
<!--
// Get the HTTP Object

function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}

// Change the value of output field
function setOutput(){
if(httpObject.readyState == 4){
document.getElementById('price').value = httpObject.responseText;
}

}

// Implement business logic
function doWork(){
httpObject = getHTTPObject();
if (httpObject != null) {
httpObject.open("GET", "test1.php?price="+document.getElementById('price').value, true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}

var httpObject = null;

//-->
</script>

 

PHP

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($result))
{

switch ($_GET['product']) {
case "Product1":
echo $_GET['price'] = $row['product_price1'];
break;
case "Product2":
echo $_GET['price'] = $row['product_price2'];
break;
case "Product3":
echo $_GET['price'] = $row['product_price3'];
break;
}

}
?>

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.