I have the following code which works well.
The code is to display all the data from the database in input text field format. User is able to edit and update the details by clicking the update button at the end of each row to update the specific row.
Instead of clicking the update buttons one by one (if user wants to edit and update more than one row), now I would like to create a hyperlink which will update ALL the data at once.
The problem now is I dunno how to pass the value of $_post to the "update.php" since I already have
<form name="form1" action="submitAction.php" method="post">
I am not sure is it possible to do so. Or are there any other alternative ways?
<html>
<script language="javascript" >
<!-- hide
function submitRequest(id) {
document.forms[id].submit();
}
// end hide -->
</script>
<!-- The Javasript (Onclick) to Remove the Readonly Attribute-->
<script language="JavaScript">
function removeAlignment(id){
document.getElementById("ProjectName_"+id).removeAttribute("readonly",0);
document.getElementById("DeviceType_"+id).removeAttribute("readonly",0);
}
</script>
<body>
<?php
$counter=1;
//Connecting and Accessing the Database
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Project", $con);
$result = mysql_query("SELECT * FROM Project where status='Ongoing'");
?>
<p><h2 align="center">ADC Project Funnel</h2></p>
<table border="1" bordercolor="lavender" cellspacing="0" cellpadding="0">
<tr>
<td width="20" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">No</font></b></td>
<td width="78" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">Project
Name</font></b></td>
<td width="72" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">Device Type</font></b></td>
<td width="67" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">Status</font></b></td>
<td width="67" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial"></font></b></td>
</tr>
<tr>
<td colspan="15" height="15" bgcolor="#AFEEEE"><font face="Arial" size="1.9">Current Project Assignment</font></td>
</tr>
<!-- Records from the database (Current Project Assignment) -->
<?php
$i = 0;
while ($row=mysql_fetch_array($result)){
?>
<tr>
<form name="form1" action="submitAction.php" method="post">
<td height="5" width="20" align="center" style="font-size: 13" valign="middle"><?php echo $counter; ?></td>
<td height="5" width="72" ><input type="text" autocomplete=off readonly="readonly" id="ProjectName_<?php echo $i; ?>" name="ProjectName<?php echo $row['No'];?>" value="<?php echo $row['ProjectName'];?>" size="20" style="font-size: 10"></font></td>
<td height="5" width="72" ><input type="text" autocomplete=off readonly="readonly" id="DeviceType_<?php echo $i; ?>" name="DeviceType<?php echo $row['No'];?>" value="<?php echo $row['DeviceType'];?>" size="15" style="font-size: 10"></font></td>
<td height="5" width="67" style="font-size: 13">
<select name="action" onchange="submitRequest(<?php echo $i; ?>);">
<option value=>Ongoing </option>
<option value="<?php echo $row['ProjectName'];?>">Complete</option>
<option value="<?php echo $row['Flag2'];?>">Future</option>
<option value="<?php echo $row['Flag1'];?>">Cancel</option>
<option value="<?php echo $row['No'];?>">Update</option>
<option value="<?php echo $row['Flag3'];?>">Delete</option>
</select>
</td>
<td height="5" width="64" ><input type="button" style="width:100%" value="Edit" onclick="removeAlignment(<?php echo $i; ?>);"></td>
</tr>
</form>
<?php
?>
<?php
$i++;
$counter++;
}
?>
<tr>
<td colspan="16" bgcolor="#AFEEEE"><font face="Arial" size="1.9">Add New Project Assignment</font></td>
</tr>
<!-- Add New Records -->
<tr>
<form action="project_insert.php" method="post">
<td width="20" ></td>
<td width="78" ><input type="text" autocomplete=off name="ProjectName" size="40" style="font-size: 10"></font></td>
<td width="72" ><input type="text" autocomplete=off name="DeviceType" size="15" style="font-size: 10"></font>
<td width="80" style="font-size: 13">
<select name="Status" style="width:100%">
<option value=Future>Future</option>
<option value=Ongoing>Ongoing</option>
</select>
</td>
<td>
<input type="Submit" style="width:100%"value="Add">
</td>
</form>
</tr>
</table>
<br/>
<table border="0" align="center" cellpadding="0" cellspacing="10">
<tr>
<td valign="middle">update</td>
</tr>
</table>
<br/>
</body>
</html>
You want to pass value in a link to another page ?
Simply use $_GET[] like in this example :
Update all
Make just one form tag for everything - not an individual form tag for each row.
Next change:
<select name="action" onchange="submitRequest(<?php echo $i; ?>);">
To:
<select name="action[<php echo $i; ?>]" onchange="submitRequest(<?php echo $i; ?>);">
(It's better to use the primary key from your database instead of the $i counter, but you don't say what it is.)
Also it has not been necessary to do:
<!-- hide
// end hide -->
For something like 10 years already, so you should stop including that.
Related
this is what i successfully done.
User key in the data into the text field and preview back the data.
this is what i failed to do:
capture back the data from db because the data that i select from drop down didn't store in the db. thats why i cannot print back the page after clicked submit.
it works well for other field but not for my drop down selection. is there somethings wrong with my code for this part?
this is my index.html file
<tr>
<td colspan="4" align="left" class="form-group form-inline"><b>Section
B</b></td>
</tr>
<script type="text/javascript">
function show() {
var dropdown = document.getElementById("employment");
var current_value =
dropdown.options[dropdown.selectedIndex].value;
if(current_value =="--Choose one--"){
didfv3.style.display = 'none';
didfv2.style.display = 'none';
}
if(current_value == "Unemployed"){
didfv3.style.display = 'none';
didfv2.style.display = '';
}
if(current_value == "Working"){
didfv3.style.display = '';
didfv2.style.display = 'none';
}
}
</script>
<td>employment</td>
<td colspan="4">
<select name="employment" id="employment" onChange="show();">
<option value="--Choose one--">--Choose one--</option>
<option value="Unemployed">Unemployed</option>
<option value="Working">Working</option>
</select>
<table id="didfv2" width="90%" border="0" align="center"
style="display:none">
<tr>
<td width="50%">Balance</td>
<td width="1%" align="center">:</td>
<td width="73%" ><input type="text" name="balance" readonly maxlength="10"
value="<?php echo $balance?>" size="5" > months</td>
</tr>
</table>
<table id="didfv3" width="90%" border="0" align="left"
style="display:none">
<tr>
<td class="form-group form-inline">Basic Salary</td>
<td width="0.5%" align="knk" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="basic" type="text" id="basic" maxlength="20"/></td>
</tr>
<tr>
<td>Gross Salary</td>
<td width="0.5%" align="center" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="gross" type="text" id="gross" maxlength="20"/></td>
</tr>
<tr>
<td class="form-group form-inline">Nett Salary</td>
<td width="0.5%" align="center" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="nett" type="text" id="nett" maxlength="20"/></td>
</tr>
<tr>
</table>
here is my insert.php
<?
$employment = $_POST['xxx'];
if (isset($_POST['xxx'])) {
$employment =$_POST['xxx'];
}
if($employment =='Working'){
$basic = $_POST['basicsalary00'];
$gross = $_POST['grosssalary00'];
$nett = $_POST['nettsalary00'];
}
elseif($xxx =='Unemployed'){
$balance = $_POST['balance'];
}
$sqlinsert = "INSERT INTO application
(
xxx,basicsalary00,grosssalary00,nettsalary00)
VALUES(0,'$xxx','$basic','$gross','$nett')";
?>
// here is to print back the output
<form name="formName" action="form1" method="post" class="form-group form-
inline">
<tr>
<td rowspan="2">Employment</td>
<td colspan="3">
<option value="<?php echo $employment;?>"><?php echo $employment;?>
</option>
<table id="didfv2" width="90%" border="0" align="center"
style="display:none">
<tr>
<td colspan="3">
<input type="hidden" id="balance" value="<?php echo $balance;?>" />
</td>
</tr>
</table>
<table id="didfv3" width="90%" border="0" align="center"
style="display:none">
<tr></tr>
<tr>
<td>Basic Salary</td>
<td>
<input name="basic" type="hidden" id="basic" value="<?php echo $basic;?>" />RM <?php echo $basic;?></td>
</tr>
<tr>
<td>Gross Salary</td>
<td>
<input name="gross" id="gross" type="hidden" value="<?php echo $gross;?>"
/>RM <?php echo $gross;?></td>
</tr>
<tr>
<td>Nett Salary</td>
<td>
<input name="nett" type="hidden" id="nett" value="<?php echo $nett;?>"
/>RM <?php echo $nett;?></td>
</tr>
</table>
</td>
</tr>
</form>
So there will be 4 pages,
1) for user input (index.html)
2) Preview back the input (i didnt include the file in here - successful)
3) Insert the input to db (insert.php)
4) Print back the result (code at the bottom of the insert.php...fetch from db- which i failed. Working for other field but not for drop down section)
I really need your help.. i've been stuck here for a week.... thank you
A proper dropdown should have a <select> and the <option> elements should be inside it!
In your insert.php, I can see that you have only used this line:
<option value="<?php echo $employment;?>"><?php echo $employment;?></option>
How about your try it like this:
<select readonly>
<option value="--Choose one--">--Choose one--</option>
<option <?php echo ($employment == 'Unemployed') ? 'selected' : ''; ?> value="Unemployed">Unemployed</option>
<option <?php echo ($employment == 'Working') ? 'selected' : ''; ?> value="Working">Working</option>
</select>
Here, we are checking whether any of those <option> element is same as the one that the user has submitted. If so, make it as selected
Hope it helps.
EDIT
If you want to store the value of $employment to display it later, you definitely have to store it in your db.
As a novice in this and I'm struggling with a small project.
I’m putting together a page for INTERNAL orders in a small MTB club. My problem is if there are more than 1 order in the $_SESSION['cart'] and I try to remove the first one [0], then I get “Undefined offset: 0”.
I get that is to do with that I remove the first entry and therefor it can’t be found when it lists the array. I just can’t see how I solve it, I have googled and tried different solutions without result.
Please advice on how to get forward for me..
Below code is made for this purpose only
enter code here <?php
session_start();
if(isset($_POST['submit'])){
$total = $_POST['Amount'] * $_POST['Price'];
$cart=array(
'Amount'=>$_POST['Amount'], //Amoun ordered of clothes
'Size'=>$_POST['Size'], //Size of clothes
'Price'=>$_POST['Price'], //Price of clothes
'Product_id'=>$_POST['Product_id'],//Id of clothes for DB
'Product_Name'=>$_POST['Product_Name'], //Name of clothes
'Product_Total'=>$total //Total price
);
$_SESSION['cart'][]=$cart;
}
if(isset($_GET['Reset'])){
unset($_SESSION['cart']);
header('location:demo.php');
}
if(isset($_GET['remove'])){
$do = $_GET['do'];
unset($_SESSION['cart'][$do]);
//Redirecting After Unset SESSION
header('location:demo.php');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Session Demo/Test</title>
</head>
<body>
<table width="590" border="4" cellspacing="0" cellpadding="5" align="center" >
<tbody>
<tr>
<td width="373">Name of clothes</td>
<td width="185">
Reset Session</td>
</tr>
</tbody>
</table>
<form method="POST">
<table width="590" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td colspan="4" rowspan="5" align="center"><img src="images/Bike_Jersy_short.jpg" width="200" height="200" alt=""/></td>
<td height="120" colspan="2">
Clothes description
</td>
</tr>
<tr>
<td width="112">Price:</td>
<td>300,00 Kr.</td>
</tr>
<tr>
<td width="112">Amount:</td>
<td width="116">
<input name="Amount" type="number" id="Amount" tabindex="1" value="" size="1" required >
</td>
</tr>
<tr>
<td>Size:</td>
<td>
<select name="Size" id="Size" tabindex="2" required>
<option value=""></option>
<option value="XS">XS</option>
<option value="S">S</option>
<option value="L">L</option>
<option value="XL">XL</option>
<option value="2XL">2XL</option>
<option value="3XL">3XL</option>
<option value="4XL">4XL</option>
<option value="5XL">5XL</option>
<option value="6XL">6XL</option>
</select>
</td>
</tr>
<tr>
<td>Add:</td>
<td>
<input type="submit" name="submit" id="submit" value="Add">
<input name="Price" type="hidden" id="Price" value="300">
<input name="Product_id" type="hidden" id="Product_id" value="1">
<input name="Product_Name" type="hidden" id="Product_Name" value="Name of clothes">
</td>
</tr>
</tbody>
</table>
</form>
<p></p>
<?php
if(empty($_SESSION['cart'])){
?>
<table width="898" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td>You have no items in yet </td>
</tr>
</tbody>
</table>
<?php
}
else{
?>
<table width="900" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td width="109">Name</td>
<td width="31">Amount</td>
<td width="33">Size.</td>
<td width="63">Price</td>
<td width="55">Total</td>
<td width="45">Remove</td>
</tr>
<?php for($i = 0 ; $i < count($_SESSION['cart']) ; $i++) {
?>
<tr>
<td><?php echo $_SESSION['cart'][$i]['Product_Name'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Amount'];?>
</td>
<td><?php echo $_SESSION['cart'][$i]['Size'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Price'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Product_Total'];?></td>
<td>Remove
<?php
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
Use the array_shift() function.
Like so:
$value = array_shift($_SESSION['cart']);
This will remove the first element in the array regardless of the current index. $value is the value of the element that was removed.
I have this code in php
<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
<form method="POST" action="addtocart.php">
<table class="table" style="overflow-x: auto; width: 900px;">
<tr>
<th class="danger" id="th">Items</th>
<th class="danger" id="th">Price(PhP)</th>
<th class="danger" id="th">Quantity</th>
<th class="danger" id="th">Action</th>
</tr>
<?php
$item=mysqli_query($conn,"select * from store");
while($pqrow=mysqli_fetch_array($item)){
$iid=$pqrow['id_item'];
?>
<tr>
<td class="warning" id="td">
<?php echo $pqrow['item']; ?></td>
<td class="warning" id="td"><input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>"><?php echo $pqrow['price']; ?></td>
<td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
<td class="warning" id="td"><input type="hidden" name="item" value="<?php echo $iid; ?>"><input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>"></td>
</tr><?php } ?>
</table></form>
</div>
When I click AddToCart in first row, the value of the last row has been fetch. I want to get the id and the input quantity when I click the AddToCart in 1st row.
As you have only one form tag and multiple inputs with same name on this form, value of next input overwrites previous one. As a result you always have value of the last row.
You can fix it for example with creating a separate form for every row, e.g:
<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
<table class="table" style="overflow-x: auto; width: 900px;">
<tr>
<th class="danger" id="th">Items</th>
<th class="danger" id="th">Price(PhP)</th>
<th class="danger" id="th">Quantity</th>
<th class="danger" id="th">Action</th>
</tr>
<?php
$item = mysqli_query($conn,"select * from store");
while ($pqrow=mysqli_fetch_array($item)) {
$iid=$pqrow['id_item'];?>
<tr>
<td class="warning" id="td">
<?php echo $pqrow['item']; ?></td>
<td class="warning" id="td"><?php echo $pqrow['price']; ?></td>
<td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
<td class="warning" id="td">
<!-- Separate form for each record -->
<form method="POST" action="addtocart.php">
<input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>">
<input type="hidden" name="item" value="<?php echo $iid; ?>">
<input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>">
</form>
</td>
</tr>
<?php } ?>
</table>
</div>
In this case you should track with javascript the value of quantity field. Of course, you can move <input type="number"> in a form too, but this will break your markup.
Also, if you're familiar with javascript, you can add some client preprocessing (getting values from inputs of a certain row) and use ajax-request to send these values to server.
I'm making a quiz. I'm getting all the questions on one page, but I don't want that. I only want the first question. After the button is clicked, then i want the second question. How can I accomplish this? Here's my code:
$qry=mysql_query("select `id`,`question` from `paper_details` where `paper_id`='$id'") or die(mysql_error());
$num_rw= mysql_num_rows($qry);
while($f=mysql_fetch_assoc($qry))
{
$q2=mysql_query("select `id`,`que`,`unique_id`,`image` from `que_master` where `id`='".$f['question']."'") or die(mysql_error());
$f2=mysql_fetch_row($q2);
$i=$pid;?>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="4" class="form_border" id="form_table">
<tr>
<th width="3%" rowspan="2" align="left" valign="top" class="form_label"></th>
<th width="5%" height="27" align="left" valign="top" class="form_label">Que<?php echo $i;?> : </th>
<th width="92%" align="left" valign="top" class="form_label"> <?php echo $f2[1]; if($f2[3]!=""){?>
<img src="admin/que_image/<?php echo $f2[3];?>" border="noborder" width="20px" height="20px"/><?php }?>
<input type="hidden" id="que_id" name="que_id" value="<?php echo $f2[0];?>"/>
</th>
</tr>
<tr>
<th height="78" align="left" valign="top" class="form_label">Ans : </th>
<td align="left" valign="top" style="line-height:2.0em;">
<input type="hidden" id="id" name="id" value="<?php echo $id;?>"/>
<input type="hidden" id="pid" name="pid" value="<?php echo $pid+1;?>"/>
<input type="hidden" id="num_raw" name="num_raw" value="<?php echo $num_rw;?>"/>
<input type="hidden" id="time" name="time" value="<?php echo $time;?>" />
<input type="hidden" id="que_id" name="que_id" value="<?php echo $f2[0];?>" />
<?php
$q3=mysql_query("select `id`,`ans`,`ans_img` from `ans_master` where `unique_id`='$f2[2]'") or die(mysql_error());
while($f3=mysql_fetch_row($q3)){
?>
<input type="radio" value="<?php echo $f3[0]; ?>" name="ans" id="ans"><?php echo $f3[1]; ?><?php if($f3[2]!=""){?>
<img src="admin/ans_image/"<?php echo $f3[2];?>" border="noborder" width="20px" height="20px"/>
<?php }
}}
?>
</td>
</tr>
<tr>
<th height="33" align="left" valign="middle" class="form_label"> </th>
<th colspan="2" align="left" valign="middle" class="form_label">
<input type="submit" name="Submit" value="Next >" />
Please help me.
If I understand correctly, you want to have a single-page application to run your quiz.
For this purpose, you could use ajax requests with jquery.
Or else, you can make a separate page for each question.
I have the following page with an option box, when the user selects the following options and then clicks submit, it takes them to the relevant url. However the user must type the correct string in the box which matches a simple static captcha image which is on the screen.
Currently the code looks like this, however i need help setting the condition so that "abcdef" must be typed in the box before it is submitted otherwise giving an error.
<?php
// Check to see if the form has been submitted.
if(isset($_POST['menu1'])) {
// If the form has been submitted, force a re-direct to the choice selected.
header('Location: ' . $_POST['menu1']);
}
?>
<div style="padding-left: 50px">
<p class="arial"><strong></strong><br /><br /></p>
<form method="post">
<table class="freecontact2form" border="0" width="400px">
<tbody>
<tr>
<td colspan="2"><span style="font-size: x-small;"> </span> <font color=#000000 ></font>
<br /><br /></td>
</tr>
<tr>
<td valign="top"><table width="400px" class="freecontact2form">
<tr>
<td colspan="2"><br />
<br />
<div class="freecontact2formmessage"> </div></td>
</tr>
<tr>
<td valign="top"><label for="menu1" >These are the following options:<span class="required_star"> </span></label></td>
<td valign="top"><select name="menu1" id="menu1">
<option selected="selected" value ="http://www.google.com">Google </option>
<option value ="www.msn.com">MSN</option>
<option value ="www.youtube.com">Youtube</option>
<option value ="bing.com">Bing</option>
</select></td>
</tr>
<tr>
<td valign="top"><label for="captcha" ><span class="required_star"></span><span class="required_star"></span></label></td>
<td valign="top"><BR /><BR /><img src="captcha.jpg" /></td>
</tr>
<tr>
<td>Please enter the characters shown in the CAPTCHA image:</td>
<td><input type="text" name="captcha" id="captcha" value="" size="10" />
</td>
</tr>
<tr>
<td style="text-align:center" colspan="2"><br /><br />
<input type="submit" value=" Submit ">
</td>
</tr>
</table></td>
<td valign="top"> </td>
</tr>
<tr>
</tr>
<tr>
</tr>
</tbody>
</table>
</form> <br />
<p> </p>
<p> </p>
Save the code in a session when you first display the page. Then check the POST value of the code field against the session variable.
session_start();
$_SESSION['captcha'] = 'abcdef';
On form submission:
session_start();
if($_POST['captcha'] == $_SESSION['captcha'] && isset($_POST['menu1'])){
header();
}