How to dropdown selected after data fetched - php

I want make option selected in dropdown html form, i am fetching data from database but into the html select cant get how to option selected
Below is the My database table data get. from that account holder is the my database value. I want to compare with select option nd make then selected into dropdown.
Array
(
[ipsf_public_pf_id] => 62
[ipsf_main_id] => 216
[account_no] => 54545455
[account_holder] => self
[pf_amt] => 1500.00
[date_of_payment] => 2020-11-15
)
and my html php form like this
$str .='<input type="hidden" name="ipsf_public_pf_id" value="<?php echo $ipsf_public_pf; ?>">
<table align="center" cellspacing="1" class="table table-bordered">
<tbody>
<tr>
<td>PPF A/c No</td>
<td>Account Holder</td>
<td>Date of Payment</td>
<td>Amount</td>
</tr>
<tr>
<td>
<input name="account_no_'.$ppf.'" type="text" id="account_no_'.$ppf.'" value="'.$account_no.'" size="20" maxlength="20" class="form-control" onkeyup="Validate_number(this)"/>
</td>
<td>
<select name="account_holder_'.$ppf.'" id="account_holder_'.$ppf.'" class="form-control">
<option value="" selected>Account Holder</option>
<option value="self">Self</option>
<option value="spouse">Spouse</option>
<option value="children">Children</option>
<!--<option value="others">Others</option>-->
</select>
</td>
<td>
<input name="date_of_payment_date_'.$ppf.'" id="date_of_payment_date_'.$ppf.'" maxlength="2" style="width: 50px;" type="text" onClick="this.value="";" value="'.$date.'" class="form-control-date" onkeyup="Validate_number(this)"/>-<input name="date_of_payment_month_'.$ppf.'" id="date_of_payment_month_'.$ppf.'" maxlength="2" style="width: 50px;" type="text" onClick="this.value="";" value="'.$month.'" class="form-control-date" onkeyup="Validate_number(this)"/>-<input name="date_of_payment_year_'.$ppf.'" id="date_of_payment_year_'.$ppf.'" maxlength="4" style="width: 65px;" type="text" onClick="this.value="";" value="'.$year.'" class="form-control-date" onkeyup="Validate_number(this)"/>
</td>
<td>
<input name="ppf_amt_'.$ppf.'" type="text" id="ppf_amt_'.$ppf.'" value="'.$pf_amt.'" size="10" onBlur="javascript:increment_ppf_amnt(this.value, '.$ppf.');" maxlength="14" class="form-control-date" onkeyup="Validate_number(this)"/>
<input type="hidden" name="prev_ppf_amount_'.$ppf.'" id="prev_ppf_amount_'.$ppf.'" value="'.$ppf.'"/>
<input type="hidden" name="is_added_0" id="is_added_0" value="no"/>
</td>
</tr>
</tbody>
<input type="hidden" name="total_ppf_amount" id="total_ppf_amount" value="0"/>
</table>';
I am confused how to selected that option inside .
Anyone know then please help me

On your html just fill the selected="<?=$selectedOption;?>.
Basically the logic behind it is: you fetch the data that you want to, and the values thaty ou on option tags you will gonna use the selected id / value or whatever that you want to use for in the selected atrribute on the <select></<select> html tag.

Related

'' is not a valid value for the type xsd:date (PHP)

