Storing radio button data in a session [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i want to store data in a session so that i could use it on the next page.
Here is my code where the radio buttons are used.
<link rel="stylesheet" type="text/css" href="payout.css"/>
<font face='calibri'>
<?php
session_start();
$conn = #mysql_connect("localhost","root","");
$db = #mysql_select_db("");
include("includes/functions.php");
mysql_set_charset("UTF8");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Billing Info</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="javascript">
$(document).ready(function () {
$("#center input[value='COD']").click(function () {
if (this.checked) {
$("#centerdown input[value='COD']").prop("checked", true);
} // end if checked
});
}); // end doc ready
$(document).ready(function () {
$("#center input[value='PickUp']").click(function () {
if (this.checked) {
$("#centerdown input[value='PickUp']").prop("checked", true);
} // end if checked
});
}); // end doc ready
$(document).ready(function () {
$("#center input[value='LBC']").click(function () {
if (this.checked) {
$("#centerdown input[value='BPI']").prop("checked", true);
} // end if checked
});
}); // end doc ready
</script>
<style>
#price{
color:red;
font-size:20px;
padding-right: 10px;
font-family: "Times New Roman", Times, serif;
float:right;
}
td{
display:block;
}
</style>
</head>
<body>
<input type="hidden" name="command" />
<div align="center">
<table border="0" cellpadding="2px">
<tr><td><table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="500px">
<?php
if(is_array($_SESSION['cart'])){
echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td></td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td></tr>';
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
$pname=get_product_prod_name($pid);
if($q==0) continue;
?>
<tr bgcolor="#FFFFFF"><td><?php echo $i+1?></td><td><?php echo $pname?></td>
<td>₱<?php echo number_format(get_prod_price($pid),2)?></td>
<td><input type="text" name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="2" size="2" disabled/></td>
<td>₱<?php echo number_format((get_prod_price($pid)*$q),2)?></td>
</tr>
<?php
}
?>
<tr><td></td><td colspan="5" align='right'><b>Order Total: ₱<?php echo get_order_total()?></b></td></tr>
<?php
}
else{
echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
}
?>
</table></td>
</tr>
</table>
<center><h1>Shipping Method</h1></center>
<form method="post">
<table width="900" border="0" align="center" cellpadding="2" cellspacing="0" id='center'>
<tr>
<td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="LBC") echo "checked";?> value="LBC"></td>
<td><img src="../paymentoptions/LBC.jpg" alt="LBC" class="picture"/></td>
<td><p>The Shipping takes 1-2 days for NCR and 2-3 days for any provincial.<p>
<!--<div id='price'> Additional ₱250 </div></td>-->
</tr>
<tr>
<td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="PickUp") echo "checked";?> value="PickUp"></td>
<td><img src="../paymentoptions/Pick-up.jpg" alt="Pick-Up" class="picture"/></td>
<td><p>Office hours: 10:00 am to 5:00 pm<p>
<!--<div id='price'> Free!! </div></td>-->
</tr>
</table>
<br>
<br>
<center><h1>Payment Method</h1></center>
<table width="900" border="0" align="center" cellpadding="2" cellspacing="0" id='centerdown'>
<tr>
<td><input type="radio" disabled name="payment" <?php if (isset($payment) && $payment=="BPI") echo "checked";?> value="BPI"></td>
<td><img src="../paymentoptions/BPI.jpg"></td>
<td><p>Pay by BPI bank deposit (we need confirmation of payment through email.)<p></td>
</tr>
<tr>
<td><input type="radio" disabled name="payment" <?php if (isset($payment) && $payment=="PickUp") echo "checked";?> value="PickUp"></td>
<td><img src="../paymentoptions/Pick-up.jpg"></td>
<td><p>Pick up. You have 5 days reservation period. You pay for the merchandise upon pick-up<p></td>
</tr>
</table>
<table>
<tr><td><input type="button" value="View Shopping Cart" onclick="window.location='shoppingcart.php?'"></td><td><input type="button" name="order" value="Place Order" onclick="window.location='billing.php?'"/> <!--<input type="button" value="Confirm Order" onclick="window.location='quotation.php?'">--></td></tr>
</table>
</form>
<?php
if (isset($_POST['order'])){
$_SESSION['carrier'] = $carrier;
$_SESSION['payment'] = $payment;
}
?>
</div>
</body>
</html>
I think what i placed at the bottom are wrong. haha. I have no idea how to do it, Thanks for looking!

