here is my problem. I have an xml file that acts like a small database. My php code goes through the xml file and retrieves the info creating a form. For example, the xml file holds info about a menu, the user chooses salad, then the code will print out all types of salads and the user then chooses his favourite salad. The actual problem is that there are different portions; small and large and different prices for each!
I am not able to keep track of the selected item... X(
Any help??
Thanks in advance
here is my code:
<?php
foreach ($xml-> CATEGORY as $category)
{
if($_GET['category']==$category["id"])
{
echo "<h3>".$category ["id"]."</h3>";
echo "<br />";
echo "<form method=\"post\" action=\"add.php\">";
foreach ($category -> FOOD as $food) {
echo "<li>";
echo "<b>".$food["name"]."</b>";
echo "<hr>";
echo "</li>";
echo "<select name=\"variations[]\">";
foreach($food -> PRICE as $price)
{
echo "<option value=\"$price\">";
echo "<ul>";
echo $price["size"]. " ";
echo $price;
echo "</ul>";
echo "</option>";
}
echo "</select>";
echo "<input type=\"submit\" value=\"Buy this\">";
}
echo "<br />";
echo "<br />";
}
echo "</form>";
}
?>
the cart looks something like this
<?php
echo "<br>";
$values = $_POST['variations'];
foreach($values as $value)
{
echo $value;
echo "<br>";
}
?>
the code above only shows prices about small items. Large items are somehow ignored!! This code is not actually what I meant to do but it's an start. It should say:
You have chosen a large portion of greek salad for £10.50.
So, I should be able to pass three variables that I don't know how: $price[size], food and $price...
Thanks for your time
if i understand you correctly, you are just recieving the selected size (price) but not the actual meal?
well two possible solutions:
version one:
create a second form field that holds the selected meal.
i think its elegant and logical if you have checkboxes for the meals and then radio buttons or lists for the size... so you can check/uncheck meals but if its check you need to select a size...
you can also just let the user select the price and dynamlically select the the meal field too with javascript but i wouldnt suggest that, if you want this kind of user experience, i'd opt for vesion two
version two:
include the meal in the value of the input field that for the price. maby seperated by a comma or as a json encoded string.
then when you retrieve the post variable you can split/decode...
if you give me a working example which i can just paste into a file and hit it (including the xml object) i can quickly do that for you...
Related
In this code, I'm trying to take a user that wants to change event statuses from a dropdown. The dropdown consists of three options:
"Contacted"
"Verified"
"Completed"
I want a user to click "Submit status" and take them to change.php, as specified in the action attribute. Change.php should have 2 get parameters: the id of the database column of the event they want to alter, and the new status they want to change it to. For example it should look like this: http://localhost/schedule/change.php?id=60?status=verified
But this is what I'm getting right now:
http://localhost/schedule/change.php?status=verified&submitStatus=Change+status
And also, the dropdown is showing an error: Notice: Undefined index: status in line 1
// Assume this is line 1
echo "<form method='get' action='change.php?id=".urlencode($e['id'])."?url=".urlencode($_GET['status'])."'><select name='status' id='status'>'";
if ($e['status'] == "contacted") {
echo "<option value='contacted' selected>Contacted</option>";
echo "<option value='verified'>Verified Appointment</option>";
echo "<option value='completed'>Job Completed</option>";
$counter++;
} else if ($e['status'] == "verified") {
echo "<option value='verified' selected>Verified Appointment</option>";
echo "<option value='contacted'>Contacted</option>";
echo "<option value='completed'>Job Completed</option>";
$counter++;
} else if ($e['status'] == "completed") {
echo "<option value='completed' selected>Job Completed</option>";
echo "<option value='contacted'>Contacted</option>";
echo "<option value='verified' selected>Verified Appointment</option>";
$counter++;
} else {
echo "No event status found!";
}
if ($counter == 1) {
echo "<input type='submit' name='submitStatus' value='Change status'></form></select>
</td>";
echo "</tr>";
}
I know the real issue lies in the first echo line, where I start the form tag, and I know the issue is in the action parameter, but I just don't know what to do! Please help me out here...
The error is that you are using ? to separate parameters instead of using & or &&. So your code should be:
echo "<form method='get' action='change.php?id=".urlencode($e['id'])."&url=".urlencode($_GET['status'])."'><select name='status' id='status'>'";
Also, as said by Phil, try using POST if you don't want to change the data values from url. For example, if you change, id from url, change.php will make changes on the basis of id parameter from url. It seems to me like you should use POST as change to me means quite secure thing and not the thing to be done via GET.
Obviously, it does not mean you should not use GET. These methods are equally important, but not as secure in the sense that data can be changed from url so easily. GET can be used in search process, for example where you don't have to change to the database, you just have to retrive.
I might have gone off topic, but I think that was quite important for you to know which method you should be using. Thank you!!!
I have a form that has several input types all that need to be a handled differently. I did a loop to name them like so in the form:
product-0-length
product-0-size
product-1-length
product-1-size
product-2-length
product-2-size
On my processing php (where the form info gets sent) I want to iterate a loop to handle say size different from length. My thought was this but no luck:
<?php
$i = 0;
foreach($_REQUEST['product-'.$i.'-length'] as $key => $val) {
//style or do what I need with the length information for each product
echo '<li>'.$key.'='.$val .'</li>';
$i++;
}
?>
As I suggested in the comments above you could use a different naming scheme for your input fields. Take a look at this example for the form creation:
<?php
foreach ($i=0; $i<3; $i++) {
echo "<input type=\"text\" name=\"product[$i][length]\">\n";
echo "<input type=\"text\" name=\"product[$i][size]\">\n";
}
When submitting this form the server will translate the notation into a php array which is a very convenient thing. You can simply iterate over that array:
<?php
foreach ($_POST['product'] as $key=>$product) {
echo "<li>Length of product $key: " . $product['length'] . "</li>\n";
echo "<li>Size of product $key: " . $product['size'] . "</li>\n";
}
Note that this is a very primitive example, all error checking and the like is missing to keep things crunch. Also I did not test this code here, it is just meant to point you into the right direction, but I hope there are not too many typos in there...
I'm working on this code in PHP and basically I have 5 checkboxes, for 5 individual items, each called "ItemCheck" and they have values of 0-4. Now I wrote a code where it has it so it displays the numbers that are checked from those 5 check lists.
Form:
for ($i=0;$i<count(5);$i++){
echo "
<input type='checkbox' name='ItemCheck' value='$i.check'>$i</input><br>"}
PHP Process:
if (isset($_POST['ItemCheck'])){
for ($o=0;$o<$ItemCount;$o++){
if($_POST['ItemCheck'] == $o.'.check') {
echo "Item " . $o . "<br>";
}
}
}
else{ echo "You must select at least one product";}
Although say if I check box #1,2 & 3, the final output will only display "Item 3". Now matter how many checkboxes you select, it will only display the one with the highest value and none other. Does anybody know what's wrong with the code and how to have it so it displays each individual number that's selected, and not just the one with the highest value?
What you want to do is set the name of the checkbox element ItemCheck[] this will make $_POST["ItemCheck"] an array.
Example:
for ($i=0;$i<count(5);$i++){
echo "<input type='checkbox' name='ItemCheck[]' value='$i.check'/> $i<br>";
}
Another thing to note is the browser won't post up anything for an unchecked checkbox so I think your processor needs to be like this.
<?php
if (isset($_POST['ItemCheck'])){
for ($o=0;$o<count($_POST['ItemCheck']);$o++){
echo "Item " . $_POST['ItemCheck'] . "<br>";
}
} else {
echo "You must select at least one product";
}
?>
Another thing so make sure of is that your form has the method set to POST
it is only displaying the first select box and the last one ..
here is the code.
function select_nom_of_guests($guest_type){
$i=0;
echo $guest_type;
echo "<select name=\"adults_num\" id= \"a\">";
while ($i<=5){
echo "<option value= $i>$i</option>" ;
$i++;
}
}
echo "<p>";
select_nom_of_guests("מספר מבוגרים");
select_nom_of_guests("מספר ילדים");
select_nom_of_guests("מספר תינוקות");
echo "</p>";
Close your <select> tags and it should work better ;-)
Note that a for loop would be more appropriate in your case.
Note that you don't end the <select> tag. I'm not sure how browsers would respond to that, but it definitely wouldn't help.
One helpful tool in these scenarios is the View Source tool that all major browsers have: instead of being confused about what's appearing on the screen, look at the HTML that the browser received to see why it might be showing what it's showing. If this is the issue, the source code would have revealed it lickity-split :)
You didn't close the select tag. You probably also want to make the name and id attributes different for each select.
function select_nom_of_guests($guest_type){
$i=0;
echo $guest_type;
echo "<select name=\"adults_num".$guest_type."\" id= \"select_".$guestType."\">";
while ($i<=5){
echo "<option value= $i>$i</option>" ;
$i++;
}
echo "</select>";
}
Thank you so much for being so helpful. Owe you all a thank you. I will be asking more questions in the future. Someone has solved the problem by giving me this code:
echo "" . strval($row['style']) . "" . "";
and it worked beautifully!!!!!!!!! You rock!
I am sorry, I don't know how to post follow up questions, so I keep posting each question as a new question. I've never joined any forum before, so don't know how to follow a thread :( Sorry
I previously asked a question, but didn't post my code, so many kind people (thank you so much) who respanded couldn't help me out.
So I'll post my partial code below.
";
echo "Select an item";
echo "";
}
while($row = mysql_fetch_array($result))
{
echo "$row[style] $row[color]";
}
mysql_close($con);
echo "";
echo "";
echo "Enter your 4 digit postcode";
echo "";
echo "";
echo "Enter quantity";
echo "";
echo "";
echo "";
echo "";
?>
Then to process form, I use $_POST['item'] to find out which item was selected, I get the first word, the rest of the words are missing.
For example, if the dropdown box was populated with the follwoing:
Dressmaker mannequin size 12
Mannequin torso PH-9 in skin color
...
if item selected was "Dressmaker manenquin size 12", $_POST['item'] gives me Dressmaker, the rests are missing.
I spent whole last night and today searching, but have made no progress, please help :(
This still applies from my previous post:
//====== Begin previous post
Hopefully, your MYSQL database has a primary key? If it does, set the value of each <option> to the primary key of the item.
For example:
SQL
id desc
1 "dressmaker thing with mannequin"
2 "dressmaker thing no mannequin"
Form PHP
echo "<option value='".$query['id']."'>".$query['desc']."</option>";
When the form is submitted, re-query the database for the desired description. You'll be doing this re-query anyway to retrieve prices and such, yes?
The reason this is happening is that spaces are discouraged in HTML attributes. You shouldn't have an attribute like value='this attribute is spaced'.
//====== End previous post
Basically, change this line:
while($row = mysql_fetch_array($result))
{
echo "<option value=$row[style]>$row[style] $row[color]</option><br />";
}
to
while($row = mysql_fetch_array($result))
{
echo "<option value='".$row['id']."'>$row['style'] $row['color']</option><br />";
}
and add this in process_form.php to get the description:
$desc = mysql_query("SELECT style FROM products WHERE id='".$_POST['item']."';");
You can also use this to get all other related info from the DB right when you need it.
// Another edit
#Cambraca - right on - I forgot to sanitize the quote.
#Ottoman - Your solution is a temporary fix. I strongly recommend applying an id/primary key system if it's not in place. An ounce of prevention is worth a pound of cure.
That is because in php you get what is in the "value" attribute of the dropdown's options.
You need to do something like this:
Replace
echo "<option value=$row[style]>$row[style] $row[color]</option><br />";
with
echo "<option value=\"$row[style] $row[color]\">$row[style] $row[color]</option><br />";
The problem is your lack of quotes in the option "echo" statement.
Try something like this
while($row = mysql_fetch_array($result))
{
printf('<option value="%s">%s %s</option>',
htmlspecialchars($row['style']),
htmlspecialchars($row['style']),
htmlspecialchars($row['color']));
}
Note also, the <br> element does not belong in the <select>
Edit: Added htmlspecialchars to properly escape any HTML entities that might exist in retrieved strings
If it gives you only the first word, then you forgot to enclose the option value="with quotes". Otherwise show us the constructed HTML source.