Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using javascript from inside PHP to get element's value 1

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi all,

I have a cart application in which the product page offers an item - with a <Select>dropdown next to it to select the size. Each option in the Select has a unique value. The Select itself as an ID.

When 'Add to Cart' is pressed, the first value is always sent. I realized, perhaps incorrectly, that there is no way to reference the value of the Select with PHP - to send along in the query string. So my next approach was to put in-line javascript to get the correct value in the name-value pair of the query string. Some code:

<A HRef='admin/update_cart.php?Item=" . document.forms[0].Elements['ClaryPrice . $item_count . '].value . "&redirect=sensual.php'><img src='home/cart.bmp' alt='add to cart'/></A>

I am expecting a string like this:

admin/cart?Item=45&redirect=sensual.php

The 45 is the value from the Select. Say there are three option value 45, 46, 47. A person select the third option (47) but the string also returned 45 - that was when I just used PHP.

Now I have javascript or at least DOM -- document.forms[0].Elements['ClaryPrice . $item_count . '].value

ClaryPrice3 might be the name of the Select. There are many select and the $item_count makes them unique (ClaryPrice1, ClaryPrice2, etc.)

Anyway I can't seem to get the JS/Dom reference to work right. Some possibilities:

Some of it belongs hardcoded, some of it belongs referenced (I have tried variation with no luck yet)

Perhaps inline JS can't be mixed in PHP? (although I can't see why not)

Perhaps there is a PHP way that I am not aware of to get a select's "real" value, not just its first one.

The redirect is used to return back to the page, sensual.php that the items are listed on. The update_cart.php writes the cart number and the item value to a database then redirects back to the page. And please don't get tiffed at sensual - it is a line of perfume. :)

All help appreciated!

Thanks,
KB


 
that there is no way to reference the value of the Select with PHP - to send along in the query string

php cannot interact with a client side application once the page has finished loading (without the aid of some intermediary like javascript or flash). php is a *server-side* language whereas javascript is a *client-side* language.

but what you are describing above is plain html. nothing to do with js or php. Consider

Code:
<form method="get" action="sompage.php">
<select name="selectbox">
<option value="apple">apple</option>
<option value="banana">banana</option>
</select>
<input type="submit" />
</form>

in the above example, the value of the select box is sent in the query string and is available to php in the $_GET superglobal. the url will look like

Code:
.../somepage.php?selectbox=apple

as to the rest of your question: it seems irrelevant given the misunderstanding about the nature of php and html.

However:
1. for questions about javascript please address the javascript forum. addressing nodes of the DOM is something they can help with
2. php can very happily send javascript to the browser. You need to be careful about enquoting. Consider

Code:
<?php
echo <<<HTML
<html>
<head>
<script>
function alerter(str){
 alert(str);
}
</script>
</head>

<body onload="alerter('hello world');">
<div>Lorem ipsum</div>
</body>
</html>
HTML;
?>
 
Hi jpadie -

Can't get it work yet. Per your suggestion, just HTML.

Here are two versions of the PHP that render HTML for the action...

<A HRef='admin/update_cart.php?Item=" . ClaryPrice . $item_count . &redirect=sensual.php'><img src='home/cart.bmp' alt='add to cart'/></A>


<A HRef='admin/update_cart.php?Item=ClaryPrice" . $item_count . "&redirect=sensual.php'><img src='home/cart.bmp' alt='add to cart'/></A>

The Select is named "ClaryPrice" and since I have multiple products on the page - each with its own select, I use an incremental variable - $item_count - to make each Select unique. Thus there are Selects such as ClaryPrice1, ClaryPric2, ClaryPric3, etc.

Each Select has two options, like this:

<Select Name='ClaryPrice3'>
<Option Value='56'>500ml $18</Option>
<Option Value='57'>1L $30</Option>
</Select>

This is the code of update_cart.php:

<?php
$connection=mysql_connect('xxx.x.x.x', 'xxxxxx', 'xxxxxx') or die ('unable to connect');
mysql_select_db("xxxxxxxx", $connection);
session_start();
if(!isset($_SESSION['cart'])){
$ssql="Select Max(CartID) from tblCarts";
$result=mysql_query($ssql,$connection) or die ('unable to get maxcart');
$row=mysql_fetch_row($result);
$row[0]=$row[0]+1;
$_SESSION['cart']=$row[0];
}
$ssql="Insert Into tblCarts (CartID, ItemID) Values (" . $_SESSION['cart'] . ", " . $_GET['Item'] . ")";
echo $ssql;
//$result=mysql_query($ssql,$connection) or die ('unable to insert item into cart');
//$redirect="Location: . $_GET['redirect'];
//echo header($redirect);
?>

Normally those last three lines are not commented out. That was so I could see the SQL command that is assembled, and here is what I get:

Insert Into tblCarts (CartID, ItemID) Values (61, ClaryPrice3)

So instead of getting the "value" of ClaryPrice3 (56 or 57 depending on the dropdown selection), I just get the name of the Select.

Hope this makes the issue clearer. I tried variations of how to get the value passed instead of the Select name but no luck yet. Ideas??

Thanks,
KB
 
the value of claryprice3 is stored in the variable $_GET['ClaryPrice3'] and not $_GET['Item']. you can tell this because ClaryPrice3 is the name of the select control.


this line
Code:
//echo header($redirect);

won't work, even uncommented. delete the echo.
 
jpadie -

OK I use $_GET['ClaryPrice3']

However the passed Select name is not known ahead of time. It could be ClaryPrice1, ClaryPrice2, ClaryPrice3, ClaryPrice4, etc.

I could just code to test for up to 30 or so possibilites (which would likely be greater than the number of products).

But much better would be if there is a way for the $_GET to be coded such as to be generic. I'll test this but off the top of your head do you think $_GET[0] (or GET[1] would work?

Or use a wild card like: $_GET['ClaryPrice*'] or perhaps it would be $_GET["'ClaryPrice" . "*" . "'"]

???

THanks!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top