These variables should be stored now in session memory.
These will be available on any page a session is started, until overwritten or destroyed, or by browser closing/timing out.
You can call them:
$somthing = $_SESSION['carrier'];
or how ever you need to use them.
<input name='blah' value='<?php echo $_SESSION['carrier'];?>'>

Related

Can I able to write with out isset($post[]) for the below code

<script>
function removeAllRowsContainingCheckedCheckbox() {
alert("hello");
for (var rowi= table.rows.length; rowi-->0;) {
var row= table.rows[rowi];
var inputs= row.getElementsByTagName('input');
for (var inputi= inputs.length; inputi-->0;) {
//alert("inside for");
var input= inputs[inputi];
if (input.type==='checkbox' && input.checked) {
//alert("indide if ")
row.parentNode.removeChild(row);
break;
}
}
}
}
</script>
<html>
<head>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<table border="1" id="table">
<tr>
<td colspan="2">Select Technolgy:</td>
</tr>
<tr>
<td>c</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td>hadoop</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td>core java</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td>Javascript</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td>springs</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td>
</tr>
<input type='button' value='del' onclick='removeAllRowsContainingCheckedCheckbox();'>Click me to delete
</table>
</form>
<?php
if(isset($_POST['sub']))
{
$db = pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=ndem$123");
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
$checkbox1=$_POST['techno'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk .= $chk1."";
echo '<script>alert("Inserted Successfully")</script>';
}
$in_ch=pg_query("insert into news_table(technology) values ('$chk')");
echo "the check box value is " . $in_ch;
if($in_ch==1)
{
echo '<script>alert("Inserted Successfully")</script>';
echo "Records created successfully\n";
}
else
{
echo pg_last_error($db);
}
pg_close($db);
}
?>
</body>
</html>
For the submit can i able to write javascript function with out using
$checkbox1=$_POST['techno'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk .= $chk1."";
echo '<script>alert("Inserted Successfully")</script>';
}
above code because if(isset($_POST['sub'])) is reloading the already submitted checkboxs in the table so user may confuse after delete the page will again show all the check box those who are submitted also .. Please suggest alternatives for this any ways i want to insert the checked checkboxes into database but my thought is to delete the checked submitted also in the table.

Submit button with 2 actions depends on dropdown menu

