Jump to content

Javascript form HREF with PHP


cjbeck71081

Recommended Posts

Right now i am modifying an existing code that works to use a CART_ID to query a database storing temporary information about a users Shopping Cart to populate the Shopping Cart Page.  What i would like to do is add a small bit of data into the end of that URL that uses information a user fills out on a product page to pass data.  Here goes:

 

There is a Product Details Page.  It has an "Add to Cart" button.  It takes a piece of code called the "Cart_URL" and passes it through the button in a peice of code that looks like this:

 

<input type="button" name="btnAddToCart" value="Add To Cart >" onclick="window.location.href='<?php echo $cart_url ?>';" />

 

I realise i could probably try to rewrite this code to  use the "OnChange" method of Javascript to populate a PHP Variable that effectively passes the value of a drop down box called "Flavor" to the URL.  I could then extract the Flavor from the URL on the Shopping Cart Page.  The problem i am having is how to get Javascript to pass the value of the selected code from the drop down to a PHP variable

 

I figured something like this:

 

$cart_flavor = "Cherry";  (I would set the variable to the default flavor);

 

<script>

 

$cart_flavor = "form1.select.selected.value"; (This would change the variable of $cart_flavor to the selected value on change, if there was no selection the default value "Cherry" would stay the same)

 

</script>

    <select onChange="runCode();"  name="select">

      <option value="Chocolate" selected="selected">TestItem1</option>

      <option value="Cherry">TestItem2</option>

      <option value="Strawberry">TestItem3</option>

    </select>

 

<input type="button" name="btnAddToCart" value="Add To Cart >" onclick="window.location.href='<?php echo $cart_url $cart_flavor ?>';" />

 

I realise however that my syntaxt is off, but i think my methodology might work.  Any suggestions would be greatly appreciated.

 

Thanks

Chris

Link to comment
https://forums.phpfreaks.com/topic/72370-javascript-form-href-with-php/
Share on other sites

The problem i am having is how to get Javascript to pass the value of the selected code from the drop down to a PHP variable

by just reading that I believe you mean you want a javascript var to php var am i right? If this is so you can't do that directly javascript runs in your browser and php on a remote server

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.