I'm working on a profile update page and i have a form field where users can select enabled or disabled. When i echo the value, it has a duplicate and can be confusing for the user. How can i get rid of the duplicate?
<div class="form-group">
<label for="status" class="col-sm-3 control-label">Change Status</label>
<div class="col-sm-9">
<select class="form-control btn-danger" id="status" name="status">
<option value="<?php echo $status ?>"><?php echo $status ?></option>
<option value="Disabled">Disabled</option>
<option value="Enabled">Enabled</option>
</select>
</div>
</div>
As you can see, if the status is enabled and i hit the select menu its gonna say Enabled twice, same thing for disabled.
The only thing i thought of was to have another field labeled current status and have the select menu to change status.
I really dont want to do that.
<div class="form-group">
<label for="currentstatus" class="col-sm-3 control-label">Current Status</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="currentstatus" name="currentstatus" value="<?php echo $status ?>">
</div>
</div>
<div class="form-group">
<label for="status" class="col-sm-3 control-label">Change Status</label>
<div class="col-sm-9">
<select class="form-control btn-danger" id="status" name="status">
<option value="<?php echo $status ?>"><?php echo $status ?></option>
<option value="Disabled">Disabled</option>
<option value="Enabled">Enabled</option>
</select>
</div>
</div>
This worked for me:
<option <?php if ($status=="Disabled") echo " selected"; ?>>Disabled</option>
<option <?php if ($status=="Enabled") echo " selected"; ?>>Enabled</option>
Instead of outputting the selected value as another option, output it as the selected attribute of an existing option. Something like this:
<select class="form-control btn-danger" id="status" name="status">
<option value="Disabled" <?php echo ($status == "Disabled") ? "selected" : ""; ?>>Disabled</option>
<option value="Enabled" <?php echo ($status == "Enabled") ? "selected" : ""; ?>>Enabled</option>
</select>
There are likely a variety of ways to achieve the same end result, but the point is to not add options based on the data you have but instead use that data to determine which of the existing, finite, hard-coded options you already have should be selected.
Here is some jQuery that will let you change the value of the select box.
function setStatusSelect(val) {
$("select#status").val(val);
}
setStatusSelect('Enabled');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label for="status" class="col-sm-3 control-label">Change Status</label>
<div class="col-sm-9">
<select class="form-control btn-danger" id="status" name="status">
<option value="Disabled">Disabled</option>
<option value="Enabled">Enabled</option>
</select>
</div>
</div>
If you include the Jquery function at the top of the page (so that it loads quickly) then you can do something like this:
<?php
print("<script type=\"text/javascript\">");
print("setStatusSelect(".$status.")");
print("</script>");
?>
to set the value.
I haven't tested the PHP portion, but I know the jQuery portion works.
Related
$batch = htmlentities($_POST['batch']);
if($batch === 'NULL'){
echo "Batch Failed";
} else {
echo "$bacth";
}
<div class="col-md-12">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-globe"></i></span>
<select class="form-control" name="batch" id="select">
<option selected disabled>Batch Preference</option>
<option value="Weekends">Weekends</option>
<option value="Holidays">Holidays</option>
</select>
</div>
</div>
</div>
How to select those select options in form using php, how i can validate them...
Thank You
I am trying to achieve something via php.
Here there are couple of options.
<select class="custom-select" id="inputGroupSelect01">
<option selected="">Select Clusters ...</option>
<option value="math-related">Math Related</option>
<option value="science-related">Science Related</option>
</select>
There are Group of subject fields which will appear when the options are changed respectively.
<div class="form-row pb-5" id="math-related"">
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num1" placeholder="English">
</div>
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num2" placeholder="Math">
</div>
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num3" placeholder="Science">
</div>
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num4" placeholder="Computer">
</div>
</div>
When the option is changed to Math related OR Science Related option, the related 5-6 subject fields will be populated like image below. Some might have 5, some might have 8.
How to refresh the page and load the new sets of input fields according to selection done via PHP?
Example Image for fields
First of all change your select dropdown with your page url + parameter and make sure to replace YOUR_PAGE_URL with actual url as follows -
<select onChange="window.location.href=this.value" class="custom-select" id="inputGroupSelect01">
<option value="">Select Clusters ...</option>
<option value="YOUR_PAGE_URL?topic=math-related">Math Related</option>
<option value="YOUR_PAGE_URL?topic=science-related">Science Related</option>
</select>
After then just do a GET parameter checking on your page to populate your fields like -
if( issset($_GET['topic']) ){
if( $_GET['topic'] == 'math-related' ){
echo "Add your math related above 5-8 fields html here.";
}elseif( $_GET['topic'] == 'science-related' ){
echo "Add your science related above 5-8 fields html here.";
}
}
I have very simple dropdown menu like below in my PHP page.
<div class="form-group">
<label class="col-md-3 control-label">FAQ Type :-</label>
<div class="col-md-6">
<select name="faq_type" id="faq_type" class="select2" required>
<option value="0">Text</option>
<option value="1">Image</option>
<option value="2">Video</option>
</select>
</div>
</div>
I want select Text, Image or Video selection automatically according to value I get from database, for other field I am able to echo like below
<div class="col-md-6">
<textarea name="question" id="question" rows="1" class="form-control" ><?php if(isset($_GET['faq_id'])){echo $row['question'];}?></textarea>
</div>
How can I do same for dropdown ?
Edit : according to answer, I was able to do it but I have one small another issue in it, I want only set selected if if(isset($_GET['faq_id'])) else want show normal all button as currently I am showing as above.
<div class="form-group">
<label class="col-md-3 control-label">FAQ Type :-</label>
<div class="col-md-6">
<?php
if(isset($_GET['faq_id'])) {
<select name="faq_type" id="faq_type" class="select2" required>
<option value="0" <?php if($row['type'] == "0") echo "selected"; ?>>Text</option>
<option value="1" <?php if($row['type'] == "1") echo "selected"; ?>>Image</option>
<option value="2" <?php if($row['type'] == "2") echo "selected"; ?>>Video</option>
</select>
} else {
}
?>
</div>
</div>
if I do like this, its giving me error like below
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\new\add_faq.php on line 169
Thanks
For drop down you can set selected field like this
<div class="form-group">
<label class="col-md-3 control-label">FAQ Type :-</label>
<div class="col-md-6">
<select name="faq_type" id="faq_type" class="select2" required>
<option value="0" <?php if($row['your_db_field'] == "0") { ?> selected="selected" <?php } ?> >Text</option>
<option value="1" <?php if($row['your_db_field'] == "1") { ?> selected="selected" <?php } ?> >Image</option>
<option value="2" <<?php if($row['your_db_field'] == "3") { ?> selected="selected" <?php } ?> >Video</option>
</select>
</div>
</div>
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
ive been all around trying to fix the code but i still get an error
Parse error: syntax error, unexpected 'userfirstName' (T_STRING), expecting ',' or ';' in /home/crosswayprinting/public_html/ztest2/functions/shop.php on line 66
here's the code btw
whats my mistake :/ ?
<?php if($user_home->is_logged_in()){
echo '
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<h2 class="section-heading">Place an Order</h2>
<div class="text-center" style="input, select, textarea{
color: #000;
}">
</div><BR>
<form role="form" class="userTrans" method="POST">
<input type="hidden" value="OrderInsert" name="act_userTrans">
<div class="form-group">
<label for="typeofservice">Select Type of Service</label>
<select id="typeofservice" class="form-control" name="typeofservice">
<option value="Tarpaulin">Tarpaulin</option>
<option value="Rush ID">Rush ID</option>
<option value="Photocopy">Photocopy</option>
<option value="Graphic Layout">Graphic Layout</option>
<option value="Invitation">Invitation</option>
<option value="Panaflex">Panaflex</option>
<option value="Signages">Signages</option>
<option value="Stickers">Stickers</option>
<option value="Sintra board">Sintra board</option>
<option value="Large Format Photo">Large Format Photo</option>
<option value="PVC ID">PVC ID</option>
<option value="Lamination">Lamination</option>
<option value="Bag Tags">Bag Tags</option>
<option value="Notary Public">Notary Public</option>
<option value="Scan">Scan</option>
</select>
</div>
<div class="form-group">
<label for="templateselect">Template Selection</label>
<select id="templateselect" class="form-control" name="templateselect">
<option value="Own Made Template">Own Made Template</option>
<option value="Pre-made Template">Pre-made Template</option>
</select>
</div>
<div class="form-group">
<label for="text">More details about your order</label>
<input type="text" class="form-control" id="orderdetails" name="orderdetails">
</div>
<div class="form-group">
<label for="text"> Customer Name</label>
<input type="text" value=" <?php echo $row['userfirstName']; ?> " class="form-control" id="customer" name="customer">
</div>
/* this is the code btw what makes it an error, i am trying to
echo a customer's name and make it Passive or uneditable textbox so it can
register to my orderlist to whom ordered */
<button type="submit" class="btn btn-default userTrans">Submit</button>
</form>
';
}
else{
echo '
<center> Please Login to Place an Order </center>
';}
?>
Get rid of the echo statements wrapping your HTMl. There's no need, just escape out of the PHP interpreter and print your HTML. Then you won't get an error when you're doing <?php echo $row['userFirstName']; ?>....Something like this:
<?php if($user_home->is_logged_in()){ ?>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<h2 class="section-heading">Place an Order</h2>
<form role="form" class="userTrans" method="POST">
<input type="hidden" value="OrderInsert" name="act_userTrans">
<div class="form-group">
<label for="typeofservice">Select Type of Service</label>
<select id="typeofservice" class="form-control" name="typeofservice">
<option value="Tarpaulin">Tarpaulin</option>
<option value="Rush ID">Rush ID</option>
<option value="Photocopy">Photocopy</option>
<option value="Graphic Layout">Graphic Layout</option>
<option value="Invitation">Invitation</option>
<option value="Panaflex">Panaflex</option>
<option value="Signages">Signages</option>
<option value="Stickers">Stickers</option>
<option value="Sintra board">Sintra board</option>
<option value="Large Format Photo">Large Format Photo</option>
<option value="PVC ID">PVC ID</option>
<option value="Lamination">Lamination</option>
<option value="Bag Tags">Bag Tags</option>
<option value="Notary Public">Notary Public</option>
<option value="Scan">Scan</option>
</select>
</div>
<div class="form-group">
<label for="templateselect">Template Selection</label>
<select id="templateselect" class="form-control" name="templateselect">
<option value="Own Made Template">Own Made Template</option>
<option value="Pre-made Template">Pre-made Template</option>
</select>
</div>
<div class="form-group">
<label for="text">More details about your order</label>
<input type="text" class="form-control" id="orderdetails" name="orderdetails">
</div>
<div class="form-group">
<label for="text"> Customer Name</label>
<input type="text" value=" <?php echo $row['userfirstName']; ?> " class="form-control" id="customer" name="customer">
<!-- now the above won't give an error -->
</div>
<button type="submit" class="btn btn-default userTrans">Submit</button>
</form>
</div>
</div>
<?php } else { ?>
<!-- Do not use center tags when you're using bootstrap framework -->
<!--<center> Please Login to Place an Order </center>-->
<div class="text-center"> Please Login to Place an Order </div>
<?php } ?>
Also what's going on with your inline styles? That's not how this works:
<div class="text-center" style="input, select, textarea{
color: #000;
}">
Just move that CSS somewhere else. You can't use targeted selectors within the style="" tag, only relevant CSS.
I have some php which lists a bunch of options (through a loop) and prints them on the page with a new line for each.
I want the options to come up in a dynamically sized drop down HTML list each with different option values.
This is the PHP (it works perfectly):
if($postOpts) {
foreach($postOpts as $option)
echo $option["name"]." - ".$option["price"]."<BR>";
}
//Otherwise, present the default postage option for domestic or international.
else {
echo "Default Shipping Option - $".$post->getDefaultPrice();
}
I have a form but as you can see the form amount of options is static not dynamic:
<form> <!-- form action will go here -->
<div class="form-group col-xs-12">
<div class="col-xs-3 col-md-2 form-label"><label>Shipping Options:</label></div>
<div class="col-xs-4 col-md-3">
<select class="form-control" name="list" title="pick a type">
<!-- I assume I need to iterate through options here -->
<option value="01">Shipping1</option>
<option value="02">Shipping2</option>
<option value="03">Shipping3</option>
</select>
</div>
</div>
</form>
EDIT I forgot the other else statement in the PHP, please check.
Replace your select with this,
<select class="form-control" name="list" title="pick a type">
<?php if(count($postOpts)) { ?>
<?php foreach($postOpts as $row) { ?>
<option value="<?= $row["price"] ?>"><?= $row["name"] ?></option>
<?php } ?>
<?php } else { ?>
<option value="<?= $post->getDefaultPrice() ?>"><?= "Default Shipping Option - $".$post->getDefaultPrice() ?></option>
<?php } ?>
</select>
You can try this
<form> <!-- form action will go here -->
<div class="form-group col-xs-12">
<div class="col-xs-3 col-md-2 form-label"><label>Shipping Options:</label></div>
<div class="col-xs-4 col-md-3">
<select class="form-control" name="list" title="pick a type">
<option value="">Choose type</option>
<?php foreach($postOpts as $option) { ?>
<option value="<?php echo $option['name'].' - '.$option['price'];?>"><?php echo $option["name"]." - ".$option["price"]; ?></option>
<?php } ?>
</select>
</div>
</div>
</form>