I am trying to figure out how to submit button with two actions based on dropdown list that I get from MySQL. One is for updating to database and then proceed to nextpage while the other actions is to proceed to specific page. I've tried all the solution but most of them only has a few dropdown menu, mine have 40 menus.
This is my JavaScript
<script type='text/javascript'>
$(window).load(function(){
//$(document).ready(function() {
<!--To Enable button "MULA"-->
$('#lst_respon').change(function() {
if ($(this).find(":selected").val()!='11') {
$('#button').attr('disabled',true);
} else {
$('#button').removeAttr('disabled');
}
});
$('#lst_respon').change(function()
{
$('#form1').submit();
});
});
function changeTextBox()
{
var val=$('#lst_respon').val();
if(val==='11')
{
$('#value').prop('disabled', true);
}
else{$('#value').removeAttr("disabled");}
}
</script>
And this is my HTML
<div align="center">
<table width="60%" border="1" class="zui-table">
<thead>
<tr>
<th colspan="3" align="center">Status</th>
</tr>
</thead>
<tr>
<?php
$smt = $conn->prepare('select * From lookup_caraterima');
$smt->execute();
$data = $smt->fetchAll();
$smt2 = $conn->prepare('select * From lookup_kodrespon');
$smt2->execute();
$data2 = $smt2->fetchAll();
?>
<td width="253">Cara Terima</td>
<td width="5">:</td>
<td width="164">
<select name="lst_cterima" id="lst_cterima">
<?php foreach ($data as $row): ?>
<option value="<? echo $row["kodterima"];?>"><? echo $row["jenis"];?>
</option>
<?php endforeach ?>
</select>
</td>
</tr>
<tr>
<td>Kod Respon</td>
<td>:</td>
<td>
<select name="lst_respon" id="lst_respon" onchange="changeTextBox()">
<?php foreach ($data2 as $row2): ?>
<option value="<? echo $row2["kod"];?>"><? echo $row2["kod"].'- '.$row2 ["Keterangan"];?></option>
<?php endforeach ?>
</select></td>
</tr>
<tr>
<td><label for ="sebab">Sebab</label></td>
<td>:</td>
<td><input type="textbox" id="value" size="100" disabled</td>
</tr>
<tr>
<td colspan="3" align="center"><form name="form1" method="post" action="main.php?load=4&SerialID=<?php echo $noSiri; ?>&month=<?php echo $monthA;?>&year=<?php echo $yearA;?>&tab=A">
<input type="submit" name="button" id="button" value="Teruskan" class="myButton">
</form></td>
</tr>
</table>
</div>
For an automated solution to submit a form based a dropdown selection, you could try using the data attribute, something like:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<form id="tester">
<select name="test">
<option value="best" data-instructions='<?php echo json_encode(array('action'=>'page1')); ?>'>Best</option>
<option value="rest" data-instructions='<?php echo json_encode(array('action'=>'page2')); ?>'>Rest</option>
<option value="guest" data-instructions='<?php echo json_encode(array('action'=>'page3')); ?>'>Guest</option>
<option value="lest" data-instructions='<?php echo json_encode(array('action'=>'page4')); ?>'>Lest</option>
</select>
<input type="submit" name="submit" value="SUBMIT" />
</form>
<script>
$(document).ready(function() {
$('#tester').submit(function(e) {
// Stop form from submitting
e.preventDefault();
// Get the select dropdown
var thisSelect = $(this).find('select[name=test]');
// Get the value
var thisVal = thisSelect.val();
// Get the options separately
var thisOpts = thisSelect.children();
// Create a holder
var thisData = {};
// Loop through the options
$.each(thisOpts,function(k,v) {
// Get the current value
var currVal = $(v).attr('value');
// If the current value is the same as the value selected
if(currVal == thisVal) {
// Get the instructions from the option
thisData = $(v).data('instructions');
// Stop looping
return false
}
});
console.log(thisData);
});
});
</script>
On submission, the selection will give you an object that you can use to instruct how to submit the form:
// thisData.action
{action: "page2"}

Save updated values to database and show them in html table

