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>
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!
Related
Before anyone flames, I have looked at many more than just the following 3 posts. None of these have working answers.
How to Post data from a php database populated Drop Down to another script?
Passing value from a dropdown list from one php to another
What is the best way to have a dynamic dropdownlist in PHP and POST selected value
These simply just do. not. work.
Now on to my question:
I have a dropdown list on my php page that is being populated by a MySQL query. This part works perfectly. However -- I can not get the selected value from the dropdown list to post in to my next PHP script.
My code on "page1.php":
Please select your dietary preference:
<form method="post" action="page2.php">
<tr>
<td>
<select name="dropdown_value">
<?php foreach ($data as $row): ?>
<option><?=$row["dietary_option"]?></option>
<?php endforeach ?>
</select>
</td>
</tr>
</form>
This successfully populates my dropdown list with the available dietary options pulled from my database.
And on page2.php:
$mealSelection = $_POST["dropdown_value"];
Simply does nothing. It yields no value(?). Echoing $mealSelection seemingly echoes nothing. It is blank.
I would ultimately like to use $mealSelection in a new query on page2.php but I can't seem to retrieve the selected value from page1.
Any wizards with some insight?
Thanks for taking the time to look at my issue here.
LC-Data
Edit: None of the answers for simply posting dropdown options from predefined HTML work. I specifically need the dropdown to be populated from my db.
Use it like this:
<?php foreach ($data as $row): ?>
<option value="<?= $row['dietary_option'] ?>"><?=$row["dietary_option"]?></option>
<?php endforeach ?>
You haven't given value field in your options. Without value field, it won't work.
I am working on customizing a plugin, however my knowledge is currently lacking on the php side.
currently i have an Ad plugin in which you can sell ads per day, however my predicament is that i want to sell the ads only in packs of 30,60 & 90 days.
In this plugin upon typing a number of days this plugin calculates the amount and gives a result right under without anything pressed, so it is actively reading an input; however the only problem with this plugin is that it allows anyone to type a random number in there.
now the only real solution that i could think of was to make a drop-down with pre-existing values 30, 60, 90
My attempts thus far have been futile as i keep applying HTML <Select> (yes i had changed the wording from <input to <select and then added <option value="value">value wording</option>)techniques and it seems that anytime that i attempt to put a number for the value as one of the choices i get nothing but an empty drop-down, this then made sense to me since this is a PHP form and not an HTML form.
I was able to find where to code the changes, as i was able to find where the field is pulling from; here is the code:
(How should i proceed to make the changes that i want happen?)
<div class="control-group" id="total_days" style="<?php echo $date_dis;?>">
<label class="ad-price-lable control-label" width="40%"><div id="total_days_label"><?php JText::_("TOTAL_DAYS_FOR_RENEWAL");?>:</div> </label>
<div class="controls">
<input type="text" maxlength="5" name="totaldays" class="ad-pricing" id="totaldays" value="<?php echo $ad_totaldays ;?>" onchange="caltotal()" >
<input type="hidden" name="ad_totaldays" id="ad_totaldays" value="<?php echo $ad_totaldays;?>" />
</div>
</div>
now i am also assuming that out of this area, the main thing to be changed would be:
<input type="text" maxlength="5" name="totaldays" class="ad-pricing" id="totaldays" value="<?php echo $ad_totaldays ;?>" onchange="caltotal()" >
of course, i could be wrong, but i really would appreciate help with this!
thank you very much!
Here is how I've always done dropdowns
<select form="examplef">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<form id="examplef">
<input type="submit">
</form>
The php you're looking at is setting a value field for the inputs. This just means that something will be in the box already when you load the page.
Also keep in mind that just because you have predefined options doesn't mean you wont get unexpected input; Changing the options is as easy as right clicking
PHP form vs HTML form
They are the same. The html <form> tag is a way to communicate with php. This tag will (most likely) have an attribute called "action" and "method". The action attribute specifies what to do with the collected information. You can think of it almost as an <a href> that includes data.
The method attribute is a little more complicated. It tells your browser how to send the data entered into the form. The two options are "GET" and "POST". the GET method is data put in the url. I'm sure you've seen youtube links with the ?id=. This is just a GET variable included in the URL. The POST method is for secure data, or data that shouldnt be in the URL. If you would like to read more, W3Schools has a great document here or from Php here and here
I have a search website.
I want the search bar form from my home page to be visible on the results page.
What solutions are there for pre-loading all the various elements of my form?
There's 3 text fields, 2 radio buttons, and two checkboxes.
Currently I'm having to interrupt each element with a php function that will see if the $_GET['valueName'] is equal to current element option, but that seems too complex and ugly to be standard.
parallel example:
<select name="section">
<option value="free" selected="<? if($_GET['section'] == 'free'){echo 'selected';} ?>">Free</option>
<option value="under100" selected="<? if($_GET['section'] == 'under100'){echo 'selected';} ?>">Under $100</option>
<option value="under200" selected="<? if($_GET['section'] == 'under200'){echo 'selected';} ?>">Under $200</option>
<option value="any" selected="<? if($_GET['section'] == 'any'){echo 'selected';} ?>">Any price</option>
</select>
I haven't ran that to know it even works, but is there not a client-side way of populating a form?
Something like:
<input type="text" name="search" value="<?php if(isset($_POST['your_var']){ echo htmlspecialchars($_POST['your_var']);}?>">
Then for check boxes/radio you'd do a loop I believe, but I can't remember right of the top of my head.
If you want variables from a form passed to another page and shown on that page you could do something like:
<form action="new_page.php" method="POST">
<input type="text" name="search_phrase">
</form>
Then on your new_page you can load them and display them:
<?php
if(isset($_POST['search_phrase'])){ echo $_POST['search_phrase']; }
?>
This is probably the most simple way to load form data onto your new page; if you don't want to use frames, divs, or AJAX. I would have gave a more specific answer but you didn't give example code; but either way this method should work just fine.
If you want the entire search bar itself included in both pages you could put the search bar in a different php file and just import it with:
include('my_search_bar.php');
And if you think the $_GET is ugly and interrupting you can use POST (it does not put variables in your link like link.php?var=abc... Also if you check if your post variables are set at the very top of your page you can make it so all those are required before the functions will execute and display variables making the functions seem more etiquette flowing.
I have been searching for an answer to this for the last two days.
I am trying to add a list of manufacturers to an existing form in php.
This worked out ok and I got it to work somewhat.
Here is the problem when the user makes the selection and submits the form
it takes them to another page that shows them a preview of their post.
In this preview it show them the manufacturer they selected. But when they finalize the the
post it doesn't show using the same code.
I know that their must be a simple way of doing this. I am not great with php but I have put forth a valiant effort to get this done. I am stumped please help.
Here is my code.
HTML:
<select action="select.php" name="manufacturer">
<option value="Lincoln">Lincoln</option>
<option value="Example">Example</option>
</select>
PHP:
<?php
if(isset($_POST['manufactuer']))
echo "Manufacturer: ".$_POST['manufacturer'];
else {?>
I call to the selection with this.
<?php echo "Manufacturer: ".$_POST['manufacturer']; ?>
How do I show this when the user posts and if the same user posts again or a different user posts to show their selections for those specific posts.
posts
You can pass your variable on from your "review" page to your next page with a hidden field.
<form><input type='hidden' value='<?php echo $_POST['manufacturer']; ?>' name='manufacturer' /></form>
Just set the selected="selected" option in the selected select with php. That makes:
<option value="Lincoln" selected="selected">Lincoln</option>
i am trying to implement a kohana sorting filter for a virtual store, meaning that whenever i want to sort some products (after price, etc)i must only select the sorting criteria from a list. i dont want to implement the sorting in another view, so that when one chooses a sort option, he must not be redirected in another page.
so i have a list:
<form name="ordering" id="ordering" method="post" action="">
<input type="hidden" id="ordering" value="0">
<select id="ordering" name="ordering">
<option value=0>All products</option>
<option value=1>Ascending Price</option>
<option value=2>Descending price</option>
</select>
</form>
i want then to take that hidden value in the controller, for being able to manipulate it in the view. (is it correct?)
i need this variable in order to be able to make a switch and to determine which sorting option has been choosen by a user.
in the controller, i try to 'catch' the variable with a $ordering = $_POST['ordering']; but i receive an error, or with a
if (Request::$is_post){
$ordering = $_POST['ordering'];
}
but it never gets there (at that bunch of code).
where i am wrong?
thank you!
Given the documentation of Request object and this example, you could try the following in your controller :
if (Request::$method == 'POST') {
$ordering = $_POST['ordering'];
// ...
}
Note: it's recommended to put quotes around HTML tag parameters
<option value="0">All products</option>
instead of
<option value=0>All products</option>
HTH
#dana: Have a look at -- http://www.ajaxlines.com/ajax/stuff/article/jquery_and_kohana_unobtrusive_ajax.php -- for an example and some direction on how to do what you wish to without having the page refresh when a user submits the form.
First, check your HTML code, you have 3 IDs named "ordering".
I don't know how you wrote your Javascript stuff, but I wouldn't be surprised if that doesn't work, IDs should be unique.
Anyway, you say you want to get the value of the hidden field, but it doesn't have any name. So you can't access to it via PHP (unless you do it with Javascript)
According to Kohana 3.1 (the last comment was linking the 3.0 documentation), to get the value you should do the following:
$this->request->post('ordering');
What version of Kohana are you working with ?