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

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.