I have a table like the following:
<script>
$("#edit").hide(); // Hide the edit table first
$("#update").click(function() {
$("#edit").toggle();
$("#shown").toggle();
// If we are going from edit table to shown table
if($("#shown").is(":visible")) {
var vouchertype = $('input[name="vouchertype[]"]').map(function(){return $(this).val();}).get();
var mode= $('select[name="mode[]"]').map(function(){return $(this).val();}).get();
// Then add it to the shown table
var baseurl='<?php echo base_url()."index.php/account/insert_voucher";?>';
$.ajax({
type: "POST",
url: baseurl,
data: $('#edit *').serialize() ,
cache: false,
success: function(html) {
alert(html);
}
});
$(this).val("Edit");
}
else $(this).val("Update");
});
</script>
<table width="62%" height="70" border="1" cellpadding="0" cellspacing="0" class="tbl_grid" id="shown">
<?php if(count($voucher_info) > 0 ){ ?>
<tr class="bgcolor_02">
<td width="22%" height="25" align="center" class="admin" >S.no</td>
<td width="37%" align="center" class="admin">Voucher Type</td>
<td width="37%" align="center" class="admin">Voucher Mode</td>
</tr>
<?php
$rownum = 1;
foreach ($voucher_info as $eachrecord){
$zibracolor = ($rownum%2==0)?"even":"odd";
?>
<tr align="center" class="narmal">
<td height="25"><?php echo $eachrecord->voucher_id; ?></td>
<td><?php echo $eachrecord->voucher_type; ?></td>
<td><?php echo ucwords($eachrecord->voucher_mode); ?></td>
</tr>
<?php }
}
else {
echo "<tr class='bgcolor_02'>";
echo "<td align='center'><strong>No records found</strong></td>";
echo "</tr>";
}
?>
</table>
<table width="62%" height="70" border="1" cellpadding="0" cellspacing="0"
id="edit">
<?php if(count($voucher_info) > 0 ){ ?>
<tr class="bgcolor_02">
<td width="27%" align="center" class="admin" >S.no</td>
<td width="37%" align="center" class="admin" >Voucher Type</td>
<td width="47%" align="center" class="admin" >Voucher Mode</td>
<!-- <td width="41%" align="center" class="narmal"> <strong>Actions</strong></td>-->
</tr>
<?php
$rownum = 1;
foreach ($voucher_info as $eachrecord){
$zibracolor = ($rownum%2==0)?"even":"odd";
?>
<tr align="center" class="narmal">
<td height="25"><?php echo $eachrecord->voucher_id ; ?><input type="hidden" name="voucher_id[]" value="<?php echo $eachrecord->voucher_id; ?>" /></td>
<td><input name="vouchertype[]" type="text" value="<?php echo $eachrecord->voucher_type; ?>" /></td>
<td><select name="mode[]" >
<option value="paidin" <?php if($eachrecord->voucher_mode=='paidin') { ?> selected="selected" <?php } ?>>Paid In</option>
<option value="paidout" <?php if($eachrecord->voucher_mode=='paidout') { ?> selected="selected" <?php } ?>>Paid Out</option>
</select></td>
</tr>
<?php } }
else {
echo "<tr class='bgcolor_02'>";
echo "<td align='center'><strong>No records found</strong></td>";
echo "</tr>";
}
?>
</table>
</td>
</tr>
<input id="update" type="submit" name="submit" value="Edit"/
In first table I am displaying values from database. But when user click on edit button below the table, Then that table values should be editable for user. I have succeeded in making editable fields but again when user click on submit then updated values are not displaying in first table. I know it's possible with jQuery or JavaScript. When I alert(newsales), the alert is undefined.
An easy way to handle this would be have two separate tables, and toggle between them when you edit a table. So for example we have a table that shows our normal data called shown and one with the input and select for a user to enter data called edit. These tables will share the same button, and when clicked it will toggle between the tables, making it look like it's switching between edit and show mode. This way we can just copy the values from the edit table to the text in the shown table. Here is an example:
$("#edit").hide(); // Hide the edit table first
$("#update").click(function() {
$("#edit").toggle();
$("#shown").toggle();
// If we are going from edit table to shown table
if($("#shown").is(":visible")) {
// Get the data from the edit table
var newSales = $("#edit tr:nth-child(1) td input[name='vouchertype']").val();
var newPay = $("#edit tr:nth-child(1) td select[name='mode']").val();
var newTax = $("#edit tr:nth-child(2) td select[name='tax']").val();
// Then add it to the shown table
$("#shown tr:nth-child(1) td:nth-child(2)").text(newSales);
$("#shown tr:nth-child(1) td:nth-child(3)").text(newPay);
$("#shown tr:nth-child(2) td:nth-child(1)").text(newTax);
$(this).val("Edit");
}
else $(this).val("Update");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="shown">
<tr>
<td>Sr no</td><td>Sales</td><td>Paid in</td>
</tr>
<tr><td>Taxed</td></tr>
</table>
<table id="edit">
<tr>
<td>Sr no</td><td><input type="text" name="vouchertype" value="Sales" /></td>
<td>
<select name="mode">
<option value="paidin">Paidin</option>
<option value="paidout">Paidout</option>
</select>
</td>
</tr>
<tr>
<td>
<select name="tax">
<option value="Taxed">Taxed</option>
<option value="Not Taxed">Not Taxed</option>
</select>
</td>
</tr>
</table>
<input id="update" type="submit" name="submit" value="Edit"/>
Notice: This is an answer to the original question, which is quite different then the new question added in as an edit.

How to properly redirect, when redirecting, query wont input values into database

How do i properly redirect to another page? here are my codes.
<link rel="stylesheet" type="text/css" href="payout.css"/>
<font face='calibri'>
<?php
session_start();
$conn = #mysql_connect("localhost","root","mypassword");
$db = #mysql_select_db("buybranded");
include("includes/functions.php");
mysql_set_charset("UTF8");
if(isset($_GET['command']) && $_GET['command']=='update'){
$first_name=$_SESSION['first_name'];
$email=$_SESSION['email'];
$home_address=$_SESSION['home_address'];
$mobile_phone=$_SESSION['mobile_phone'];
$carrier=$_REQUEST['carrier'];
$payment=$_REQUEST['payment'];
$result=mysql_query("insert into customers values('','$first_name','$email','$home_address','$mobile_phone','$carrier','$payment')");
$customerid=mysql_insert_id();
date_default_timezone_set("Asia/Hong_Kong");
$date=date('Y-m-d h:i:s');
$result=mysql_query("insert into orders values('','$date','$customerid')");
$orderid=mysql_insert_id();
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
$price=get_prod_price($pid);
mysql_query("insert into order_detail values ($orderid,$pid,$q,$price)");
}
}
// else if(isset($_REQUEST['success']) && $_REQUEST['success']=='1'){
// header('refresh: 0; url=samplebrand.php');
// $message = "Thank you for buying, You will now be redirected";
// echo("<script type='text/javascript'>alert('$message');</script>");
// }
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Billing Info</title>
<script language="javascript">
function validate(){
var f=document.form1;
f.command.value='update';
f.submit();
alert("Order submitted");
}
</script>
</head>
<body>
<form name="form1" onsubmit="return validate()">
<input type="hidden" name="command" />
<div align="center">
<h1 align="center">Shipping and Payment</h1>
<table border="0" cellpadding="2px">
<tr><td>Order Total:</td><td>₱ <?php echo get_order_total()?></td><td> </td>
</tr>
</table>
<center><h1>Shipping Method</h1></center>
<form method="post">
<table width="900" border="0" align="center" cellpadding="2" cellspacing="0" id='center'>
<tr>
<td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="LBC") echo "checked";?> value="LBC"></td>
<td><img src="img/LBC.jpg" alt="LBC" class="picture"/></td>
<td><p>The Shipping takes 1-2 days for NCR and 2-3 days for any provincial.<p></td>
</tr>
<tr>
<td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="COD") echo "checked";?> value="COD"></td>
<td><img src="img/Cash on Delivery.jpg" alt="COE" class="picture" height="90" width="125"/></td>
<td><p>This service is only available for Meto Manila and Metro Cebu.<p></td>
</tr>
<tr>
<td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="Personal") echo "checked";?> value="Personal"></td>
<td><img src="img/buybranded2.jpg" alt="buybranded" class="picture"/></td>
<td><p>The Shipping takes 2-3 days after processing for NCR and 3-5 days for any provincial.<p></td>
</tr>
<tr>
<td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="NextDayDelivery") echo "checked";?> value="NextDayDelivery"></td>
<td><img src="img/NextdayDelivery.jpg" alt="NextDayDelivery" class="picture"/></td>
<td><p>The Shipping takes 1-2 days for NCR and 2-3 days for any provincial.<p></td>
</tr>
<tr>
<td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="SameDayDelivery") echo "checked";?> value="SameDayDelivery"></td>
<td><img src="img/Same day Delivery.jpg" alt="SameDayDelivery" class="picture"/></td>
<td><p>Available only for NCR. Get your sporting good/s the same day you purchase the item. Cutoff is 12noon.<p></td>
</tr>
<tr>
<td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="PickUp") echo "checked";?> value="PickUp"></td>
<td><img src="img/Pick-up.jpg" alt="Pick-Up" class="picture"/></td>
<td><p>Office hours: 10:00 am to 6:00 pm<p></td>
</tr>
</table>
<br>
<br>
<center><h1>Payment Method</h1></center>
<table width="900" border="0" align="center" cellpadding="2" cellspacing="0" id='centerdown'>
<tr>
<td><input type="radio" name="payment" <?php if (isset($payment) && $payment=="BPI") echo "checked";?> value="BPI"></td>
<td><img src="img/BPI.jpg"></td>
<td><p>Pay by BPI bank deposit (we need confirmation of payment through email.)<p></td>
</tr>
<!--
<tr>
<td><input type="radio" name="payment" <?php if (isset($payment) && $payment=="PayPal") echo "checked";?> value="PayPal"></td>
<td><img src="img/paypal.gif"></td>
<td><p>Pay with your PayPal account, credit card (CB, Visa, Mastercard...), or private credit card.<p></td>
</tr>
-->
<tr>
<td><input type="radio" name="payment" <?php if (isset($payment) && $payment=="PickUp") echo "checked";?> value="PickUp"></td>
<td><img src="img/cashondelivery.gif"></td>
<td><p>Pick up. You have 5 days reservation period. You pay for the merchandise upon pick-up<p></td>
</tr>
<tr>
<td><input type="radio" name="payment" <?php if (isset($payment) && $payment=="COD") echo "checked";?> value="COD"></td>
<td><img src="img/Cash on Delivery.jpg" height="90" width="125"/></td>
<td><p>Pay with your Cash on Delivery (COD)<br>Choose this option if you have selected COD under shipping. Otherwise, choose other options for payment.<p></td>
</tr>
</table>
<table>
<tr><td><input type="submit" value="Place Order"/></td></tr>
</table>
</form>
</div>
</form>
</body>
</html>
and the commented part of my code does not work as well. i am having a hard time fixing this.
when i add action="billingredirect.php" it doesnt input the query to the database. but when i remove it, it inserts the query.
if you are curious about my billingredirect.php here it is
<?php
header('refresh: 0;url=lorem.html');
$message = "You will now be redirected";
echo("<script type='text/javascript'>alert('$message');</script>");
?>
and when i add a redirect code in my script, it doesnt work as well
<script language="javascript">
function validate(){
var f=document.form1;
f.command.value='update';
f.submit();
alert("Order submitted");
}
</script>
die('hehe');
Try adding that line of code inside your php tags, before the last } to stop the script. to check if it works
To redirect to a different page/script use:
header("location: location.php");
exit(); //stops the current script executing
You seem to be using the super global $_GET which retrieves the data in the query string of the URL. Usually it appears after the ? in the address bar. What is does is get all the data from a GET request. The super global $_POST retrieves the data from a POST request.
Forms usually send POST requests, that would you do not get inside the IF statement.
To make sure of it, try using var_dump($_GET) or var_dump($_POST) this will print out all their content, that way you can verify what is wrong.
When redirecting on form submissions, the form data does not follow to the new page. The processing of the form data should be done in the script doing the redirection.

