I want to let the user select one "row" to use to submit the with to request a report type. How can I put radio buttons in the first column of a table and whichever is selected is the active row that gets sent to the next page via the submit button?
I think Andreas is on the right track, but it's not as useful as it could be. This should be a bit better:
<?php
blah ...
echo <<<HTML
<form action="handler.php" action="post">
<table>
HTML;
foreach ($rows as $row)
{
$id = $row['id'];
$text = $row['text']; // escape this unless you know it's safe
echo <<<HTML
<tr>
<td><input type="radio" value="$id" name="theRadioButton" /></td>
<td><input type="text" name="textfield_$id" value="$text" /></td>
</tr>
HTML;
}
echo <<<HTML
</table>
</form>
HTML;
form handler:
<?php
$id = isset($_POST['theRadioButton']) ? $_POST['theRadioButton'] : null;
if ($id)
{
$textfield = $_POST["textfield_$id"];
}
?>
If you want to do it in pure PHP, i guess you could do this:
<form action="ascript.php" action="post">
<table>
<tr>
<td><input type="radio" value="row1" name="theRadioButton" /></td>
<td><input type="text" name="row1textfield" /></td>
</tr>
<tr>
<td><input type="radio" value="row2" name="theRadioButton" /></td>
<td><input type="text" name="row2textfield" /></td>
</tr>
<tr>
<td><input type="radio" value="row3" name="theRadioButton" /></td>
<td><input type="text" name="row3textfield" /></td>
</tr>
</table>
</form>
ascript.php
<?php
if ($_POST['theRadioButton'] == "row1") {
echo $_POST['row1textfield'];
// Handle row 1 ..
}
else if ($_POST['theRadioButton'] == "row2") {
echo $_POST['row2textfield'];
// Handle row 2 ..
}
else if ($_POST['theRadioButton'] == "row3") {
echo $_POST['row3textfield'];
// Handle row 3 ..
}
?>
However, if you're willing to use some jQuery, you could just name the textfields the same thing and disable the fields you're not going to use. Here's a fiddle: http://jsfiddle.net/rrvQu/1/
Related
I've got two tables: Subjects and Careers :
"Subjects" includes (id, careers_id (is the foreign key to the column "id" of the table Careers) subject, description, hours)
"Careers" includes (id,name,description)
I put a button which allows me to add a new subject. So when I click on it another page open. I need to add a slider/select which shows me the careers available in the table career. Take a look,I need something like this :
Here is my code to add a new subject (it works,but i dont know how to make the slider/select which fetch data from the table careers :/)
<?php include('connect.php');
$error="";
if(isset($_POST['btnsave']))
{
$carreras_id=$_POST['txtcarreras_id'];
$subject=$_POST['txtsubject'];
$descripcion=$_POST['txtdescripcion'];
$carga_horaria=$_POST['txtcarga_horaria'];
if($_POST['txtid']=="0")
{
$a_sql=mysql_query("INSERT INTO subjects VALUES('','$carreras_id','$subject','$descripcion','$carga_horaria')");
if($a_sql)
{
header("location:index.php");
}
}else{
echo "Actualizar";
}
}
?>
<h2 align="center">ADD NEW SUBJECT</h2>
<form method="Post">
<table align="center">
<tr>
<td>Career:</td>
<td>
<input type='text' name='txtcarreras_id' />
<input type="hidden" name="txtid" value="0" />
</td>
</tr>
<tr>
<td>Subject:</td>
<td>
<input type='text' name='txtsubject' />
</td>
</tr>
<tr>
<td>Description:</td>
<td>
<input type='text' name='txtdescripcion' />
</td>
</tr>
<tr>
<td>Hours:</td>
<td>
<input type='text' name='txtcarga_horaria' />
</td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' value=save name='btnsave' />
</td>
</tr>
</table>
</form>
I don't know what to do?
Hope you can help me!
Thanks!
To build your <option>s you would do something like this ->
$sql = mysql_query("SELECT * FROM Careers");
$options = "";
while($result = mysql_fetch_array($sql)){
$options .= "<option value='".$result['id']."'>".$result['name']."</option>";
}
(Note - you should update from mysql_ to MySQLi or PDO - MySQL: choosing an API )
Then you would change your <input> to a <select> with your $options ->
<tr>
<td>Career:</td>
<td><select name='txtcarreras_id'><?php echo $options; ?></select><input type="hidden" name="txtid" value="0" /></td>
</tr>
I'm trying to return to a page that is dynamically created from data stored in a database. However when I try to return to the main dynamic page it does not work. It should on submit redirect to the page that shares the yard id for the track. Instead it just gives me a failure message
Controller
public function update_track($yard_id)
{
$data = array(
'track_id'=>$this->input->post('track_id'),
'train_id'=>$this->input->post('train_id'),
'train_direction'=>$this->input->post('train_direction'),
'train_length' =>$this->input->post('train_length'),
'train_hpt'=>$this->input->post('train_hpt'),
'train_tons'=>$this->input->post('train_tons'),
'train_status'=>$this->input->post('train_status'),
);
$this->load->model('atisyard_model');
$this->atisyard_model->update_track($data);
if($this->db->affected_rows()>0)
{
redirect('/atis/'.$value->yard_id);
}
else
{
echo 'failure';
}
}
View
<?=form_open('atis/update_track/'); foreach ($record as $value) {?>
<h1>Edit Track:</h1>
<b><p>Track Name: </b><?php echo $value->track_no;?></br>
<b>Track Length: </b><?php echo $value->track_length;?>'</br></br></p>
<table cellpadding="5" border="0" align="center">
<input type="hidden" name="track_id" title="track_id" value="<?php echo $value->track_id;?>">
<tr>
<td>Lead Engine No. / Symbol / Work Order:</td>
<td><input type="text" name="train_id" id="train_id" size="28" maxlength="28" value="<?php echo $value->train_id;?>"></td>
</tr>
<tr>
<td>Train Direction:</td>
<td><?=form_dropdown('train_direction', $train_direction, $value->train_direction);?></td>
</tr>
<tr><td>Train Length:</td>
<td><input type="text" name="train_length" id="train_length" size="4" maxlength="6" value="<?php echo $value->train_length;?>"></td>
</tr>
<tr><td>HP/T:</td>
<td><input type="text" name="train_hpt" id="train_hpt" size="2" maxlength="5" value="<?php echo $value->train_hpt;?>"></td>
</tr>
<tr><td>Train Tons:</td>
<td><input type="text" name="train_tons" id="train_tons" size="4" maxlength="6" value="<?php echo $value->train_tons;?>"></td>
</tr>
<td>Train Status:</td>
<td> <?=form_dropdown('train_status', $train_status, $value->train_status);?></td>
</tr>
<tr>
<tr><td><button id="submitbtn" >Submit</button></td></tr>
</table>
Back to Yard
<?php } echo form_close();?>
Taking a second look at your code, would it be better to add the yard_id as a hidden field on your form:
<input type="hidden" name="yard_id" name ="<?=$value->yard_id; ?>" />
then change your redirect to:
redirect('/atis/'.$this->input->post('yard_id'));
If I understand this correctly and you are passing $yard_id into your update_track() function, then shouldn't this:
redirect('/atis/'.$value->yard_id);
Be just :
redirect('/atis/'.$yard_id);
Because I can't see where your getting $value from.
I am creating a page that would allow the user to select an existing address, or input a new one, here are my codes.
<table cellpadding="10px">
<tr>
<td><input type="radio" id="huhu" name="huhu" value="<?php echo $_SESSION['home_address']; ?>"></td><td><?php echo $_SESSION['home_address']; ?></td>
</tr>
<tr>
<td><input type="radio" id="huhu" name="huhu" value="New"></td><td><input type="text" placeholder="New Address" id="newAdd" name="newAdd" disabled></td>
</tr>
</table>
and here are my codes at the next page.
<?php
if(isset($_POST['newAdd'])){
$_SESSION['home_address'] = $_POST['newAdd'];
echo $_POST['newAdd']."<br>";
}
else{
$_SESSION['home_address'];
}
echo $_SESSION['home_address'];
?>
When i click on the existing address, it just deletes it. and does not store anything. but when i input a new on in the text area. it works.
I need to make it so that when the user clicks the address, the same address from the existing session displays.
please help. thank you.
I think you have missed session_start() method in your PHP file. Try to add the following code at the beginning of PHP file
if (!isset($_SESSION))session_start();
if your session info is correctly set.. this should work out.
<?php
session_start();
// for my testing....
$_SESSION['home_address'] = 'curr_session_address';
var_dump($_POST);
var_dump($_SESSION);
$s_addr = isset($_SESSION['home_address']) ? $_SESSION['home_address'] : '';
$p_addr = isset($_POST['newAdd']) ? $_POST['newAdd'] : '';
if ( !empty($p_addr) ) {
$_SESSION['home_address'] = $p_addr;
echo "new_address = $p_addr<br>";
}
else {
echo "session_address = $s_addr<br>";
}
?>
<form method='post' action='?'>
<table cellpadding="10px">
<tr>
<td><input type="radio" id="huhu" name="huhu" value="<?php echo $_SESSION['home_address']; ?>"></td>
<td><?php echo $_SESSION['home_address']; ?></td>
</tr>
<tr>
<td><input type="radio" id="huhu" name="huhu" value="New"></td>
<td><input type="text" placeholder="New Address" id="newAdd" name="newAdd"></td>
</tr>
</table>
<input type='submit' value='submit'>
</form>
Try this one.
if(empty($_POST['newAdd'])){
$_SESSION['home_address'] = $_POST['huhu'];
}
else if(!empty($_POST['newAdd'])){
$_SESSION['home_address'] = $_POST['newAdd'];
}
and i suggest that you dont use $_SESSION in your radio button page. it leads to complications and it will always be over written.
I need to pass back a large string of results to a form, so that the form can read those results from the URL and then populate the form with them. Problem is, the link ends up being:
&key=value&key=value ... until it can't process anymore (I assume a URL has a length limit?) resulting in my form not being able to fully populate. I need another way to pass values back to my form file.
VIEW.php file (basically just a table of values right as they are from the database, with the first column "id" being a link. When I click on "id", it goes back to my add.php(form page) and populates the form with the data matching that id)
<table border="0" cellpadding="0" cellspacing="0" id="table">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>MANUFACTURER</th>
<th>MODEL</th>
<th>DESCRIPTION</th>
<th>ON HAND</th>
<th>REORDER</th>
<th>COST</th>
<th>PRICE</th>
<th>SALE</th>
<th>DISCOUNT</th>
<th>DELETED</th>
<th></th>
</tr>
</thead>
<tbody>
<?php } ?>
<?php
// loop to fetch data
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>
<a href='molszewski1_a2_add.php'>$row[id]</a></td>";
echo "<td>$row[name]</td>";
echo "<td>$row[manufac]</td>";
echo "<td>$row[model]</td>";
echo "<td>$row[descrip]</td>";
echo "<td>$row[onhand]</td>";
echo "<td>$row[reorder]</td>";
echo "<td>$row[cost]</td>";
echo "<td>$row[price]</td>";
echo "<td>$row[sale]</td>";
echo "<td>$row[discont]</td>";
echo "<td>$row[deleted]</td>";
$status = "$row[deleted]";
echo "<td><a href='molszewski1_a2_delete.php?id=$row[id]&flag=$status&sort=$sort'>";
$status = "$row[deleted]";
if ($status == 'n') {
$flag = "restore";
echo "delete";
} else if ( $status == 'y') {
$flag = "delete";
echo "restore";
}
echo "</a></td>";
echo "</tr>";
} ?>
<?php { ?>
</tbody>
</table>
ADD.php (form page where the form is supposed to fetch the data and populate it)
<?php
// If no form has been submitted, present form
if (empty($_GET))
{
add_form();
}
// if a form has been submitted
else
{
// if form_validity() == 1, proceed to connect
if (form_validity() == 1)
{
// connect to mysql + database
connect();
$saleItem = "n";
$discountItem = "n";
if( array_key_exists( 'saleItem', $_GET ) && $_GET['saleItem'] == 'y' )
{ $saleItem = "y"; }
if( array_key_exists( 'discountItem', $_GET ) && $_GET['discountItem'] == 'y' )
{ $discountItem = "y"; }
// get values from form, insert into database
$sql=("INSERT INTO inventory (name,
manufac,
model,
descrip,
onhand,
reorder,
cost,
price,
sale,
discont,
deleted)
VALUES ('$_GET[itemName]',
'$_GET[manufacturer]',
'$_GET[model]',
'$_GET[description]',
'$_GET[numberOnHand]',
'$_GET[reorderLevel]',
'$_GET[cost]',
'$_GET[sellingPrice]',
'$saleItem',
'$discountItem', 'n')");
// if the query doesn't work, display error message
if (!(mysql_query($sql))) { die ("could not query: " . mysql_error()); }
add_form();
// redirect to view.php after form submission
// use php instead
echo "<meta http-equiv='REFRESH' content='0;url=molszewski1_a2_view.php'>";
}
else
{
// if form is not valid (form_validity returns 0), display error messages
add_form();
}
}
?>
FUNCTIONS.php (all my functions for stuff like the form)
<?php function page_navigation(){ ?>
<div class="center">
<input type="button" value="ADD" />
<input type="button" value="VIEW" />
<input type="button" value="VIEW DELETED" />
<input type="button" value="VIEW ACTIVE" />
<br />
<br />
</div>
<?php } ?>
<?php function add_form() { ?>
<form action="molszewski1_a2_add.php" method="get" id="form">
<table width="529px">
<tr>
<td>ITEM NAME</td>
<td><input name="itemName" size="30" type="text" value="<?php echo $_GET["itemName"] ?>"/></td>
</tr>
<tr>
<td>MANUFACTURER</td>
<td><input name="manufacturer" size="30" type="text" value="<?php echo $_GET["manufacturer"] ?>"/></td>
</tr>
<tr>
<td>MODEL</td>
<td><input name="model" size="30" type="text" value="<?php echo $_GET["model"] ?>"/></td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td><textarea name="description" rows="3" cols="20"><?php echo $_GET["description"] ?></textarea></td>
</tr>
<tr>
<td>ON HAND</td>
<td><input name="numberOnHand" size="30" type="text" value="<?php echo $_GET["numberOnHand"] ?>"/></td>
</tr>
<tr>
<td>REORDER LEVEL</td>
<td><input name="reorderLevel" size="30" type="text" value="<?php echo $_GET["reorderLevel"] ?>"/></td>
</tr>
<tr>
<td>COST</td>
<td><input name="cost" size="30" type="text" value="<?php echo $_GET["cost"] ?>"/></td>
</tr>
<tr>
<td>SELLING PRICE</td>
<td><input name="sellingPrice" size="30" type="text" value="<?php echo $_GET["sellingPrice"] ?>"/></td>
</tr>
<tr>
<td>SALE ITEM</td>
<td>
<input type="checkbox" name="saleItem" value="y" <?php if( isset( $_GET['saleItem'] ) ){ ?> checked="checked" <?php } ?> />
</td>
</tr>
<tr>
<td>DISCOUNTED ITEM</td>
<td>
<input type="checkbox" name="discountItem" value="y" <?php if( isset( $_GET['discountItem'] ) ){ ?> checked="checked" <?php } ?> />
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="save" name="submit" id="submit" /></td>
</tr>
</table>
</form>
<?php } ?>
Use method="post" and $_POST (instead of $_GET).
POST requests can be much larger than GET requests as GET requests are limited by the maximum length of a URL. POST requests are limited by the size of the max_post_size ini-value which is usually a few megabytes.
I've a doubt. I've 3 textboxes and each is having checkboxes next to it. I want to display
the values of only those textboxes whose respective checkboxes are clicked. Following is the attached HTML and PHP codes:
<html>
<head>
</head>
<body>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="a1" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Hostel"></td>
</tr>
<tr>
<td><input type="text" name="b1" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Transport"></td>
</tr>
<tr>
<td><input type="text" name="c1" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Food"></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
and below is the PHP part.
<?php
$a=$_POST['a1'];
$b=$_POST['b1'];
$c=$_POST['c1'];
$facilityArray = $_POST['facility'];
$facility = "";
if(count($facilityArray) > 0)
{
foreach($facilityArray as $fac)
{
$facility .= " " . $fac;
}
}
echo $facility; echo "<br>";
echo $a; echo "<br>";
echo $b; echo "<br>";
echo $c;
?>
With the help of following codes I am able to display all the values of checked checkboxes. I am also able to display the values of all the textboxes. But I actually want to display the values of only those textboxes whose respective checkboxes are clicked. I know it may be a very basic question but please help me grow in PHP. Thanks in advance... :(
Your textboxes should also be in an array post to achieve this.
To achieve this change the input lines as:
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
From php you'll be getting the posted textboxes in an array as:
$textbox=$_POST['textboxes'];
You should then loop through the checkboxes array and if the corresponding checkbox is "on" (clicked), then display the textboxes value. To do this you would also need a counter to make sure you are on the same array index for both checkboxes and textboxes:
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
I've also added a name to your submit button so you only check the form when it is submitted.
Your page should now look something like this:
<?php
if(isset($_POST['submit']))
{
$textbox=$_POST['textboxes'];
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
?>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td colspan="3"><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
UPDATE:
To make sure that the $_POST variable exists before assigning it to a variable we use the isset(). In your case just update the php segment as:
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['textboxes']))
{
$textbox=$_POST['textboxes'];
if(isset($_POST['facility']))
{
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
}
}
?>
Where the only changes are the addition of another two if statements that take a boolean flag from the isset() function according to whether the $_POST variable has been posted successfully
if(isset($_POST['textboxes']))
AND
if(isset($_POST['facility']))