Multiple Submit Buttons Linked Back To Main Record - php

I wonder whether someone may be able to help me please.
Firstly, this is something I've never tackled before, so please bear with me if this is something which is straight forward to the more seasoned developer.
The code below is a portion of a script which correctly shows a 'Locations List' for each user.
$i=0;
while ($i < $num) {
$lid=mysql_result($result,$i,"locationid");
$lname=mysql_result($result,$i,"locationname");
$laddress=mysql_result($result,$i,"returnedaddress");
include("admin/link.php");
include("admin/opendb.php");
$fquery = "SELECT COUNT(*) num FROM finds WHERE locationid = '$lid'";
$fcount = mysql_query($fquery) or die('error');
$row = mysql_fetch_assoc($fcount);
$findscount = $row['num'];
mysql_close($connect);
?>
<table width="603" border="0">
<tr>
<th width="156">Location</th>
<th width="302">Address</th>
<th width="131">No. Of Finds Made</th>
</tr>
<tr>
<td><?php echo $lname;?></td>
<td><?php echo $laddress;?></td>
<td><?php echo $findscount;?></td>
</tr>
</table>
<?php
echo'<form name="locations" method="post">';
$i++;
}
if ($num == 0) {
echo"<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan='3'><div align='center'><strong>No locations have been added yet. <a href='saveaddress.php'>Click here</a> to add one.</strong></div></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>";
}
echo '<input type="submit" name="submitlocation" id="submitlocation" value="View/Amend Location Details">';
echo '<input type="submit" name="submitfinds" id="submitaddfinds" value="Add Finds">';
echo '</div>'."\n";
if(isset($_POST["submitlocation"])) {
header("updatelocation.php");
}
else if(isset($_POST["submitaddfinds"])) {
header("addfinds.php");
}
?>
What I'm trying to do is add two submit buttons for each Location record, one which takes the user to a page called updatelocation.php and the second which takes the user to a page called addfinds.php. But both must be linked to the Location via the field 'locationid'. So in other words if the user wants to Add Finds for Location 1 upon clicking on the relevant button, they are taken to the Add Finds page for Location 1.
I've read a number of tutorials, but I've obviously misunderstood somewhere along the lines because although I've managed to add the buttons to the record, when I click either button the 'Locations List is refreshed, rather than the user being taken to the respective page.
I just wondered whether someone could possibly have a look at this please and let me know where I'm going wrong?
Many thanks and kind regards