disabled textbox causes errors in PHP page when trying to update table in mysql

I'm caught in a set of twisting codes. I've a table in a form which consists of all the information from one table say emp_info. I am not including all the mysql query and db connection but just suppose this table has fetched information from emp_info table from my database. Example:
<table align="center">
<form name="f1" action="update.php" method="post">
<tr>
<td>Enter name : </td><td><input type="text" name="ename" id="field1"
disabled="disabled" /></td><td><input id="myCheckBox1" type="checkbox"
onclick="enableText1(this.checked, 'field1');" />
</td>
</tr>
<tr>
<td>Enter Address : </td><td><input type="text" name="address" id="field2"
disabled="disabled" /></td><td><input id="myCheckBox2" type="checkbox"
onclick="enableText2(this.checked, 'field2');" /></td>
</tr>
<tr>
<td><input type=""submit value="submit"/></td>
</tr>
</form>
</table>
and following is the respective JAVASCRIPT written in the HEAD section:
<script type="text/javascript">
function enableText1(checkBool, textID)
{
text1 = document.getElementById(textID);
//Disable the text field
text1.disabled = !checkBool;
//Clear value in the text field
if (!checkBool) { text1.value = ''; }
}
function enableText2(checkBool, textID)
{
text2 = document.getElementById(textID);
//Disable the text field
text2.disabled = !checkBool;
//Clear value in the text field
if (!checkBool) { text2.value = ''; }
}
</script>
I want user to check the checkbox to enable the textbox to edit it. What can a user do with it? He can check the checkbox to edit it and when he unchecks it once again textbox will be disabled. That means, what he edited in the checkbox is going to be locked. To prevent him from doing it; I'm setting the textbox to empty; once he unchecks the checkbox. It's working up to it. My real problem starts from here. when the textbox is disabled and he submits the records to update, it throws an error while accepting the value using POST method ($name=$_POST['name']) in update.php:
Notice: Undefined variable: address in C:\xampp\htdocs\xampp\Test\HRMS\try\chcking with checkboxes\update.php on line 4
My main intention is to update the record into the temporary table. When a record is submitted using disabled textbox or empty textbox; in that case I would like to fetch its value from the old table and store it into the temporary table. I do not know how clear I am but accepting some support from enthusiastic master's in coding here. Thanks in advance.
please check my actual code. I am including both the pages below :
<?php
session_start();
if(!$_SESSION['logged'])
{
header("Location: login.php");
exit;
}
echo 'Welcome, '.$_SESSION['user'];
//echo 'Welcome, '.$_SESSION['email'];
//echo 'Welcome, '.$_SESSION['eid'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function enableText1(checkBool, textID)
{
text1 = document.getElementById(textID);
//Disable the text field
text1.disabled = !checkBool;
//Clear value in the text field
if (!checkBool) { text1.value = ''; }
}
</script>
</head>
<body>
</body>
</html>
<?php
$user=$_SESSION['user'];
$email=$_SESSION['email'];
$eid=$_SESSION['eid'];
echo "<br>";
//echo $email;
//echo $eid;
//echo $user;
$con = mysql_connect("localhost","mars","mars");
if (! $con)
die(mysql_error());
mysql_select_db("marsweb",$con);
mysql_query(" UPDATE login_info SET user_staus=1 WHERE username='$email'");
$query="SELECT * FROM emp_info WHERE eid='$eid'";
//echo "$query";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {
$userid=mysql_result($result,$i,"eid");
$name=mysql_result($result,$i,"ename");
$password=mysql_result($result,$i,"password");
$address=mysql_result($result,$i,"address");
$source=mysql_result($result,$i,"source");
$salary=mysql_result($result,$i,"salary");
$zip=mysql_result($result,$i,"zip");
$mobile=mysql_result($result,$i,"mobile");
$email=mysql_result($result,$i,"email");
?>
<div class="go_right">
<table width="100" cellpadding="10" cellspacing="0" border="2" align="center">
<form action="change_record1.php" method="post">
<tr>
<td>Employee ID</td><td><input type="text" name="userid" value="<?php echo "$userid" ?>" readonly></td>
</tr>
<tr>
<td>Name:</td><td><input type="text" name="name" value="<?php echo "$name"?>" readonly></td>
</tr>
<!--<tr>
<td>Password: </td><td><input type="text" name="password" value="<?php echo "$password"?>" ></td>
</tr>-->
<tr>
<td>Address:</td><td> <input type="text" name="address" value="<?php echo "$address"?>" id="field1" dissabled="disabled" /></td>
<td><input id="myCheckBox1" type="checkbox" onClick="enableText1(this.checked, 'field1');" /></td>></td>
</tr>
<tr>
<td>Source: </td><td><input type="text" name="source" value="<?php echo "$source"?>"></td>
</tr>
<tr>
<td>Salary: </td><td><input type="text" name="salary" value="<?php echo "$salary"?>" readonly></td>
</tr>
<tr>
<td>Mobile:</td><td> <input type="text" name="mobile" value="<?php echo "$mobile"?>"></td>
</tr>
<tr>
<td>Zip: </td><td><input type="text" name="zip" value="<?php echo "$zip"?>"></td>
</tr>
<tr>
<td>Email: </td><td><input type="text" name="email" value="<?php echo "$email"?>" readonly></td>
</tr>
<tr style="border:#FFF";>
<td><input type="Submit" value="Update"></td>
</tr>
</form>
</table>
<?php
++$i;
}
?>
and the php page which will update the temporary table is given below...
<?php
session_start();
if(!$_SESSION['logged'])
{
header("Location: login.php");
exit;
}
echo 'Welcome, '.$_SESSION['user'];
echo "<br>";
echo "<br>";
?>
<?php
$userid=$_POST['userid'];
//$name=$_POST['name'];
//$password=$_POST['password'];
if(isset($_POST['address']))
$address=$_POST['address'];
$source=$_POST['source'];
$salary=$_POST['salary'];
$mobile=$_POST['mobile'];
$zip=$_POST['zip'];
$email=$_POST['email'];
$con = mysql_connect("localhost","mars","mars");
if (! $con)
die(mysql_error());
mysql_select_db("marsweb",$con);
//echo $userid; echo "<br>";
echo $address; echo "<br>";
if($address=="")
{
$query1="select address from `emp_info` where email='$email'";
$fetched1=mysql_query($query1);
while($record1=mysql_fetch_assoc($fetched1))
{
while(each($fetched1))
{
$address=$record1["address"];
}
}
}
echo $address;
mysql_query("UPDATE temp_emp_info SET eid='$userid' , address='$address' , source='$source' , mobile='$mobile' , zip='$zip' WHERE eid='$userid'");
echo "<br>";echo "<br>";echo "<br>";
echo "<center><h2>Record updated</h2></center><br><br>";
?>
When a field is marked "disabled" it doesn't get submitted to the server, so the value is lost. You either need to adapt your server side code to be aware of the possibility that the textbox is unset, or use readonly="readonly" instead of disabled="disabled" to make the text field uneditable. Read only controls are still submitted to the server, they just can't be edited by the user.

Categories