Making input to select - php

I'm trying to make auto dropdown list with quantity of products.
okay, that's problem (cms - opencart)
I have In default input to write product qnt:
<input type="text" name="quantity" size="2" value="<?php echo $minimum; ?>" />
$minimum is that you set when uploading product.
I want to make dropdown list with options.
Now I'm trying to change to select, and thats my code:
<select name="quantity">
<?php foreach ($discounts as $discount) { ?>
<option name="quantity" value="<?php $discount['quantity']; ?>"><?php echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?></option><br />
<?php } ?>
</select>
okay thats good in preview, I'm get my select list, but then customer select one from the list, It not quanty for product, always all options set to = 1.
In prieview text $discount['quantity'] show correctly number, but not set to value.. Please help :)

Don't set the attribute "name" to the option tag. This might be the issue. And there is also no need for the br tag after each option. It should look like this:
<select name="quantity">
<?php foreach ($discounts as $discount) { ?>
<option value="<?php echo $discount['quantity']; ?>"><?php echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?></option>
<?php } ?>
</select>

Related

How to change form submission into SEO friendly URL

I have a simple form that compares two products using a select form and when user hits submit, it redirects to another site but the problem is the URL is rather long and ugly looking right now its displayed like this:
product_details.php?item1=core-i3-7300&submit=Compare&item2=core-i5-12400
I would like the form to submit in this URL structure:
product/item1/item2
Here is the code for my form, I also already have the HTACCESS rule created to accept the URL structure I want, I just need to correct format from the submission form.
<form action="compare/" method="GET" onSubmit="return validateThisFrom (this);" >
<select id="selectPlatform" Name="item1" required="required" class="classic">
<option value="">- Select Product -</option>
<?php foreach ($Names as $Name) : ?>
<option value="<?php echo $Name->Slug; ?>"><?php echo $Name->Make; ?> - <?php echo $Name->Name; ?></option>
<?php endforeach; ?>
</select>
<button input type="submit" name="submit" value="Compare" class="button red">
<select id="selectPlatform2" Name="item2" required="required" class="classic">
<option value="">- Select Product -</option>
<?php foreach ($Names as $Name) : ?>
<option value="<?php echo $Name->Slug; ?>"><?php echo $Name->Make; ?> - <?php echo $Name->Name; ?></option>
<?php endforeach; ?>
</select>
</form>

How to disable input in datalist?

I want to block the user to input I just want him to choose only from the list
I tried disable and readonly but this will disable my list and I don't want that.
This is my code
<div class="form-group row justify-content-center" id=" input-champs " style="margin-left:;" ><!-- grossistee -->
<label for="grossiste" class="col-2 col-form-label"></label>
<div class="col-3">
<input type="text" list="list-gro" placeholder="Grossiste1" id="g_name1" name="g_name1" class="form-control">
<datalist id="list-gro">
<?php while($row = mysqli_fetch_array($resultg)) { ?>
<option value="<?php echo $row['g_name']; ?>"><?php echo $row['g_name']; ?></option>
<?php } ?>
</datalist>
</div>
From the living standard spec of the datalist element:
Each option element that is a descendant of the datalist element, that is not disabled, and whose value is a string that isn't the empty string, represents a suggestion. Each suggestion has a value and a label.
Conversely, this means that a user can enter anything he wants. The datalist element only contains suggestions that the user can use, but does not have to.
For your puprose I 'd suggest a select element. A select element dictates strict values for the user to use.
<label for="my-select">My Select</label>
<select name="my-select" id="my-select" required>
<option value="" disabled selected>Select your option</option>
<?php foreach ($databaseresult as $row): ?>
<option value="<?= htmlspecialchars($row['value']) ?>">
<?= htmlspecialchars($row['label']) ?>
</option>
<?php endforeach ?>
</select>
Its better to use select in place of input as you dont want the user to type in anything from himself/herself.
You can do like this to implement select with database query
<select name="g_name1" id="g_name1" class="form-control">
<?php while($row = mysqli_fetch_array($resultg)) { ?>
<option value="<?php echo $row['g_name']; ?>"><?php echo $row['g_name']; ?></option>
<?php } ?>
</select>