You'd be better off making a seperate page (i.e., forrmaction.php) which processes the logic and sends the user to the correct page. If you do not want to do that, you can also make a seperate form for each button and simply set the action attribute of form to point to either updatelocation.php or addfinds.php
Example of formaction.php:
if (isset($_POST['type']) {
if ($_POST['type'] == 'update')
$url = 'updatelocation.php';
else
$url = 'addthis.php';
header("Location: " . $url);
}
Example of form with two buttons that works with formaction.php:
<form name="locations" method="POST" action="formaction.php">
<input type="submit" name="type" value="update">
<input type="submit" name="type" value="add">
</form>
However, for your question, the problem is that you are not using the correct header. To use a redirect, you must use "Location:" in the header. For example,
header("Location: updatelocation.php");
Be careful, because this will not work if headers have already been sent. You may want to look into output control in that case.

Related

How do I toggle a displayed database field with single click

I have a database table and one specific value is either a value of Yes or No represented by an icon of checkmark or X.
I want to be able to toggle this value with a single click rather then the current workflow where the user clicks the icon and then they are directed to update page where they toggle the value and then update.
The value is either Yes or No. I simply want to click it have the value change to the opposite value real time without refreshing.
Thank you
Here is the partial code for the display database, the field name is called 'enter'
<?php
include "db.inc.php";
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 15;
$sql = "SELECT * FROM ircb ORDER BY id DESC LIMIT $start_from, 15";
$rs_result = mysql_query ($sql);
$num_rows = mysql_num_rows($rs_result);
$query = mysql_query("SELECT * FROM ircb");
$number=mysql_num_rows($query);
while($rows=mysql_fetch_array($rs_result)){
<tr bgcolor='#9B9D9F'>
<td><?= $rows['totalt'] ?></td>
<td><?= $rows['service'] ?></td>
<td><?= $rows['item'] ?></td>
<td align="center">
<a href="edit_form1.php?id=<?= $rows['id'] ?>">
<img src="backgrounds/bg/<?= $rows['enter'] ?>.png" border="0">
</a>
</td>
</tr>
The edit.form1
<form method="post" action="update1.php">
<input type="hidden" name="id" value=<?php echo "$row[id]"?>">
<tr>
<td>Entered</td>
<td>
<input type="radio" name="enter"
value=Yes>Yes<input type="radio" name="enter" value=No checked>No
</td>
</tr>
<hr />
<tr style="height: 20px"><td></td></tr>
<tr>
<td align="right">
<input class="button" type="submit"
value="Update">
</td>
The problem you are having seems to be because you are redirecting to a page that has a form to do the update. Instead of using a form, you could use the link directly to do the trick. The best way to do it is to have the href lead to the exact same page that displays the table, but adding query parameters to tell the scripts to toggle the required row.
Using this, you could check in your PHP, before querying the database to display the tables:
if($_GET['toggle_id']){
//$_GET['toggle_id'] = the ID of the line to modify
//Use the same script as the form handler
}
So instead of waiting for a value of YES or NO, you would figure it out yourself using the current value:
$nextValue = $currentValue=='NO'?'YES':'NO';
That should cover most of the logic.

Store PHP/SQL foreach form items in variables

Sorry I'm a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.
I have a PDO statement that gets all users from a database.
With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.
My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error.
I also specified to 'POST in the form but it doesnt seem to be doing that.
Does anyone have any ideas?
Thanks
//function.php
function getName($tableName, $conn)
{
try {
$result = $conn->query("SELECT * FROM $tableName");
return ( $result->rowCount() > 0)
? $result
: false;
} catch(Exception $e) {
return false;
}
}
//form.php
<form action "index.php" method "POST" name='form1'>
<table border="1" style="width:600px">
<tr>
<th>Name</th>
<th>Number Entered</th>
<tr/>
<tr>
<?php foreach($users as $user) : ?>
<td width="30%" name="FullName">
<?php echo $user['FullName']; ?>
</td>
<td width="30%">
<input type="int" name="NumberedEntered">
</td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="submit"></td>
</form>
//index.php
$users = getName('users', $conn);
if ( $_REQUEST['NumberedEntered']) {
echo $_REQUEST['NumberedEntered'];
echo $_REQUEST['FullName'];
}
The variable FullName isn't transmitted by your form to index.php. Only values of form elemnts are sent. You can add a hidden form field, that contains FullName like this:
<input type="hidden" name="FullName" value="<?php echo $user['FullName']">
But your second problem is, that your foreach loop will create several input fields with the exact same name. You won't be able to recieve any of the entered numbers, except the last one. have a look at this question for possible solutions.
Update
Putting each row in individual form tags should solve your problem:
<?php foreach($users as $user) : ?>
<form action="index.php" method="POST">
<tr>
<td align="center" width="40%" >
<?php echo $user['FullName']; ?>
<input type="hidden" name="FullName" value="<?php echo $user['FullName']; ?>" />
</td>
<td width="30%">
<input name="NumberedEntered"/>
</td>
<td>
<input type="submit" value="submit"/>
</td>
</tr>
</form>
<?php endforeach; ?>

Including .php pages after a form has been submitted

I'm attempting to make a master-detail search page for my intranet that will allow me to search both staff and hardware from the same page. I've gotten part of the way there but I've gotten myself stuck and need some help.
I have the inventory page (index.php) setup and I've included a form with two button - "Staff" and "Hardware" - to include the master-details .php pages I've designed.
The code for this is as follows:
<div class="content">
<h1>Inventory</h1>
<p></p>
<form action="" method="post">
<input type="submit" name="Staff" value="Staff" />
<input type="submit" name="Hardware" value="Hardware" />
</form>
<p></p>
<p></p>
<?php
if (isset($_POST['Staff']))
{
include 'details.php';
include 'lib/staffsearch.php';
};
if (isset($_POST['Hardware']))
{
include 'detailhw.php';
include 'lib/hwsearch';
};
?>
</div>
Again, this works fine - I click "staff" and it loads both my "staffsearch.php" and "details.php" pages. This is where I run into trouble however. My staffsearch.php includes a form that queries my staff database and returns the matching results in a table. I should be able to click on one of the results and the detailed information is displayed in the details box above. What happens though is that I click on the "Staff" button, my includes are displayed, I enter the search criteria, click "search" and get a blank page as a result.
If I click on the "staff" button again though my includes are reloaded along with the correct search result. Accessing the details however results in the same problem - once my results are displayed and I click on one instead of the details I receive a blank page until I click the "staff" button again - then my includes are reloaded and my results are displayed.
Is there anyway that when I click the search button on my staffsearch form that I can force the index page to reload the includes without having to click the "staff" button again?
Thanks, ahead of time for any advice as I'll throw in the mandatory. "I'm new to php" and my Googling hasn't really netted me any good results. If you need anymore information just ask!
EDIT 1
The code for my searchstaff.php page is as follows:
<table width="598px" border="1" align="center">
<tr valign="middle">
<td alight="left" colspan="3"> <form id="form1" name="form1" method="get" action="">
<label for="name">Search:</label>
<input type="text" name="name" id="name" /><input type="submit" name="search" id="search" value="Submit" style="margin-left:10px";/>
</form>
</td>
<tr>
<td align="center" width="40px">No.</td>
<td width="300px">Name</td>
<td width="158px">IP Address</td>
</tr>
<?php do { ?>
<tr>
<td align="center"><?php echo $row_inventorystaff['staff_id']; ?><?php echo $row_DetailRS1['staff_id']; ?></td>
<td> <?php echo $row_inventorystaff['f_name']; ?> <?php echo $row_inventorystaff['l_name']; ?><?php echo $row_DetailRS1['f_name']; ?> <?php echo $row_DetailRS1['l_name']; ?></td>
<td><?php echo $row_inventorystaff['ip_address']; ?><?php echo $row_DetailRS1['ip_address']; ?></td>
</tr>
<?php } while ($row_inventorystaff = mysql_fetch_assoc($inventorystaff)); ?>
</table>
<br />
<table border="0" style="margin-left:15px;" >
<tr>
<td><?php if ($pageNum_inventorystaff > 0) { // Show if not first page ?>
First
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_inventorystaff > 0) { // Show if not first page ?>
Previous
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_inventorystaff < $totalPages_inventorystaff) { // Show if not last page ?>
Next
<?php } // Show if not last page ?></td>
<td><?php if ($pageNum_inventorystaff < $totalPages_inventorystaff) { // Show if not last page ?>
Last
<?php } // Show if not last page ?></td>
</tr>
</table>
<p style="margin_left:15px">Records <?php echo ($startRow_inventorystaff + 1) ?> to <?php echo min($startRow_inventorystaff + $maxRows_inventorystaff, $totalRows_inventorystaff) ?> of <?php echo $totalRows_inventorystaff ?>
</p>
I have screen shots of the process of clicking through the index and performing a search that I can upload if it helps the explanation.
I would use a radio button to make a decision which will be easier to parse. Double submit buttons are a funny thing and some browsers dont like that.
<input name="dept" value="staff" type="radio"> Staff
<input name="dept" value="hardware" type="radio"> Hardware
<?php
if( isset($_POST['dept'] && $_POST['dept'] == 'staff' ) {
include 'staffForm.php';
} else {
include 'hardwareForm.php';
}
?>

HTML form to update Mysql with PHP (and HTML)

I've been trying to develop a real estate page where people can add listings. I am new to the world of php mysql. I have been over this problem for over a day and can't figure out where the problem is.
I have a form where people can add data. That's good and working. Now I am starting to have a place where people can add / delete / update their info. I am trying to build this step by step.
This is where a user could pull the information. My problem is with the piece of the code:
edit_form.php?idBAR=$row[id].
Full code below.
<table>
<tr>
<td align="center">EDIT DATA</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include"configS_OH.php";//database connection
$order = "SELECT * FROM braaasil_brokerstour.property";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[id]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>$row[day]</td>");
echo ("<td>$row[hours]</td>");
echo ("<td>Edit</td></tr>");
}
?>
</table>
</td>
</tr>
</table>
Then this tutorial try to pass id through the address bar (I don't know much about php to actually say much)
It tries to upload the data into a new form where a person could edit info.
But I can't load the data into the new form. If I use where id=7, I get the info into the form. But this method of passing the info in the address bar like ?idBAR=8... and then try to catch it in the other code (where id=$idBAR), is not working.
Here is the code:
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?php
include "configS_OH.php";//database connection
print $database;
$order = "SELECT * FROM braaasil_brokerstour.property
WHERE id='$idBAR'";
print $idBAR;
$result = mysql_query($order) or die( mysql_error() );
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="idBAR" value="<?php echo "$row[id]"?>">
<tr>
<td>Address</td>
<td>
<input type="text" name="address"
size="20" value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td>Date</td>
<td>
<input type="text" name="day" size="40"
value="<?php echo "$row[day]"?>">
</td>
</tr>
<tr>
<td>Time</td>
<td>
<input type="text" name="time" size="40"
value="<?php echo "$row[hours]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
I tried an tried and tried..
Thank you for your time in advance.
WHERE id='$idBAR'
You haven't assigned $idBAR any value. You need to read it from the $_GET array first:
$idBAR = $_GET['idBAR'];
You should, of course, check that this value exists first, and is acceptable.
I don't see anywhere you have actually used the GET data, just the reference name which is used in GET.
If you first query is working and is getting the $row['id'] value ok - you can verify this when you go to edit_form.php, in your browser URL bar at the top, does it say this:
edit_form.php?idBAR=7
(or whatever number should be there)
If so, then you just need to use the PHP GET. Your data is stored in $_GET[], and in this case, the reference name is idBAR. So your id from your previous page query is sent through the link into your URL, and on your edit_form.php page, you'd use that data as:
$_GET['idBAR']
You can use that, but personally I assign the data to a variable, such as:
$strGetId = $_GET['idBAR'];
Then you can use $strGetId throughout your code.
Also, check things like isset(), empty() etc, just so you know you are working with A) something is actually there, and B) it's not empty etc
if you are putting a variable directly in a string without concatenating, it can't be an array variable; you must concatenate those you also need to surr. so this
echo ("<td>Edit</td></tr>");
should be this
echo ("<td>Edit</td></tr>");
also, it looks like your form is sending data with POST. When you pass form data in the url string after the question mark, that is passing with get.
so...in your form where you want to use that variable, you set it up like this
$idBAR=$_GET['idBAR']; //to get the variable if it was part of the URL
$idBAR=$_POST['idBAR']; //if it was sent with post, as is the case with your form
also, request contains both get and post, so
$idBAR=$_REQUEST['idBAR'];
will work in either case.
The problem is the $row[id] is seen as text just like everything else. You want the value of $row[id]. Instead of
echo ("<td>Edit</td></tr>");
try
echo ("<td>Edit</td></tr>");

Is POST information from jquery $.post sent in parent page

I have a parent page which has a drop down list in it. using the onchange event, data is posted to a second page using $.post(). This page uses the posted variables to output mysql data with a checkbox for each line. This page output is then inserted into the parent page using jquery $('#DIV name').html(output).show();
The user can then see the mysql table rows with corresponding checkboxes. They then select the checkboxes they want and say delete. Delete is a form submit button.
My question is, when they click delete how do I take the form data from the second page and post it with that of the parent page so that I can then use $_POST[] to get the checkbox info and delete the selected table rows?
example of the parent page code is:
javascript/jquery
<script type="text/javascript">
function get(row){ //row being processed, defined in onchange="get(x)"
('getpeopleinjobs.php',{ //data posted to external page
postvarposition: form["position"+row].value, //variable equal to input box, sent to external page
postvarjob: form["job"+row].value, //variable equal to input box, sent to external page
postvarperson: form["person"+row].value, //variable equal to drop down list, sent to external page
postrow: row}, //variable equal row being processed, sent to external page
function(output){
$('#training'+row).html(output).show(); //display external results in row div
//popupWindow = window.open('t4.php?variable1Name=Vicki&variable2Name=Maulline','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
});
}
</script>
Form data is
<tr>
<td>
<?PHP echo $i; ?>
</td>
<td>
<input type=text NAME="position<?PHP echo $i; ?>" id="position<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,0);?>">
</td>
<td>
<input type=text NAME="job<?PHP echo $i; ?>" id="job<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,1);?>">
</td>
<td>
<SELECT NAME="person<?PHP echo $i; ?>" id="person<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" onchange="get(<? echo $i; ?>);">
<OPTION VALUE=0 >
<?=$optionpeople?>
</SELECT>
</td>
<td onclick="train(<? echo $i; ?>);" style="color:grey; cursor: pointer;">
<div id="training<?PHP echo $i; ?>"><font color=grey size=2></div>
</td>
<td>
</td>
</tr>
<?PHP
$i++;
$r++;
}
?>
The second page or page called by jquery, the output is:
echo
"
<table border=0 width=400>
<tr>
<td width=20>
</td>
<td width=150>
<b>Position<b>
</td>
<td>
<b>Job<b>
</td>
</tr>
";
while($line = mysql_fetch_array($result))
{
echo "
<tr>
<td>
";
?>
<input type=checkbox name="delete[]" id="delete[]" value="<?php echo $rows['id']; ?>">
<?PHP
echo "
</td>
<td>
";
echo $line['position'];
echo "
</td>
<td>
";
echo $line['job'];
echo "
</td>
</tr>
";
}
?>
<tr>
<td>
<input type=submit name="update" id="update" value="Update">
</td>
</tr>
</table>
<?PHP
}
To repeat I want the table checkbox element from the second page posted with the form data of the parent page.
Is this possible as my testing hasnt given me any luck.
Am I doing something wrong or do I need to modify the jquery code?
Thanks as always for the assistance.
There is no second page. Really - you're loading HTML content from the second page, but it's being inserted into the parent page. All content, from your browsers perspective, is in the same page. The fields should be included as long as they're inside the DOM inside the element. Use the developer tools for your browser or Firebug for Firefox to make sure that the content is placed within the form element in the DOM. The developer tools should also be able to show you exactly which variables are submitted to the server when the form is submitted.
The 'action' parameter of 'form' will indicate where the content gets submitted (and it seems you've left that part out of your HTML, so it's impossible to say if you've left out or just not included it in the paste.

Categories