I have an HTML form that posts all the gathered information (i.e. when the user enters the details and clicks on submit) it will post to the to a PHP script for processing. The problem is that when I do not pass any value for the date fields, I am getting the error below.
Error: '' is not a valid value for the type xsd:date
How can I make my post accept a null value for the fields and successfully submit?
Form.html:
<form class="form-horizontal" action="CandidateCreationPage.php" method="post" enctype="multipart/form-data">
<table class="form-table">
<tbody>
<tr>
<td class="label-cell"><label>First Name <span class="required-input letters-only">*</span></label></td>
<td>
<input class="required-field letters-only" type="text" name="firstname" id="first-name" maxlength="25" />
</td>
</tr>
<tr>
<td class="label-cell"><label>Last Name <span class="required-input">*</span></label></td>
<td>
<input class="required-field letters-only" type="text" name="lastname" id="last-name" maxlength="30" />
</td>
</tr>
<tr>
<td class="label-cell"><label>How Did You Learn of This Opportunity? <span class="required-input">*</span></label></td>
<td>
<select class="required-field" id="hear-this-opportunity" name="hearthisopportunity">
<option value="">--None--</option>
<option value="Kijiji">Kijiji</option>
</select>
</td>
</tr>
<tr>
<td class="label-cell"><label>Please Specify (If Applicable) <span id="please-specify-asterisk" class="required-input hide">*</span></label></td>
<td>
<input class="letters-only" type="text" name="pleasespecify" id="please-specify" maxlength="50" />
</td>
</tr>
<tr>
<td class="label-cell"><label>Status</label></td>
<td>
<select id="status" name="status">
<option value="">--None--</option>
<option value="Full-time">Full-time</option>
</select>
</td>
</tr>
<tr>
<td class="label-cell"><label>Have You Worked With Us Before?</label></td>
<td>
<select id="worked-previously" name="workedpreviously">
<option value="">--None--</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
</tr>
<tr>
<td class="label-cell"><label>When (If Applicable)</label></td>
<td>
<input type="text" name="workedpreviouslywhen" id="worked-previously-when" />
</td>
</tr>
<tr>
<td class="label-cell"><label>Available to Start</label></td>
<td>
<input type="text" name="availabletostart" id="available-to-start" />
</td>
</tr>
<tr>
</form>
</body>
</html>
Post.PHP
if(isset($_POST['submit']))
{
try{
$fields = array (
'First_Name__c' => $_POST['firstname'],
'Last_Name__c' => $_POST['lastname'],
'Primary_contact_number__c'=>$_POST['primarycontactnumber'],
'Secondary_contact_number__c'=>$_POST['secondarycontactnumber'],
'Email__c'=> $_POST['email'],
'How_did_you_learn_of_this_Opportunity__c'=>$_POST['hearthisopportunity'],
'Please_Specify__c'=>$_POST['pleasespecify'],
'Status__c'=>$_POST['status'],
'Have_you_worked_with_us_before__c'=>$_POST['workedpreviously'],
'When__c'=>$_POST['workedpreviouslywhen'],
'Available_to_start__c'=>$_POST['availabletostart'],);
as an alternative way You can remove date related variables from the first array declaration and thereafter You can check if date variables are not empty and append them to your fields array.
$fields = array (
'First_Name__c' => $_POST['firstname'],
'Last_Name__c' => $_POST['lastname'],
etc
}
remove from $fields array variables like:
["When__c"]=>
["Available_to_start__c"]
and then lets check, and append variables to an array if they are not empty:
if(!empty($_POST['When'])){
$fields['When__c'] = $_POST['When'];
}
if(!empty($_POST['AvlToStart'])){
$fields['Available_to_start__c'] = $_POST['AvlToStart'];
}
etc

Pass multiple values of same data field through POST

I'm trying to pass multiple values from same parameters of a form using POST, but can't figure out how to proceed. I've used bootstrap css such that I can add multiple products. And I want to process the data of multiple orders by passing the values using POST method.
On clicking 'Add another' link, additional set of data field appear which enables recording of multiple transactions of a same user.
The code is as follows:
<div class="col-xs-5 col-lg-offset-3">
<form action="billingProceed.php" method="post" role="form">
<table id="itemElement">
<tr>
<td>
<select class="form-control">
<option class="form-control"> Customer Name</option>
<option class="form-control"> Customer ID</option>
</select>
</td>
<td><input type="text" name="<?php echo $data["name"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td>
<select class="form-control">
<option class="form-control"> Item Name</option>
<option class="form-control"> Item ID</option>
</select>
</td>
<td ><input type="text" name="<?php echo $data["item"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td style="float:right;">Quantity
</td>
<td><input type="text" name="<?php echo $data["quantity"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td style="float:right;">Price
</td>
<td><input type="text" name="<?php echo $data["price"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td style="float:right;">Discount
</td>
<td><input type="text" name="<?php echo $data["discount"]; ?>" class="form-control" /></td>
</tr>
</table>
<input type="submit" value="Proceed" class="btn btn-primary" />
<p style="float:right;">Add another</p>
You can use an array name.
Example:
<input name="data['name'][1]">
<input name="data['name'][2]">
Firstly you should be aware of the name array, for input names.
HTML Example:
<form>
<a id="add_another" href="#">Add Another</a>
<table>
<tr class="product_item">
<td>
<input type="text" name="product[1][name]" value=""/>
</td>
<td>
<input type="text" name="product[1][item]" value=""/>
</td>
</tr>
<tr id="dummy">
<td>
<input type="text" name="product[0][name]" value=""/>
</td>
<td>
<input type="text" name="product[0][item]" value=""/>
</td>
</tr>
</table>
</form>
On POST, in your PHP script, you will access them as follows:
foreach($_POST['product'] as $product)
{
$name = $product['name'];
$item = $product['item'];
}
Shoot for the JS
//You'll always start with one product row.
var productCount = 1;
$('#add_another').click(function() {
var dummyproduct = $('#dummy').clone();
//Increment Product count
productCount += 1;
//Rename all inputs
dummyproduct.find('input[name^="product[0]"]').each(function(){
$(this).attr('name',$(this).attr('name').replace('product[0]','product['+productCount +']'));
});
//Remove Id from cloned dummy
dummyproduct.removeAttr('id').addClass('product_item');
//Insert row before Dummy
dummyproduct.insertBefore('#dummy');
});

javascript change remaining dropdown values depending on first

My HTML table populates its rows dynamically. One of the table data is a dropdown with thesame options. What I want to do is when I change the value of the first, the remaining dropdowns on the other rows will have a selected value, thesame as what is recently selected.
If I change the selected value on the second row, the rest of the dropdowns will be like the second, without affecting the value of the first. Here are a few codes:
<table id="CheckDetails" width="60%">
<tr>
<th>Date</th>
<th>Category</th>
<th>Description</th>
<th>Amount</th>
</tr>
<tr class="row1">
<td><input name="date1" id="date1" class="" value="22-09-2013" /></td>
<td><select name="category" id="cat0" class="dropdown" >
<option value="0">[-- Please Choose --]</option>
<option value="1">References</option>
<option value="2">Inspirational</option>
</select>
</td>
<td><input type="text" class="entry" name="desc" id="desc0" value="1"/></td>
<td><input type="text" class="entry" name="amt" value="420.83333333333"/></td>
</tr>
<tr class="row1">
<td><input name="date1" id="date1" class="" value="22-09-2013" /></td>
<td><select name="category" id="cat0" class="dropdown" >
<option value="0">[-- Please Choose --]</option>
<option value="1">References</option>
<option value="2">Inspirational</option>
</select>
</td>
<td><input type="text" class="entry" name="desc" id="desc0" value="1"/></td>
<td><input type="text" class="entry" name="amt" value="420.83333333333"/></td>
</tr>
<tr class="row1">
<td><input name="date1" id="date1" class="" value="22-09-2013" /></td>
<td><select name="category" id="cat0" class="dropdown" >
<option value="0">[-- Please Choose --]</option>
<option value="1">References</option>
<option value="2">Inspirational</option>
</select>
</td>
<td><input type="text" class="entry" name="desc" id="desc0" value="1"/></td>
<td><input type="text" class="entry" name="amt" value="420.83333333333"/></td>
</tr>
I am working with php for this project. Any help is appreciated. Thanks!
Try
$('select[name="category"]').change(function(){
var $this = $(this), $nextAll = $this.closest('tr').nextAll('tr').find('select[name="category"]');
$nextAll.val($this.val())
});
Demo: Fiddle

Add another text box when a user select other option

My form is looking like this.
<form action="add_customers.php" method="post" onsubmit="MM_validateForm('name','','R','passport_no','','R','remarks','','R');return document.MM_returnValue">
<tr>
<td>Name of the Applicant:</td>
<td><label for="name"></label>
<input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>Country applying for :</td>
<td><label for="country"></label>
<select name="country" id="country">
<option>Singapore</option>
<option>Malaysia</option>
<option>Istanbul</option>
<option>Thailand</option>
<option>China</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td>Visa Categeory :</td>
<td><label for="visa_categeory"></label>
<select name="visa_categeory" id="visa_categeory">
<option>Visit</option>
<option>Business</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td>Passport No:</td>
<td><input type="text" name="passport_no" id="passport_no" /></td>
</tr>
<tr>
<td>Remarks:</td>
<td><label for="remarks"></label>
<textarea name="remarks" id="remarks" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" id="submit" value="Submit" /></td>
</tr></form>
Now My problem is
1) when a user select other option from Visa Category then automatically display a text box for user and capture that what he enter.
2) save that details into mysql database
Used languages are PHP,MYSQL
Thanks.
You can add a text box in your form and make it hidden first time.
Get the textbox visible on change of the dropdown you have mentioned using javascript.
You can get the value of the textbox in the page where you accepts the data and insert into data database in add_customers.php along with other variables.
<script>
function visacatOnchange(){
var visa_cat = document.getElementById('visa_categeory').value
if(visa_cat == "Visit")
document.getElementById("new_textbox").style.display="block";
else
document.getElementById("new_textbox").style.display="none";
}
</script>
In html form add
<input type="text" name="new_textbox" id="new_textbox" style="display:none;">
and change
<select name="visa_categeory" id="visa_categeory" onChange="visacatOnchange();">
Good luck :-)
Initially you can keep the textbox div hidden and then display it on "onchange" event of select input.
<div id="textboxdiv" style="visibility: hidden">
Visa Option <input type="text" id="someid"/>
</div>
<select name="visa_categeory" id="visa_categeory" onchange="showTextBox()" >
<option>Visit</option>
<option>Business</option>
<option value="99" >Other</option>
</select>
Using Jquery you can display it like this:-
function showTextBox(){
if ($('#visa_categeory').val() == '99') {
$('#textboxdiv').css({'visibility':'visible'});
}
Here's some basic JavaScript/jQuery that should help you solve your first problem: http://jsfiddle.net/j4aXQ/
HTML
<form>
<select name="visa_category">
<option>Visit</option>
<option>Business</option>
<option>Other</option>
</select>
</form>
JS
$('select').change(function() {
var $selected = $('select option:selected');
if ('Other' === $selected.text()) {
$('form').append($('<input>').prop({'type':'text','id':'visa_other','name':'visa_other'}));
$('form').append($('<label>').prop({'for':'visa_other'}).html('Testing'));
} else {
$('input').remove();
$('label').remove();
}
});
The above code will only display a text box (and accompanying label) if the "Other" option is selected, and will then remove that text box if a different option is subsequently picked.
Hope that helps!

missing keys in $_POST

I am trying to get some form data from POST method.
Here's the code of form -
<form action="" method="post" name="form1" id="form1">
<input type="hidden" value="15" name="ad_id">
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="block">
<tbody><tr>
<td valign="top"> </td>
<td align="right">all fields are required</td>
</tr>
<tr>
<td valign="top"> </td>
<td align="center"></td>
</tr>
<tr>
<td valign="top" width="150"><label for="name">Advertisement Name</label>
*</td>
<td><input type="text" size="45" value="Banner" id="name" name="name">
e.g Home Banner</td>
</tr>
<tr>
<td valign="top"><label for="placement">Advertisement Placement</label></td>
<td><select id="placement" name="placement">
Wide Skyscrapper 160 x 600
</tr>
<tr>
<td valign="top"><label for="code">Advertisement Code</label></td>
<td><textarea rows="5" cols="45" id="code" name="code"></textarea></td>
</tr>
<tr>
<td>Status</td>
<td><label>
<input type="radio" checked="checked" value="1" name="status">
Active</label>
<label>
<input type="radio" value="0" name="status">
Inactive</label></td>
</tr>
<input type="hidden" value="1" name="banner_uploaded" id="banner_uploaded">
<tr>
<td>For Country - </td>
<td>
<select id="country" name="country">
<option>Not posting all the names of country</option>
</select>
</td>
</tr>
<tr>
<td><label for="Scheduling">Valid From </label></td>
<td><input type="text" value="" id="date-from" name="date-from"> Format : dd/mm/yyyy:hh/mm</td>
</tr>
<tr>
<td><label for="Scheduling">Valid Till </label></td>
<td><input type="text" value="" id="date-to" name="date-to"> Format : dd/mm/yyyy:hh/mm</td>
</tr>
<tr>
<td> </td>
<td align="right"><input type="submit" onclick="return validate_ad_form(add_adv)" value="Update Advertisement" class="button" name="update"></td>
</tr>
</tbody></table>
</form>
But I am getting $_POST['code'] empty when I am passing HTML code through it.
When I pass plain text, it works fine.
When I printed $_POST [i.e. used - print_r($_POST) ], I got the following output -
Array ( [ad_id] => 15 [name] => Banner [placement] => ad_468x60 [code] => [status] => 1 [banner_uploaded] => 1 [country] => IN [date-from] => [date-to] => [update] => Update Advertisement )
Please be known, I haven't entered the 'date-from','date-to' fields.
I have entered on purpose as StackOverflow don't allow me to post images!
People,any help will be highly appreciated.
The problem is you're print_r() is being displayed in a brower. If you've got HTML in your POST data, those tags will be interested as HTML when you dump the data back out. You'll have to force the output into plaintext mode (header('Content-type: text/plain);), or run the fields which can have HTML in them through htmlspecialchars() to escape the < and > characters.
If you view the source of the page you're doing the print_r() on, I suspect you'll see the HTML tags there. If you've only tested with that example.jpg image, you wouldn't see anything, because that image most likely doesn't exist, so it won't render, and then the <a> tag has nothing to underline and won't show up either.

Categories