I have a form that consists of a table with four data cells for comparison in it and also each column has a check box. When I click on the checkbox and submit the form then I want to display only the checked tables in new page. How can I do that? Can anyone help me with working example?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comparision of Printers</title>
</head>
<body>
<form action="newpage.php" method="get">
<table width="1023" border="1">
<tr>
<td width="193"></td>
<td class="p1" width="113"><img src="images/upmini.png"/>
<label>
<input type="checkbox" name="p[]" value="Printer1" id="CheckboxGroup1_0" />
Printer1</label>
</td>
<td class="p2" width="67"><img src="images/upmini.png"/>
<label>
<input type="checkbox" name="p[]" value="Printer2" id="CheckboxGroup1_1" />
Printer2</label></td>
<td class="p3" width="67"><img src="images/upmini.png"/><label>
<input type="checkbox" name="p[]" value="Printer3" id="CheckboxGroup1_2" />
Printer3</label></td>
<td class="p4" width="67"><img src="images/upmini.png"/><label>
<input type="checkbox" name="p[]" value="Pritner4" id="CheckboxGroup1_3" />
Printer4</label></td>
</tr>
<tr>
<td>Title</td>
<td class="p1" >Printer1</td>
<td class="p2" >Printer2</td>
<td class="p3" >Printer3</td>
<td class="p4" >Printer4</td>
</tr>
<tr>
<td>Manufacturer</td>
<td class="p1" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
<td class="p2" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
<td class="p3" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
<td class="p4" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
</tr>
</table>
<input type="submit" name="compare" value="compare" />
</form>
</body></html>
I think you should use POST instead of GET and store your printer and manufacturer information in a array. i use a 2D array to store the information to make it easy. and get the key values of this array in GET REQUEST and than print only those which match the keys. very simple..
this is full functional code ( changed little bit )
Comparision of Printers
</head>
<body>
// store your infomation in a arrya and use KEY same as in your form checkbox name
<?php
$printers = array(
'printer1' => array(
'title' => 'printer1',
'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 1'
),
'printer3' => array(
'title' => 'printer2',
'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 2'
),
'printer2' => array(
'title' => 'printer3',
'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 3'
),
'printer4' => array(
'title' => 'printer4',
'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 4'
)
)
?>
//if form submited then print the submited index only
<?php if(isset($_GET['p'])):?>
<?php $printerIndex = $_GET['p'];?>
<table width="1023" border="1">
<td>Title</td>
<?php foreach($printerIndex as $index): ?>
<td class = "p1" ><?php echo $printers[$index]['title']; ?></td>
<?php endforeach; ?>
</tr>
<tr>
<td>Manufacturer</td>
<?php foreach($printerIndex as $index): ?>
<td class = "p1" ><?php echo $printers[$index]['manufacturer']; ?></td>
<?php endforeach; ?>
</tr>
</table>
<?php endif; ?>
//make the table
<?php if(!isset($_GET['p'])): ?>
<form action="" method="get">
<table width="1023" border="1">
<tr>
<td width="193"></td>
<?php foreach($printers as $printer ): ?>
<td class="p1" width="113"><img src="images/upmini.png"/>
<label> <?php echo $printer['title']; ?></label>
<input type="checkbox" name="p[]" value="<?php echo $printer['title']; ?>" id="CheckboxGroup1_0" />
</td>
<?php endforeach; ?>
</tr>
<tr>
<td>Title</td>
<?php foreach($printers as $printer): ?>
<td class = "p1" ><?php echo $printer['title']; ?></td>
<?php endforeach; ?>
</tr>
<tr>
<td>Manufacturer</td>
<?php foreach($printers as $printer): ?>
<td class = "p1" ><?php echo $printer['manufacturer']; ?></td>
<?php endforeach; ?>
</tr>
</table>
<input type="submit" name="compare" value="compare" />
</form>
<?php endif; ?>
</body></html>
The checked printers are stored as array p in the $_GET variable. Eg. checking printer 1 and 3 in your example will show the following result:
<?php var_dump($_GET) ?>
array
'p' =>
array
0 => string 'Printer1' (length=8)
1 => string 'Printer3' (length=8)
'compare' => string 'compare' (length=7)
You have only ONE table with several TDs.
So within newpage.php you can do sth like:
if (isset($_GET['P']))
{
$P = $_GET['P'];
for ($i=0;$i<sizeof($P);$i++)
{
// Show what you like, e.g.
echo "Printer".$P[$i]."<br />";
}
}
Is it what you've meant?
Related
I'm trying to insert data into an array, I want it to add the new data without replacing the previous data
I'm trying to use array_push but it still replaces the previous data.
<?php
if (isset ($_POST['submit']; )) {
$num = 0.5;
$data[] = array(
'no1' => $_POST['no1'],
'no2' => $_POST['no2'],
'no3' => $_POST['no3']
);
array_push($data, array(
'no1' => $_POST['no1'],
'no2' => $_POST['no2'],
'no3' => $_POST['no3']
));
}
?>
and this is the form
<h4>Add data</h4>
<form class="" action="" method="post">
<ul>
<li>
<label for="ni1">No 1 :</label>
<input type="number" id="no1" name="no1" value="">
</li>
<li>
<label for="ni1">No 2 :</label>
<input type="number" id="no2" name="no2" value="">
</li>
<li>
<label for="ni1">No 3 :</label>
<input type="number" id="no3" name="no3" value="">
</li>
</ul>
<button type="submit" name="submit">ADD</button>
</form>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>No 1</td>
<td>No 2</td>
<td>No 3</td>
</tr>
<?php foreach ($data as $n): ?>
<tr>
<td><?= $n['no1']* $num ?></td>
<td><?= $n['no2']* $num ?></td>
<td><?= $n['no3']* $num ?></td>
</tr>
<?php endforeach; ?>
</table>
I expect the new data inserted not replacing the previous data and showed into the table
You can use Push like this function for example:
<?php
$dataArray = array('Mathematics','Physics');
array_push($dataArray,'Chemistry','Biology');
print_r($dataArray);
?>
output:
Array ([0] => Mathematics [1] => Physics [2] => Chemistry [3] => Biology)
Need assistance with following code. Value for "customer_id" is not posting after hitting submit for the first generated row only. the second and following work as intended.
Other pages use this same format for deleting, but do not redirect to another page like this one does. No issues with deleting.
<html>
<!-- the body section -->
<body>
<main>
<section>
<!-- display a table of customers -->
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<?php foreach($customers as $cus) : ?>
<tr>
<td><?php echo $cus['firstName'].' '.$cus['lastName']; ?></td>
<td><?php echo $cus['email']; ?></td>
<td class="right"><?php echo $cus['city']; ?></td>
<td><form action="view_and_update.php" method="post" id="search">
<input type="text" name="customer_id" value="<?php echo $cus['customerID']; ?>">
<input name="submit" type="submit" value="Select">
<?php include "redirect.php";?>
</form></td>
</tr>
<?php endforeach; ?>
</table>
</section>
</main>
</body>
</html>
redirect.php looks like this:
<?php
if(isset($_POST["submit"])){
// To redirect form on a particular page
header("Location: view_and_update.php");
}
?>
Any assistance is greatly appreciated. This is for an assignment, and we are using the languages as specified with XAMPP/myphpadmin. No problem retrieving, editing, adding, or deleting data in any other circumstance. This is the only problem page.
By using the following example code I got the correct post to view_and_update.php
It has to be something else, it is not the code.
<?php $customers = [
['firstName' => 'Alice', 'lastName' => 'Al', 'email' => 'alison#domain.com', 'city' => 'MyCity', 'customerID' => 123],
['firstName' => 'Bob', 'lastName' => 'Bo', 'email' => 'bob#domain.com', 'city' => 'MyCity', 'customerID' => 124]
]; ?>
<html>
<!-- the body section -->
<body>
<main>
<section>
<!-- display a table of customers -->
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<?php foreach($customers as $cus) : ?>
<tr>
<td><?php echo $cus['firstName'].' '.$cus['lastName']; ?></td>
<td><?php echo $cus['email']; ?></td>
<td class="right"><?php echo $cus['city']; ?></td>
<td><form action="view_and_update.php" method="post" id="search">
<input type="text" name="customer_id" value="<?php echo $cus['customerID']; ?>">
<input name="submit" type="submit" value="Select">
</form></td>
</tr>
<?php endforeach; ?>
</table>
</section>
</main>
</body>
</html>
I am working on the settings page for a plugin I am writing. There is a form field that let's the user select which currency symbol they want to use when displaying prices. I have created a function that easily displays select menus and determines which option should be selected based on the option stored in the database. For some reason, nothing is being selected in the select menu of the form.
This is the plugin code for this page...
<?php
function hc_options() {
?>
<div class="wrap">
<h2>HeadChef Settings</h2>
<form method="post" action="options.php">
<?php settings_fields( 'my-cool-plugin-settings-group' ); ?>
<?php do_settings_sections( 'my-cool-plugin-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Currency Format</th>
<td>
<?php echo select_menu('currency_format', array('1' => '$12.50', '2' => '$12,50'), esc_attr( get_option('currency_format'))); ?>
</td>
</tr>
<tr valign="top">
<th scope="row">Currency Character</th>
<td><?php
$options = array(
htmlspecialchars('º') => 'º Generic',
htmlspecialchars('$') => '$ Dollar',
htmlspecialchars('£') => '£ Pound',
htmlspecialchars('¥') => '¥ Yen',
htmlspecialchars('') => ' Euro',
htmlspecialchars('₱') => '₱ Peso'
);
echo select_menu('currency_prefix', $options, htmlspecialchars(esc_attr(get_option('currency_prefix')))); ?></td>
</tr>
<tr valign="top">
<th scope="row">Currency Suffix <br><small>(I.E. USD... Leave blank to disable.)</small></th>
<td><input type="text" name="currency_suffix" value="<?php echo esc_attr( get_option('currency_suffix') ); ?>" /></td>
</tr><tr>
<th scope="row">Enable Dish Ratings</th>
<td>
<fieldset><?php echo radio_buttons('Enable Dish Ratings', 'enable_dish_ratings', array('1' => 'Yes', '2' => 'No'), esc_attr(get_option('enable_dish_ratings'))); ?>
</td>
</tr><tr>
<th scope="row">Description Character Limit <small>Leave blank for no limit</small></th>
<td><input type="text" name="description_char_limit" value="<?php echo esc_attr( get_option('description_char_limit') ); ?>"></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
function select_menu($title, $options, $selected){
$output = '<select name="'.$title.'">\r\n';
if(count($options) > 0){
foreach($options as $val => $label){
$output .= '<option value="'.$val.'"';
if($val == $selected){
$output .= ' SELECTED';
}
$output .= '>'.$label."</option>\r\n";
}
}
$output .= "</select>\r\n";
return $output;
} ?>
Apparently the esc_attr function was screwing with it and the currency_prefix option was parsed as $ instead of $.
I have the following view to List all products, and in that you can delete or edit a particular product, onclick of delete or edit it will call the controller,
<?php
$this->load->helper('url');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>diluks eCommerce - Home</title>
<link href="<?php
echo base_url();
?>Public/scripts/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="<?php echo base_url();?>index.php/productlist_controller" method="post">
<div class="container">
<?php
include 'header-adminpanel.php';
?>
<div class="level3 clearfix">
<?php
include 'product-sidebar.php';
?>
<div class="body-content">
<div class="items">
<h2>Product List</h2>
<table class="CSSTable" cellspacing="0px" cellpadding="0px">
<tr>
<td>Item Code</td><td>Item Name</td><td>Item Price</td><td>Edit</td><td>Delete</td>
</tr>
<?php foreach($products as $row): ?>
<form method="POST" action="<?php echo base_url();?>index.php/productlist_controller">
<tr>
<td><?php echo $row->itemcode; ?></td><td><?php echo $row->itemname; ?></td><td>$<?php echo $row->itemprice; ?></td><td><center><button name="btn_edit" class="link-button" value="<?php echo $row->itemcode; ?>" type="submit">Edit</button></center></td><td><center><button name="btn_delete" class="link-button" value="<?php echo $row->itemcode; ?>" type="submit">Delete</button></center></td>
</tr>
</form>
<?php endforeach ?>
</table>
</div>
</div>
</div>
<div style="clear:both"></div>
<div class="level4">
<div class="footer-area">
<div class="lined-space"></div>
<div class="site-map" align="left">
<table>
<tr>
<td class="footer-text">About Us</td>
<td class="footer-text">Facebook</td>
</tr>
<tr>
<td class="footer-text">Contact Us</td>
<td class="footer-text">Twitter</td>
</tr>
<tr>
<td class="footer-text">FAQs</td>
<td class="footer-text">Terms & Conditions</td>
</tr>
<tr>
<td class="footer-text">Help</td>
</tr>
</table>
</div>
<div class="developer-info">
<a class="developers-text">Designed & Developed By Diluks Software Solutions.</a>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
So the controller to the above view look like belo
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Productlist_controller extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index()
{
if(isset($_POST["btn_delete"])){
$pid = $_POST["btn_delete"];
$this->load->model('product_model');
$result = $this->product_model->deleteProduct($pid);
if($result==true){
$data = array();
$this->load->model('product_model');
$data['products'] = $this->product_model->availableProductList();
$this->load->view('admin_product_list_view',$data);
}
else{
echo "Oops! Error occured..!";
}
}
else if(isset($_POST["btn_edit"])){
$pid = $_POST["btn_edit"];
$data = array();
$this->load->model('product_model');
$data['product'] = $this->product_model->readProduct($pid);
//$this->load->model('category_model');
//$data['categories'] = $this->category_model->getCategories();
$this->load->view('admin_product_edit_view', $data);
}
}
}
"admin_product_edit_view" which the user will be redirected onclick of edit for an particular item is as follows,
<?php
$this->load->helper('url');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>diluks eCommerce - Home</title>
<link href="<?php
echo base_url();
?>Public/scripts/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form enctype="multipart/form-data" action="<?php
echo base_url();
?>index.php/addproduct_controller" method="post">
<div class="container">
<?php
include 'header-adminpanel.php';
?>
<div class="level3 clearfix">
<?php
include 'product-sidebar.php';
?>
<div class="body-content">
<div class="items">
<h2>Edit Product</h2>
<?php foreach($product as $row){ ?>
<table>
<tr>
<td class="captions">Product Code</td>
<td><input name="txt_pcode" type="text" readonly="true" value="<?php echo $row->itemcode; ?>"/></td>
</tr>
<tr>
<td class="captions">Product Name</td>
<td><input name="txt_pname" type="text" size="40" value="<?php echo $row->itemname; ?>" /></td>
</tr>
<tr>
<td class="captions">Product Price</td>
<td><input name="txt_pprice" type="text" value="<?php echo $row->itemprice; ?>" /></td>
</tr>
<tr>
<td class="captions">Product Category</td>
<td><select name="txt_pcategory">
<?php
foreach($categories as $row)
{
echo '<option value="'.$row->catname.'">'.$row->catname.'</option>';
}
?>
</select></td>
</tr>
<tr>
<td class="captions">Product Description</td>
<td><textarea name="txt_pdesc" style="width:300px;height:100px;"><?php echo $row->itemdesc; ?></textarea></td>
</tr>
<tr>
<td class="captions">Product Image</td>
<td><input type="file" name="userfile" size="20" /></td>
</tr>
<tr>
<td class="captions">Product Options</td>
<td><input name="txt_poptions" size="40" type="text" /><a class="hint"> (Separate by a "," comma)</a></td>
</tr>
<tr><td><input name="btn_add" class="grey-button" type="submit" value="Update" /></td></tr>
</table>
<?php } ?>
<br />
</div>
</div>
</div>
<div style="clear:both"></div>
<div class="level4">
<div class="footer-area">
<div class="lined-space"></div>
<div class="site-map" align="left">
<table>
<tr>
<td class="footer-text">About Us</td>
<td class="footer-text">Facebook</td>
</tr>
<tr>
<td class="footer-text">Contact Us</td>
<td class="footer-text">Twitter</td>
</tr>
<tr>
<td class="footer-text">FAQs</td>
<td class="footer-text">Terms & Conditions</td>
</tr>
<tr>
<td class="footer-text">Help</td>
</tr>
</table>
</div>
<div class="developer-info">
<a class="developers-text">Designed & Developed By Diluks Software Solutions.</a>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
Now the problem is in the controller i need to load up categories (which I have commented) in order to show them in the edit view, but when I un-commented it, categories are loading, but Gives an Error in Item Description textarea saying
Message: Undefined property: stdClass::$itemdesc
With the category model loading line commented, it works fine except no categories will load up in the dropdownlist,Please someone suggest me a way to get rid of this.
Please use this in your view
if(isset($row->itemdesc)) echo $row->itemdesc;
I think this will solve your problem
I saw that you got an answer but i have a few suggestions for your code:
1. in your controller you load the model 3 times: you should load it once; you could do something like this:
if(!empty($_POST)){
$this->load->model('product_model');
if(isset($_POST["btn_delete"])){
//some code
}elseif(isset($_POST["btn_edit"])){
//some other code
}
}
2. in your views you load a helper: this should be loaded into the controller, right after the first verification from point 1.
3. in views you should check your variable for content, especially the ones that you are going to use in a loop, like $products
I am using following form but when I am submitting form without entering any mandatory fields, then in other input fields values are converting into garbage values.
<?php echo form_open_multipart($formAction); ?>
// form action is coming from controller
<div id="content">
<table border="0" cellpadding="0" cellspacing="5" width="100%" >
<tr>
<td>First Name<em>*</em></td>
<td>
<?php
$data = array('name' => 'firstName');
echo form_input($data, $firstName);
?>
</td>
</tr>
<tr>
<td>Middle Name<em>*</em></td>
<td>
<?php
$data = array('name' => 'middleName');
echo form_input($data, $middleName);
?>
</td>
</tr>
<tr>
<td>Last Name<em>*</em></td>
<td>
<?php
$data = array('name' => 'lastName');
echo form_input($data, $lastName);
?>
</td>
</tr>
<tr>
<td colspan="2">
<div class="formbuttons">
<?php echo form_submit('submit', "Save", "class='button'"); ?>
</div>
</td>
</tr>
</table>
<?php echo form_close(); ?>
If I enter "test's" in first name field and submit form without entering other mandatory fields then in First Name text box it is showing test's.
Try this..
echo form_input($data, html_entity_decode($firstName,ENT_QUOTES));
this should work.