Writing this PHP code in OO [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a PHP function which I want to convert from procedural into OOP but I am confused on how to do it. Please help.
Below is my original PHP code.
function smw_admin() {
global $smw, $shortname, $options;
$i=0;
if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$smw.' settings saved.</strong></p></div>';
if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$smw.' settings reset.</strong></p></div>';
?>
<div class="wrap rm_wrap">
<h2><?php echo $smw; ?></h2>
</div>
<form method="post">
<div class="wrap rm_wrap">
<div class="rm_opts">
<?php foreach ($options as $value) {
switch ( $value['type'] ) {
case "open":
?>
<?php break;
case "close":
?>
</div>
</div>
<br />
<?php break;
case "title":
?>
<p>To easily use the <?php echo $smw;?>, you can use the menu below.</p>
<?php break;
case 'text':
?>
<div class="rm_input rm_text">
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
<input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?>" />
<small><?php echo $value['desc']; ?></small>
<div class="clearfix"></div>
</div>
<?php
break;
case 'textarea':
?>
<div class="rm_input rm_textarea">
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
<textarea name="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?>
</textarea>
<small><?php echo $value['desc']; ?></small>
<div class="clearfix"></div>
</div>
<?php
break;
case 'select':
?>
<div class="rm_input rm_select">
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
<?php foreach ($value['options'] as $option) { ?>
<option <?php if (get_settings( $value['id'] ) == $option) { echo 'selected="selected"'; } ?>><?php echo $option; ?></option>
<?php } ?>
</select>
<small><?php echo $value['desc']; ?></small>
<div class="clearfix"></div>
</div>
<?php
break;
case "checkbox":
?>
<div class="rm_input rm_checkbox">
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
<?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>
<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
<small><?php echo $value['desc']; ?></small>
<div class="clearfix"></div>
</div>
<?php break;
case "section":
$i++;
?>
<div class="rm_section">
<div class="rm_title">
<h3><img src="<?php echo plugin_dir_url(__FILE__);?>/assets/images/trans.png" class="inactive" alt="""><?php echo $value['name']; ?></h3>
<span class="submit">
<input name="save<?php echo $i; ?>" type="submit" value="Save changes" />
</span>
<div class="clearfix"></div>
</div>
<div class="rm_options">
<?php break;
}
}
?>
<input type="hidden" name="action" value="save" />
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</div>
<?php
}
Here is OOP I wrote,
class Admin_Option{
public function smw_admin() {
$i=0;
if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$this->smw.' settings saved.</strong></p></div>';
if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$this->smw.' settings reset.</strong></p></div>';
?>
<div class="wrap rm_wrap">
<h2><?php echo $this->smw; ?></h2>
</div>
<form method="post">
<div class="wrap rm_wrap">
<div class="rm_opts">
<?php foreach ($this->options as $value) {
switch ( $value['type'] ) {
case "open":
?>
<?php break;
case "close":
?>
</div>
</div>
<br />
<?php break;
case "title":
?>
<p>To easily use the <?php echo $this->smw;?>, you can use the menu below.</p>
<?php break;
case 'text':
?>
<div class="rm_input rm_text">
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
<input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?>" />
<small><?php echo $value['desc']; ?></small>
<div class="clearfix"></div>
</div>
<?php
break;
case 'textarea':
?>
<div class="rm_input rm_textarea">
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
<textarea name="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?></textarea>
<small><?php echo $value['desc']; ?></small>
<div class="clearfix"></div>
</div>
<?php
break;
case 'select':
?>
<div class="rm_input rm_select">
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
<?php foreach ($value['options'] as $option) { ?>
<option <?php if (get_settings( $value['id'] ) == $option) { echo 'selected="selected"'; } ?>><?php echo $option; ?></option>
<?php } ?>
</select>
<small><?php echo $value['desc']; ?></small>
<div class="clearfix"></div>
</div>
<?php
break;
case "checkbox":
?>
<div class="rm_input rm_checkbox">
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
<?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>
<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
<small><?php echo $value['desc']; ?></small>
<div class="clearfix"></div>
</div>
<?php break;
case "section":
$i++;
?>
<div class="rm_section">
<div class="rm_title">
<h3><img src="<?php echo plugin_dir_url(__FILE__);?>/assets/images/trans.png" class="inactive" alt="" /><?php echo $value['name']; ?></h3>
<span class="submit">
<input name="save<?php echo $i; ?>" type="submit" value="Save changes" />
</span>
<div class="clearfix"></div>
</div>
<div class="rm_options">
<?php break;
}
}
?>
<input type="hidden" name="action" value="save" />
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</div>
<?php
}
}
Does it make sense? Am I going in the right way? Is there any other good way to do it?
Thanks

Before you consider of moving that code to another paradigm (OO) you must first clean it up, so that it can show its true colors, what it does, what it does good (work) and what it does wrong (bugs).
So we must first refactor the code. For example:
Get rid of bugs:
you open <div>s outside of the foreach loop and close them inside the loop and only in case "close". Now since that behavior is controlled by the data structure of $options probably you are feeding the loop with a 'close' value at the end of the data structure and thus that possible error never manifests, but it can and the whole thing makes your code hard to read. Move the closing of the <div>s outside the loop where they must be since we must always close them regardless of the data structure.
Get rid of stale code:
Now the above change results in the 'close' case doing nothing, and you have another case that was doing nothing the 'open' case. Those two cases must be removed altogether - they serve no purpose other than reminding us that somewhere we have a data structure that can have an open and close value.
Gather your inputs:
See what your inputs are and move them to the start of the code blocks so that you can see them in a glance. Give them meaningful names, for example $i probably is counting section numbers so name it $sectionNumber. Also you see now in a specific point that you use an asset according to __FILE__, later when you move the code file to another relative location you will change it easily.
Also in the department of stale code you also have an unused variable, $shortname. We are dropping it also.
Divide the logic from the presentation:
No I don't hint at anything like MVC just get the presentation that is more than one lines out of the way so that you can see the logic of your code. Just make them into functions - even if are going to be used only from one point in the code - and move them out of the way. Later when you have cleaned out the logic you will probably make a proper template that you will feed with the data it needs etc.
see what changes and what remains the same etc
Now all of the above result in the following which is way easier to read:
<?php
function smw_admin() {
global $smw, $options;
$saved = $_REQUEST['saved'];
$reset = $_REQUEST['reset'];
$imageTrans = plugin_dir_url(__FILE__) . '/assets/images/trans.png';
$sectionNumber=0;
if ($saved) echo '<div id="message" class="updated fade"><p><strong>'.$smw.' settings saved.</strong></p></div>';
if ($reset) echo '<div id="message" class="updated fade"><p><strong>'.$smw.' settings reset.</strong></p></div>';
?>
<div class="wrap rm_wrap">
<h2><?php echo $smw; ?></h2>
</div>
<form method="post">
<div class="wrap rm_wrap">
<div class="rm_opts">
<?php
foreach ($options as $value) {
switch ( $value['type'] ) {
case "open":
break;
case "close":
break;
case "title":
echo "<p>To easily use the $smw, you can use the menu below.</p>";
break;
case 'text':
render_div_rm_input_rm_text($value);
break;
case 'textarea':
render_rm_input_rm_textarea($value);
break;
case 'select':
render_rm_input_rm_select($value);
break;
case "checkbox":
render_rm_input_rm_checkbox($value);
break;
case "section":
$sectionNumber++;
render_rm_section($imageTrans, $value, $sectionNumber);
break;
}
}
?>
</div>
</div>
<br />
<input type="hidden" name="action" value="save" />
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</div>
<?php
}
and various functions like:
function render_rm_section($imageTrans, $value, $i)
{
?>
<div class="rm_section">
<div class="rm_title">
<h3><img src="<?php echo $imageTrans; ?>" class="inactive" alt="""><?php echo $value['name']; ?></h3>
<span class="submit">
<input name="save<?php echo $i; ?>" type="submit" value="Save changes"/>
</span>
<div class="clearfix"></div>
</div>
<div class="rm_options">
<?php
}
at that point you could start the OO way - although you should refactor it more for example by not echoing the result directly but instead getting the result as a return value, consolidating more the code base etc.
A 1st phase example could be:
Class SmwPage {
private options;
private $smw;
private $imageTrans;
function __construct($options, $smw) {
$this->options = $options;
$this->smw = $smw;
$this->imageTrans = plugin_dir_url(__FILE__) . '/assets/images/trans.png';
}
function render() {
// render the beggining
// the same code as before only we now operate with $this-> at the class member variables (properties)
foreach ($this->options as $value) {
// switch statement here
}
// render the end
}
with a usage of:
$smwPage = new SmwPage($options, $smw);
$smwPage->render();
Of course the above class would need to change
every time you would need to add a new option and
every time we need to change how an option is rendered
so in the next step you will want to change the data structure to a collection of objects that each knows how to render() itself. Then the render of SmwPage would be only a loop through the collection of those objects and a call to their render() method.

Related

get value of radio button using jQuery and php

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>

PHP form with radio buttons - make only one answer required, second optional

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;

foreach inside of foreach

I have this code, which is very long, sorry.
<ul id="org" style="display:none">
<?php foreach ($note1 as $item1) { ?>
<li>
<?php echo $item1->Node_Id; ?>
<?php foreach ($noteLevel1 as $item2) { ?>
<p style="font-size:15px;padding:10px 5px 10px 0px"><?php echo $item2->LabelNote; ?></p>
<div class="SmallBox" id="<?php echo $item2->DetailNode_id; ?>" style="margin-bottom:10px;">
<input type="text" value="<?php if(empty($item2->Realisasi)) { echo ''; }else{ echo number_format($item2->Realisasi,0,",","."); } ?>" class="TextInput" />
<input type="text" value="<?php if(empty($item2->Target)) { echo ''; }else{ echo number_format($item2->Target,0,",","."); } ?>" class="TextInput" />
<input type="text" value="<?php if(!empty($item2->PercentRealisasi)) { echo $item2->PercentRealisasi.'%'; }else{ echo '0%'; } ?>" class="PercentInput" />
<img class="circles4" src='http://localhost/CST/public/img/green.png' onload="this.onload=null; this.src=GetStatusImage(<?php echo $item2->PercentRealisasi; ?>);" />
</div >
<?php } ?>
<div class="clearDiv"></div>
<?php foreach ($note2 as $item3) { ?>
<?php echo $item3->Node_Id.'T'.$item3->ParentNode; ?>
<ul>
<?php foreach ($noteLevel2 as $item4) { ?>
<li>
<p class="NodeLabel"><?php echo $item4->LabelNote; ?></p>
<div id="<?php echo $item4->DetailNode_id; ?>" class="SmallBox">
<center>
<input type="text" value="<?php if(empty($item4->Realisasi)) { echo ''; }else{ echo number_format($item4->Realisasi,0,",",".");} ?>" class="TextInput" readonly="true"/>
<input type="text" value="<?php if(empty($item4->Target)) { echo ''; }else{ echo number_format($item4->Target,0,",","."); } ?>" class="TextInput" readonly="true"/>
<input type="text" value="<?php if(!empty($item4->PercentRealisasi)) { echo $item4->PercentRealisasi.'%'; }else{ echo '0%'; } ?>" class="PercentInput" readonly="true"/>
<img class="circles" src='http://localhost/CST/public/img/green.png' onload="this.onload=null; this.src=GetStatusImage(<?php echo $item4->PercentRealisasi; ?>);" />
</center>
</div>
<?php foreach ($note3 as $item5) { ?>
<?php echo $item5->Node_Id.'T'.$item5->ParentNode; ?>
<ul>
<?php foreach ($noteLevel3 as $item6) { ?>
<li>
<p class="NodeLabel"><?php echo $item6->LabelNote; ?></p>
<div id="<?php echo $item6->DetailNode_id; ?>" class="SmallBox">
<center>
<input type="text" value="<?php if(empty($item6->Realisasi)) { echo ''; }else{ echo number_format($item6->Realisasi,0,",",".");} ?>" class="TextInput" readonly="true"/>
<input type="text" value="<?php if(empty($item6->Target)) { echo ''; }else{ echo number_format($item6->Target,0,",","."); } ?>" class="TextInput" readonly="true"/>
<input type="text" value="<?php if(!empty($item6->PercentRealisasi)) { echo $item6->PercentRealisasi.'%'; }else{ echo '0%'; } ?>" class="PercentInput" readonly="true"/>
<img class="circles" src='http://localhost/CST/public/img/green.png' onload="this.onload=null; this.src=GetStatusImage(<?php echo $item6->PercentRealisasi; ?>);" />
</center>
</div>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
You can see the table in this picture:
The result should be like this:
I want node_id to appear when node_id is equal with parent_node. But when I try to add code like this:
<?php foreach ($note3 as $item5) { ?>
**<?php if($item5->ParentNode == $item3->Node_Id){ ?>**
<?php echo $item5->Node_Id.'T'.$item5->ParentNode; ?>
<ul>
<?php foreach ($noteLevel3 as $item6) { ?>
<li>
<p class="NodeLabel"><?php echo $item6->LabelNote; ?></p>
<div id="<?php echo $item6->DetailNode_id; ?>" class="SmallBox">
<center>
<input type="text" value="<?php if(empty($item6->Realisasi)) { echo ''; }else{ echo number_format($item6->Realisasi,0,",",".");} ?>" class="TextInput" readonly="true"/>
<input type="text" value="<?php if(empty($item6->Target)) { echo ''; }else{ echo number_format($item6->Target,0,",","."); } ?>" class="TextInput" readonly="true"/>
<input type="text" value="<?php if(!empty($item6->PercentRealisasi)) { echo $item6->PercentRealisasi.'%'; }else{ echo '0%'; } ?>" class="PercentInput" readonly="true"/>
<img class="circles" src='http://localhost/CST/public/img/green.png' onload="this.onload=null; this.src=GetStatusImage(<?php echo $item6->PercentRealisasi; ?>);" />
</center>
</div>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
And the result should be like this:
It's like I just loop with the first index of the array. How can I fix it?

double serialize function checkbook in php

I have 3 checkbook with 3 option answer every checkbook. I'm saving every checkbook using serialize, but I can't save the data if the check book has 1 more answer.
<?php if( ! empty($questiontype)): ?>
<label>Answer Type</label><br>
<label>A</label>
<div class="form-group">
<?php foreach($questiontype as $key => $val): ?>
<label class="checkbox-inline" >
<input name="meta_answertype[][]" value="<?php echo $key; ?>" type="checkbox"<?php if($key == #$meta['answertype'][0][0]) echo 'checked'; ?> /> <?php echo $val; ?>
</label>
<?php endforeach; ?>
</div>
<label>B</label>
<div class="form-group">
<?php foreach($questiontype as $key => $val): ?>
<label class="checkbox-inline">
<input name="meta_answertype[][]" value="<?php echo $key; ?>" type="checkbox"<?php if($key == #$meta['answertype'][1][1]) echo 'checked'; ?> /> <?php echo $val; ?>
</label>
<?php endforeach; ?>
</div>
<label>C</label>
<div class="form-group">
<?php foreach($questiontype as $key => $val): ?>
<label class="checkbox-inline">
<input name="meta_answertype[][]" value="<?php echo $key; ?>" type="checkbox"<?php if($key == #$meta['answertype'][2][2]) echo 'checked'; ?> /> <?php echo $val; ?>
</label>
<?php endforeach; ?>
</div>
<?php endif; ?>

isset multiple check boxes in php from mysql database

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>

Categories