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
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 am making a shopping cart using arrays and sessions.
I would like to update the quantity of an item that has a specific id number, without updating the other quantities which have different id number.
My code is bellow and to run it, you must get id from URL.
So just put ?id=1 in front of the .php in the address bar (as I am sure everyone is aware of more than me).
Cart.php
<?php
session_start();
// set the array and store in session
$_SESSION['items'][]=array(
'id'=>"'".$_GET['id']."'",
'qty'=> 1
);
// Display each array value stored in the session
foreach ($_SESSION['items'] as $value) {
// Display the id
echo 'The ID is ' ."''''" . $value['id'] ."''''" ;
// Display the Qty
echo 'the QTY ' ."''''" . $value['qty'] ."''''" ;
echo "<br/>";
//Echo a form that displays the Qty in a input box,
// My problem is that, I cant update the qty value in the input box where each id is different.
echo "<form method='post' action='id.php' name='edit'>
<p>Quantity: <input type=\"text\" size=\"20\" name='".$value['id']."' value='".$value['qty']."'/> </p><br>
<input class='save' name='update' value='update' type='submit'>
</form>";
}
// check if the update button is submited so that the qty value can be changed where the id number of the selected qty input box is edited
if (isset($_POST["update"])) {
$_SESSION['item'][$id]= $_POST["'".$value['id']."'"];
}
else
{
echo "quantity is updated";
}
?>
Unless you are making a $_SESSION['items'] Array of Arrays, change
$_SESSION['items'][]
to
$_SESSION['items']
Otherwise, you'll need a loop within your loop.
Right now, $value in
foreach ($_SESSION['items'] as $value) {
really represents an Array, which has your other Array in it. Additionally, you should not have more than one form with the same name. Don't put a form in a loop unless you're changing the name at each step of the loop.
I am building a Order form for a Client's website. One of the pages lets users choose Vehicle Spareparts by entering the quantity beside each part. I have achieved this by writing the following code
<input class="qty" type="number" name="oil-pump" min="0" max="100">
The above input attribute is added several times besides each sparepart type while changing just the name value. On clicking Submit, the user is taken to a review page with details on the spareparts ordered.
I presently have the following code in review page:
<?php
echo 'Engine Bearings: ' . $_POST['eng-bearings'];
echo 'Cylinder Heads: ' . $_POST['cyl-heads'];
echo 'Cam and Valve Train: ' . $_POST['cam-valve-train'];
echo 'Oil Pump: ' . $_POST['oil-pump'];
echo 'Oil Caps and Pipes: ' . $_POST['oil-caps-pipes'];
?>
This works well if the user has entered a value in every field. If he doesn't, then the review page has just the echo text without any quantity. Also, I don't want the quantity to apppear if the user has entered 0.
I can't figure out how to use isset() with null and OR statements to not echo text when the value entered is 0 or left blank.
Any help is appreciated. Thanks!!
This is the perfect opportunity for a function:
function print_user_input($title, $queryVar) {
if (isset($_POST[$queryVar]) && !empty($_POST[$queryVar])) {
echo $title . ': ' . $_POST[$queryVar];
}
}
Use it like so:
print_user_input('Engine Bearings', 'eng-bearings');
Echo the value conditionally. If the target data is empty, echo an empty string:
echo empty($_POST['eng-bearings']) ? '': 'Engine Bearings: ' . $_POST['eng-bearings'];
Repeat for the other post values. Information about the ternary operator (<conditional> ? <value1> : <value2>) is here.
You need to check if its set & that its not empty before printing it on a screen.
<?php
if (! empty($_POST['eng-bearings'])) {
echo 'Engine Bearings: ' . $_POST['eng-bearings'];
} else {
// whatever you wanna do here in case eng-bearings is not set or its empty
}
// Do same for other $_POST values ...
?>
I'm trying to use ACF to output different salary amounts for a job listing board.
E.g:
Select box option in ACF for salary type ("competitive", per day, per annum etc). Looks like this:
Then I have conditional fields. So if "P.A" is selected it shows the P.A field:
I have a bunch of these conditional fields for the different select box options.
My question is:
How do I add these to an if statement so that the correct field is shown based on the content that was supplied?
E.g
if job_salary was selected as "competitive" =
echo "competitive"
else if job_salary was selected as "p.a" =
echo job_salary_singular
else if job_salary was selected as "p.a range" =
echo job_range_start " to " job_range_end
Hopefully this makes sense. This is the code I have at the moment, which just outputs the type of salary entered (e.g, competitive, P.A, Per Day Range) as text.
This approach should work. In my example, the main selector field where you choose if it's per annum pay, competitive, a range, etc., is called salary_type.
Then I've got per_annum. It appearing is conditional on 'Per Annum' being selected from salary_type. And finally per_annum_range_low and per_annum_range_high, both of which are conditional on 'Per Annum Range' being selected.
We can then test based on the salary_type selection to output the appropriate values and HTML. Alternatively, you could test for the existence of those fields. But I think this is a bit cleaner and allows you to skip adding an additional field for "Competitive".
PHP
<?php
if (get_field('salary_type')) { //first we check if the salary_type field exists.
$selection = get_field('salary_type'); //then we store its value as '$selection'
if ($selection === 'comp') { //we check which selection was made by looking at its label.
echo '<p>' . 'Competitive' . '</p>';
} else if ($selection === 'perannum') { //ACF allows you to store a selection as both a value (in this case, 'perannum') and a label ('Per Annum', which is what the user sees.)
echo '<p>' . get_field('per_annum') . ' per year</p>';
} else if ($selection === 'perannumrange') {
echo '<p>From ' . get_field('per_annum_range_low') . ' to ' . get_field('per_annum_range_high') . ' per year</p>'; //and then echo its output and any HTML markup you want.
} else {
echo '<p>No Salary Info Given.</p>'; //if they don't make a selection
}
}
?>
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...