i am populating a form from mysql. code is. .
task.php
<div data-role="content">
<h2> Please select cars </h2>
<form method="post" action="cars.php">
<?php
$carq = "select * from cars";
$executecars = mysql_query($carq);
while($row=mysql_fetch_assoc($executecars)){
$cname = $row['name'];
?>
<label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
<input type="checkbox" name="car" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
<?php }
?>
<input type="submit" name="submitcars" id="submitcars" value="View Details"/>
</form>
</div>
now in cars.php i want to make a query to display the details of car selected,
<div data-role="content">
<?php
if(isset($_POST['submitcars'])){
echo $_POST[$cname];?????????????
}
?>
</div>
now how to process the form here in cars.php ?
thanks
Make car attribute array::
<input type="checkbox" name="car[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
and get them on next page with
if(isset($_POST['submitcars'])){
foreach($_POST['car'] as $car){
// do something with $car
}
}
Try this
<form method="post" action="cars.php">
<?php
$carq = "select * from cars";
$executecars = mysql_query($carq);
while($row=mysql_fetch_assoc($executecars)){
$cname = $row['name'];
?>
<label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
<input type="checkbox" name="car[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
<?php }
?>
Firstly, let me preface this by saying that you shouldn't be using the mysql functions. They're depreciated and no longer recommended.
On task.php, your opening form tag was being printed inside your loop. Secondly, you should probably use an array of checkboxes using [].
<div data-role="content">
<h2> Please select cars </h2>
<form method="post" action="cars.php">
<?php
$carq = "select * from cars";
$executecars = mysql_query($carq);
while($row=mysql_fetch_assoc($executecars)){
$cname = $row['name'];
?>
<label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
<input type="checkbox" name="cars[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
<?php } ?>
<input type="submit" name="submitcars" id="submitcars" value="View Details"/>
</form>
</div>
Then, on cars.php:
<div data-role="content">
<?php
if(isset($_POST['submitcars'])){
if(isset($_POST['cars'] && count($_POST['cars']) > 0){
foreach($_POST['cars'] as $key => $car){
echo $car . '<br /'>;
}
}
else{
echo 'No cars found!';
}
}
?>
</div>
Related
I'm having problems with a simple contact form.
The error I'm getting is:
Notice: Undefined index: terms in contact.php on line 29
Attention! You have to check the Privacy Policy box to accept our terms.
Code:
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$terms = $_POST['terms']; // <---- line 29
if(trim($terms) == '') {
echo '<div class="alert error"><div class="msg">Attention! You have to check
the Privacy Policy box to accept our terms.</div></div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="alert error"><div class="msg">Attention! Please enter your
message.</div></div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
The template file is below:
<form action="<?php echo $this->config->get('config_url').'ajax/contact.php' ?>" method="post" id="contact_form" enctype="multipart/form-data">
<table>
<tr>
<td>
<label for="name"><?php echo $entry_name; ?></label>
<input type="text" name="name" id="name" style="margin-right:20px" value="<?php echo $name; ?>" />
<?php if ($error_name) { ?>
<span class="error"><?php echo $error_name; ?></span>
<?php } ?>
</td>
<td>
<label for="email"><?php echo $entry_email; ?></label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
<?php if ($error_email) { ?>
<span class="error"><?php echo $error_email; ?></span>
<?php } ?>
</td>
</tr>
</table>
<label for="enquiry"><?php echo $entry_enquiry; ?></label>
<textarea name="enquiry" id="enquiry" cols="30" rows="5"><?php echo $enquiry; ?></textarea>
<?php if ($error_enquiry) { ?>
<span class="error"><?php echo $error_enquiry; ?></span>
<?php } ?>
<input type="checkbox" name="terms" value="<?php echo $terms; ?>" />
<label for="terms">Tick this box to confirm you comply with our Privacy Terms</label>
<input type="submit" id="submit" value="<?php echo $button_continue; ?>" class="button dark-bt" />
</form>
Try this:
$terms = isset($_POST['terms']) ? $_POST['terms'] : '';
You will get the checkbox field only if it is checked by the user in the form.
So, always check like this for checkbox and radio buttons.
I have two questions with radio button (each question having 4 radio) in PHP
But there is single check in all 8 radio buttons, but it should be, with 4-4 radio buttons.
Look: https://imgur.com/Sytl72a
My Code:
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<?php
$lstmt = $user->runQuery("SELECT * FROM mcq WHERE LRN=:lrn ORDER BY Sr ASC ");
$lstmt->bindparam(":lrn",$id);
$lstmt->execute();
if($lstmt->rowCount() > 0)
{
$i=0;
while($lrow=$lstmt->fetch(PDO::FETCH_ASSOC))
{
extract($lrow);
$i++;
?>
<div>
<h1><?php echo $i; ?>) <?php echo $Question; ?></h1></br>
<h2> <input type="radio" name="radio" value="<?php echo $Oa; ?>"> A) <?php echo $Oa; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Ob; ?>"> B) <?php echo $Ob; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Oc; ?>"> C) <?php echo $Oc; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Od; ?>"> D) <?php echo $Od; ?></h2>
</div>
<hr></br></br>
<?php
}
}
?>
<button class="btn btn-large btn-primary" type="submit" name="btn-submit">Submit</button>
</form>
<?php
}
if(isset($_POST['btn-submit']))
{
echo $ufname = trim($_POST['radio']);
}
?>
Update your radio input name attribute just like this,
<div>
<h1><?php echo $i; ?>) <?php echo $Question; ?></h1></br>
<h2> <input type="radio" name="radio<?php echo $i; ?>?>" value="<?php echo $Oa; ?>"> A) <?php echo $Oa; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Ob; ?>"> B) <?php echo $Ob; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Oc; ?>"> C) <?php echo $Oc; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Od; ?>"> D) <?php echo $Od; ?></h2>
</div>
And for submitting:
<?php
}
$count = $i;
for($j = 1; $j <= $count; $j++){ if(isset($_POST['btn-submit'])) {
echo $ufname = trim($_POST['radio'.$j]);
}
}
?>
It will definitely solve your problem,
I want to get the value clicked in radio button.
This is my code:
<ul class="collapsible popout" data-collapsible="accordion" id="clicks">
<?php
foreach ($preguntas['preguntas'] as $row)
{
$opciones = $pregunta->opciones($row[0]);
?>
<li>
<div class="collapsible-header"><i class="material-icons">question_answer</i><?php echo utf8_encode($row[2]); ?></div>
<div class="collapsible-body">
<?php foreach ($opciones as $opcion){ ?>
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
<?php } ?>
</div>
<?php } ?>
</li>
</ul>
I need to get the value of this input id="opcion_..."
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
The problem is name and id is changing, it's not the same
Any idea?
Thank you.
JQuery selector for part of attribute $("[attribute^='value']") like this:
$("[id^='opcion_']").click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label><input name="pregunta_xxx" type="radio" id="opcion_123" value="123">radio for 123</label>
<label><input name="pregunta_xxx" type="radio" id="opcion_456" value="456">radio for 456</label>
I have this PHP form with radio buttons:
{
$agreements = jm_get_application_setting( 'application_agreements', '' );
if( empty( $agreements ) ) return;
$questions = explode( "\n", $agreements );
if (!empty($questions)):
foreach ($questions as $index => $question) :
?>
<div class="form-group">
<p><strong for="question-<?php echo sanitize_title($question); ?>" ><?php echo $question; ?></strong></p>
<div class="form-control-flat">
<label class="radio">
<input type="radio" name="_noo_application_answer_<?php echo $index; ?>" value="1" required><i></i><?php echo esc_html__('Do il consenso', 'noo'); ?>
</label>
</div>
<div class="form-control-flat">
<label class="radio">
<input type="radio" name="_noo_application_answer_<?php echo $index; ?>" value="0" required><i></i><?php echo esc_html__('Nego il consenso', 'noo'); ?>
</label>
</div>
<input type="hidden" name="_noo_questions[]" value="<?php echo esc_attr($question); ?>"/>
</div>
<?php
endforeach;
endif;
}
How can I make only one answer required to validate the form?
I mean.. Just the first MUST be selected, but I also need to show the second one for "legal reasons".
As is, this form accepts both choices as acceptable.
You can set a variable for the condition, and change it's value at the end of first iteration.
if (!empty($questions)):
$first = 1; // <----- HERE
foreach ($questions as $index => $question) :
?>
<div class="form-group">
<p><strong for="question-<?php echo sanitize_title($question); ?>" ><?php echo $question; ?></strong></p>
<div class="form-control-flat">
<label class="radio">
<input type="radio"
name="_noo_application_answer_<?php echo $index; ?>"
value="1"
<?php echo $first ? "required" : ""; ?>> // <----- HERE
<i></i><?php echo esc_html__('Do il consenso', 'noo'); ?>
</label>
</div>
<div class="form-control-flat">
<label class="radio">
<input type="radio"
name="_noo_application_answer_<?php echo $index; ?>"
value="0"
<?php echo $first ? "required" : ""; ?>> // <----- HERE
<i></i><?php echo esc_html__('Nego il consenso', 'noo'); ?>
</label>
</div>
<input type="hidden" name="_noo_questions[]" value="<?php echo esc_attr($question); ?>"/>
</div>
<?php
$first = 0; // <----- HERE
endforeach;
endif;
I've succesfully managed to add custom fields to the customer. However I need these fields to show up in onepage checkout.
I've overridden Mage_Customer_Block_Widget_Name and created my own customer/widget/name.phtml, added the attributes in the sql/xxx_setup/installer-x.y.z.php (added them to adminhtml_customer, customer_account_edit, checkout_register and customer_account_create) and they work fine in the admin site, however they just wont work on the checkout form. The field shows up, but it has the wrong value and no label.
I'm clueless why does it work in the customer registration form but doesn't in the checkout.
The installer code to add the attribute is:
$attributes = array(
'lastname2' => array(
'frontend_label'=>'Apellido Materno',
'label' => 'Apellido Materno',
'input' => 'text',
'type' => 'varchar',
//System = False and visible true = Show in 'customer_account_create', 'customer_account_edit', 'checkout_register'
'system'=>true,
'visible'=>true, //Watch out!! Only visible fields get processed by the form controllers!!!
'user_defined'=>false,
'used_in_forms' => array('adminhtml_customer', 'customer_account_edit', 'checkout_register','customer_account_create'),
'required' => 0,
'position' =>69
));
foreach($attributes as $attribute_code=>$definition)
{
$installer->addAttribute('customer', $attribute_code, $definition);
/**
* #var Mage_Eav_Model_Config
*/
Mage::getSingleton('eav/config')
->getAttribute('customer', $attribute_code)
->setData('used_in_forms',$definition['used_in_forms'])
->save();
}
The code in name.phtml is
<div class="<?php echo $this->getContainerClassName()?>">
<?php if ($this->showPrefix()): ?>
<div class="field name-prefix">
<label for="<?php echo $this->getFieldId('prefix')?>"<?php if ($this->isPrefixRequired()) echo ' class="required"' ?>><?php if ($this->isPrefixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('prefix') ?></label>
<div class="input-box">
<?php if ($this->getPrefixOptions() === false): ?>
<input type="text" id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getPrefix()) ?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?> />
<?php else: ?>
<select id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?>>
<?php foreach ($this->getPrefixOptions() as $_option): ?>
<option value="<?php echo $_option?>"<?php if ($this->getObject()->getPrefix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<div class="field name-firstname">
<label for="<?php echo $this->getFieldId('firstname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('firstname') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo $this->getFieldName('firstname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>" title="<?php echo $this->getStoreLabel('firstname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php if ($this->showMiddlename()): ?>
<?php $isMiddlenameRequired = $this->isMiddlenameRequired(); ?>
<div class="field name-middlename">
<label for="<?php echo $this->getFieldId('middlename')?>"<?php echo $isMiddlenameRequired ? ' class="required"' : '' ?>><?php echo $isMiddlenameRequired ? '<em>*</em>' : '' ?><?php echo $this->getStoreLabel('middlename') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('middlename')?>" name="<?php echo $this->getFieldName('middlename')?>" value="<?php echo $this->escapeHtml($this->getObject()->getMiddlename()) ?>" title="<?php echo $this->getStoreLabel('middlename') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('middlename') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php endif; ?>
<div class="field name-lastname">
<label for="<?php echo $this->getFieldId('lastname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('lastname') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('lastname')?>" name="<?php echo $this->getFieldName('lastname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname()) ?>" title="<?php echo $this->getStoreLabel('lastname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<div class="field name-lastname">
<label for="<?php echo $this->getFieldId('lastname2')?>"><?php echo $this->getStoreLabel('lastname2') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('lastname2')?>" name="<?php echo $this->getFieldName('lastname2')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname2()) ?>" title="<?php echo $this->getStoreLabel('lastname2') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname2') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php if ($this->showSuffix()): ?>
<div class="field name-suffix">
<label for="<?php echo $this->getFieldId('suffix')?>"<?php if ($this->isSuffixRequired()) echo ' class="required"' ?>><?php if ($this->isSuffixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('suffix') ?></label>
<div class="input-box">
<?php if ($this->getSuffixOptions() === false): ?>
<input type="text" id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getSuffix()) ?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?> />
<?php else: ?>
<select id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?>>
<?php foreach ($this->getSuffixOptions() as $_option): ?>
<option value="<?php echo $_option?>"<?php if ($this->getObject()->getSuffix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
This fields in the checkout you are talking about are address attributes, not customer attributes. So you need to load them differently. For registered users you could use Mage::getSingleton('customer/session')->getCustomer()->getLastname2() but it won't be saved to your address, as there is no attribute yet.
Depending on where you want lastname2 to be accessible you can create corresponding attributes for the entities customer_address,quote_address and order_address. They are created the same way you did for customer with
$installer->addAttribute($entityName, $attribute_code, $definition);
But that's not all. For the attributes to be converted correctly you need to set up conversion rules in your module config.xml. See for example the configuration of Mage_Sales.
In the global node there is a node fieldsets with the corresponding rules. There is a node customer_address to convert address attributes to quote address attributes. In sales_convert_quote there are rules to convert this attributes to order attributes.
So for your attributes to be accessible in all you config should look like this:
<global>
<fieldsets>
<customer_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastname2>
</customer_address>
<sales_copy_order_billing_address>
<lastname2>
<to_order>*</to_order>
</lastname2>
<sales_copy_order_billing_address>
<sales_copy_order_shipping_address>
<lastname2>
<to_order>*</to_order>
</lastname2>
</sales_copy_order_shipping_address>
<sales_convert_quote_address>
<lastname2>
<to_order_address>*</to_order_address>
<to_customer_address>*</to_customer_address>
</lastname2>
</sales_convert_quote_address>
<sales_convert_order_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastnam2e>
<sales_convert_order_address>
<customer_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastname2>
</customer_address>
</fieldsets>
</global>