retrieve value from drop down bar and display result

on calculatePC.php, I have this code to display the finish_product
Select product:
<select class="itemTypes">
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
Let's say I have chosen Table as the finish_product.
On docalculate.php, I would like to display what I've chosen based on the dropdown list I've selected.
I tried this but there is error.
<?php echo $_POST['finish_product'] ?>
May I know how to display the result?
This doesn't exist:
$_POST['finish_product']
because you don't have a form element named "finish_product" in your markup. Add that name to the form element:
<select name="finish_product" class="itemTypes">
You need to do two things:-
Create a form before select and give it an action
give name attribute to your select box, then only you can get data in $_POST
So do like below:-
<form method="POST" action = "docalculate.php"> // give the php file paht in action
<select class="itemTypes" name="finish_product"> // give name attribute to your select box
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
<input type ="submit" name="submit" value ="Submit">
</form>
Now on docalculate.php:-
<?php
echo "<pre/>";print_r($_POST['finish_product']); // to see value comes or not
Note:- through Jquery its also possible. Thanks

Previous dropdown value not getting set with set_value in Codeigniter

The following is my HTML code
<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
<option value="">Select Martial Status</option>
<?php foreach ($martial_status as $status) { ?>
<option value="<?php echo $status['id'] ?>"><?php echo $status['status_name'] ?></option>
<?php } ?>
</select>
Even though i have used the form_validation in controller side, the value is not getting set in drop down once the form fails submitting. How to set the previous value selected in drop down on form failure. Where am I going wrong.
You cannot set the value straightly to select tag. You need to set the selected attribute to option tag which you want it to be get selected. Just like it below....
<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
<option value="">Select Martial Status</option>
<?php foreach ($martial_status as $status) { ?>
<option value="<?php echo $status['id'] ?>" <?php if ($status['id'] == set_value('martial_status')) echo "selected = 'selected'"?>><?php echo $status['status_name'] ?></option>
<?php } ?>
</select>
On generating the options tag check which option got selected and echo selected = 'selected' in the attribute of that option tag.

Require help to show selected list items in php/html form based on existing MySQL data

Any help would be greatly appreciated!!
I am building a php form to update a MySQL database linked to my website.
Part of my form includes list fields where the user can hold ctrl to select multiple values and these are then passed into one MySQL field as comma separated values. See below for three real examples of the data:
3014
3014,3015,3026
3026,3028
The form is updating my database as required, however when the user goes back in to the form to do another update the multiple values they chose aren't pre-selected.
Can anyone help me to figure out why $row_editme['NewsTypeID'] is the field containing the string of values. The field type is a SET.
<td nowrap="nowrap" align="right">NewsTypeID:</td>
<td>
<select name="NewsTypeID[]" size="4" multiple="multiple">
<?php
do {
?>
<option value="<?php echo $row_types['id']?>" <?php if (!(strcmp($row_types['id'], htmlentities($row_editme['NewsTypeID'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_types['name']?></option>
<?php } while ($row_types = mysql_fetch_assoc($types)); ?>
</select>
</td>
Try something like this,
<select name="NewsTypeID[]" size="4" multiple="multiple">
<?php
do {
$selected = htmlentities($row_editme['NewsTypeID'], ENT_COMPAT, 'utf-8');
$selected_arr = explode(",",$selected);
?>
<option value="<?php echo $row_types['id']?>" <?php if (in_array($row_types['id'],$selected_arr)) {echo "selected";} ?>>
<?php echo $row_types['name']?>
</option>
<?php } while ($row_types = mysql_fetch_assoc($types)); ?>
</select>
I think it is not supposed to be selected.
Can you try checked?
<input type="checkbox" <?php /*if condition is met here, then*/ echo "checked='checked'" ?> />
It will pre-select all the values the user selected.

Categories