I'm in the midst of creating a website that includes shopping cart functionality and have run into an issue with passing variables back-and-forth from HTML/Javascript to PHP. I understand that these languages are fundamentally different and was hoping someone could provide some guidance. I've seen several questions on similar topics, but unfortunately have yet to find a solution that works for my situation.

I have created a multidimensional array of products in php and would like to capture the value from a dropdown menu to call a function in which the value of the dropdown corresponds to a row in the product array. My list of products appear in a HTML table. I have experimented with $_GET and $_POST, but haven't had any luck. Plus I would like to avoid adding a submit button as the print_wp_cart_button_for_product function outputs an add to cart button. The print_wp_cart_button_for_product also creates the shopping cart on the sidebar.

<TD>
  <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
  <select id="productcat1" name="productcat1">
    <option value="$">--Please Select--</option>
    <option value="1">Product # 1 - $1.99</option>
    <option value="2">Product # 2 - $1.99</option>
    <option value="3">Product # 3 - $9.99</option>
    <option value="4">Product # 4 - $9.99</option>
  </select>
  </form>
</TD>
<TD>    
  <?php $currentrow = 0; ?>
  <?php $currentrow = $_GET["productcat1"]; ?>
  <?php echo print_wp_cart_button_for_product($products[$currentrow]["Product"], $products[$currentrow]["Price"]); ?>
</TD>       
share|improve this question
If you want to respond to the selection on the client, you can't use PHP, but must add some Javascript/JQuery event handler functions. – Olaf Dietsche Dec 26 '12 at 18:45
php has nothing with browser ... you need ajax for this – NullPointer Dec 26 '12 at 19:04
feedback

1 Answer

up vote 0 down vote accepted

Since PHP is a server side language, it has no way of knowing what is happening in the client (i.e. what is happening live in the users browser, such as which dropdown they have selected). You will have to use javascript/ajax, either to run your function entirely, or to communicate the selected option back to the server to run the PHP function. Alternatively, you can communicate with the server without javascript/ajax by submitting the form, but you said you don't want to do that.

Good luck!

share|improve this answer
Thanks for the responses. Was hoping that there was a solution that I was overlooking, but that doesn't appear to be the case. Time to learn some Ajax! – panoramic Dec 26 '12 at 21:55
@pan Good luck! As a new user, if you feel that my answer is complete (albeit a disappointing reality), I'd be grateful if you'd vote up or accept my answer. I'm still trying to gain basic site functionality, so I need every rep point I can get! – Noah Dyer Dec 27 '12 at 0:22
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.