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.
Related
learning PHP and have hit a wall early with passing data in an HTML form in to PHP. When I hit submit, the form page acts like it submits properly but when the processorder.php page opens, the data is not visible. When I do a dump on the page, I am able to see the values displayed but they are not showing in the script. Below is the code I'm using. I've searched online and at SO and feel like I've pretty much exhausted all options. Any assistance you can provide is much appreciated.
HTML:
<form action="processorder.php" method="POST">
<table>
<tr>
<td>Item</td>
<td>Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td><input type="text" name="tireqty" id="tireqty" size="3" /></td>
</tr>
<tr>
<td>Oil</td>
<td><input type="text" name="oilqty" id="oilqty" size="3" /></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td><input type="text" name="sparkqty" id="sparkqty" size="3" /></td>
</tr>
<tr>
<td colspan="2" text-align"2"><input type="submit" value="Submit Order">
</td>
</tr>
</table>
</form>
PHP:
<?php
var_dump( $_POST );
/*var_dump($GLOBALS);*/
$tireqty = $_POST['$tireqty'];
$oilqty = $_POST['$oilqty'];
$sparkqty = $_POST['$sparkqty'];
/*echo phpinfo();*/
?>
<h1 />Bob's Auto Parts
<h2>Order Results</h2>
<?php
/*
ini_set('display_errors',1);
error_reporting(E_ALL);
*/
echo "<p>Your Order is as Follows: </p>";
echo htmlspecialchars($tireqty).' tires<br />';
echo htmlspecialchars($oilqty).' bottles of oil<br />';
echo htmlspecialchars($sparkqty).' spark plugs<br />';
echo "<p>Order Processed at ";
echo date('H:i, jS F Y');
echo "</p>";
print_r($_POST);
/*var_dump($_REQUEST)*/
?>
Remove the $ of your $_POST methods. Change:
$tireqty = $_POST['$tireqty'];
$oilqty = $_POST['$oilqty'];
$sparkqty = $_POST['$sparkqty'];
to:
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
I am trying to process multiple arrays from a form, but I constantly face the issue where only the last field is submitted.
The form is echoing results (IP addresses and their rDNS settings) from an simple xml output and publishing these in an editable form and table.
<?php include_once("API.php");
$ips=array('xx.xx.xx.xx','xx.xx.xx.xx','xx.xx.xx.xx');
foreach($ips as $ip){
$API->GetRdns($ip);
$xml = simplexml_load_string($API);
}
foreach ($xml->RdnsDetails as $RdnsDetails) :?>
<tr>
<td><?php echo $ip; ?></td>
<td><input type="text" name="hostname" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="extra" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
<?php endforeach; ?>
When the submit button is clicked, the changes have to be submitted.
<button type="submit" name="submit">Submit</button>
<?php if (isset($_POST['submit'])){
foreach ($ips as $ip) {
$API->Sent($ip,$_POST[hostname],$_POST[extra])
}
?>
How do I get the values 'hostname' and 'extra' submitted for each of the IP's ($ip)?
You should try creating an array instead of a single value. Try this:
<tr>
<td><?php echo $ip; ?></td>
<td><input type="text" name="ip[<?php echo $ip; ?>]['hostname']" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="ip[<?php echo $ip; ?>]['extra']" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
You can catch this with:
<?php if (isset($_POST['submit'])){
foreach ($_POST['ip'] as $ip => $info) {
$API->Sent($value,$info['hostname'],$info['extra']);
}
?>
You should bind your submit button into a <form> to get the values
<form>
<?php foreach ($xml->RdnsDetails as $RdnsDetails) :?>
<tr>
<td><?php echo $ip; ?></td>
<td><input type="text" name="hostname" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="extra" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
<?php endforeach; ?>
<button type="submit" name="submit">Opslaan</button>
</form>
another problem is you missed '' here:
$API->Sent($value,$_POST['hostname'],$_POST['extra'])
a form by default will only send one value for each "name" entity in the form. Can you try adding the brackets to the [name] property of the inputs so it sends the values as an array ?
I think i understand a little more now, you need the association between the IP and the hostname/extra values. How about if you add the ip as a hidden form field
<?php foreach ($xml->RdnsDetails as $RdnsDetails) :?>
<tr>
<td><?php echo $ip; ?></td>
<td><input type="hidden" name="ips[]" value="<?php echo $ip; ?>"></td>
<td><input type="text" name="hostname[]" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="extra[]" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
<?php endforeach; ?>
then you can process in the backend something like this:
<?php
$ips = $_POST['ips'];
$hostnames = $_POST['hostname'];
$extras = $_POST['extras'];
for($i=0;$i<count($ips);$i++)
{
echo "IP: " . $ips[i] . ", Hostname: " . $hostnames[i] . ", Extra: " . $extras[$i];
}
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 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/
I'm trying to get the emails corresponding to the checkbox using the following codes. But, I'm not getting the correct checked emails in the new variable. Can anyone please check ??
<?php
include("connection.php");
$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM contacts WHERE username='$username'");
$num=mysql_num_rows($query);
$info=mysql_fetch_array($query);
$i=0;
$msg='';
?>
<table width="672" border="0">
<?php
$i=0;
while($info)
{
?>
<form action="compose.php" method="post">
<tr style="font-size:14px;">
<td width="21" bgcolor="#f2f2f2"> <input type="checkbox" name="add" onSelect="<?php $msg=$msg.$info['email'].", ";?>"/> </td>
<td width="229" bgcolor="#f2f2f2"> <?php echo $info['email']; ?> </td>
<td width="408" bgcolor="#f2f2f2"> <?php echo $info['name']; ?> </td>
</tr>
<?php
$info=mysql_fetch_array($query);
$i++;
}
$_SESSION['contacts']=$msg;
?>
<tr><td></td><td></td><td><br />
<input class="new-button" type="submit" value="Insert & Compose" name="submit" /></td>
</tr>
</form>
</table>
To get any value back for checkboxes they must have a value=. In your case you probably would want the value to be the according email address.
One problem with your code is using onSelect= instead of value=, and second you didn't print the actual value into the page. Rewrite it to:
<td width="21" bgcolor="#f2f2f2">
<input type="checkbox" name="add"
value="<?php print $info['email']; ?>"/> </td>
If you need the $msg variable to do something, assemble it after the output.
<input type="checkbox" name="add" value="<?php echo $msg.$info['email'];?>"/>
checkbox does not have onSelect event probobly you got value in mind and in PHP code you should echo and what .", " is for?