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
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;
}

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.