Hey so I am trying to grab the user input text within a textarea but it is not working out too well. What is happening is that we are grabbing a text (movie review) from our server and we want the user to be able to update it and then send it back to the server. Anyone know what we are doing wrong??
We arent getting any error, it just that we are unable to grab the textarea field data. We are pretty new to php and html so I am assume it is some small typeo we are overlooking.
UPDATE: Full fills here.
http://dl.dropbox.com/u/21443163/Reviews.php
http://dl.dropbox.com/u/21443163/updateReview.php
while($RecordSetMovieRow = odbc_fetch_array($RecordSetMovie))
{
echo "<tr>";
$review = $RecordSetMovieRow['Review'];
echo "<td align = 'center'>" . $RecordSetMovieRow['FirstName']. $RecordSetMovieRow['LastName'] . "</td>";
echo "<td align = 'center'><textarea name = 'textarea' rows = '5' cols= '40'>" . $review . "</textarea></td>";
$textarea = $_GET['textarea'];
$u = $Re[0];
echo "<td><form action = 'updateReview.php?id=".$RecordSetMovieRow['ReviewID']."&review=$textarea' method = 'POST'><input type='submit' value='Update'></form></td>";
echo "</tr>";
}
echo "</table>";
odbc_close($Conn);
If you want to send large blocks of data to the database then enclose everything in a form with the method=POST name/attribute
<form action="updatingScript.php" name="myForm" method="POST" >
<textarea name="textArea" rows="5" cols="40"><?=$review ?></textarea>
</form>
Then in your updatingScript.php do this
if(isset($_POST['myForm'])) {
$textInfo = mysql_real_escape_string($_POST['textArea']);
//move this info in your database
mysql_connect("localhost", "root", "");
mysql_select_db("myDb")
$query="UPDATE myTable SET userTextInfo='$textInfo' WHERE userId='$userId' ";
$result=mysql_query($query);
}
Also set error_reporting(E_ALL); at the beginning of your PHP script as this will display what went wrong (in response to your "we aren't getting any errors")
You mention method='POST' in your form definition (which is right), but attempt to check $_GET['textarea'] (which is wrong either way). I'd suggest fixing the latter: sending large blocks of text in URL itself is usually not great.
Don't forget to get rid of the &review=$textarea as well; no need to send the content twice, in two different variables. )
Your code, with just a few minor tweaks to make it get the proper data from the form. The credit goes to raina77ow, though - his answer is absolutely correct. I just saw that you requested some code, so here it is.
Also, you need to have the form tags such that the textarea is WITHIN them, otherwise it is not part of the form, and it's data does not get posted (that edit is included below).
echo '<form action = 'updateReview.php?id=".$RecordSetMovieRow['ReviewID']."' method = 'POST'>'; // Moved this outside of the while - BUT it needs to be BEFORE the <table> tag also!
echo '<table>'; // If this is not where you want your opening table tag, that's fine - but move the opening FORM tag to BEFORE the opening Table tag
while($RecordSetMovieRow = odbc_fetch_array($RecordSetMovie))
{
echo "<tr>";
$review = $RecordSetMovieRow['Review'];
echo "<td align = 'center'>" . $RecordSetMovieRow['FirstName']. $RecordSetMovieRow['LastName'] . "</td>";
echo "<td align = 'center'><textarea name = 'textarea' rows = '5' cols= '40'>" . $review . "</textarea></td>";
$textarea = $_POST['textarea']; // Changed from $_GET["textarea"] because you are using method='post' in form
$u = $Re[0];
echo "<td><input type='submit' value='Update'></td>";
echo "</tr>";
}
echo "</table>";
echo '</form>'; // Moved this to the end of the form, so data from form will get passed
odbc_close($Conn);
Related
In my program, the href redirects to another page with the $id, and I am able to edit the row with that id from that page (signoutteacher.php). No I also want to be able to transfer the location from the input box, but I need a button to do so. Does anyone have an idea of how I could do both using only one button?
while ($row = mysqli_fetch_array($result)){
$id = $row['id'];
if ($row['status']== "In"){echo "<tr class='w3-hover-green w3-large'>";
echo "<td width='10%'>".$row['name']."</td>";
echo "<td width='10%'>".$row['surname']."</td>";
echo "<td width='5%'>".$row['status']."</td>";
echo "<td width='20%'><form action='Teacher.php' method='post'><input type='text' name='location'></form></td>";
$location=$_POST['location'];
echo "<td width='10%'>"."
<a href='signoutteacher.php?myId=".$id."&myLocation=".$location."' class = 'w3-btn w3-input w3-green w3-round-jumbo'>Sign Out</a>
</td>";
echo "</tr>";
The table heading have already been printed out, and everything else works fine when not considering the location. The error message says that the index 'location' is not defined. I know that I need to submit the input, but how?
Instead of including the $id in the url, I created a form where the $id is a hidden input and sent that to the form along with the location. Now everything works fine.
I am on the process of modifying old code. I want to have set of input fields as rows in a table with Update button along with them. Below code is included in seperate ajax_edit_designation.php page which is being called by the edit_designation page. When the update is selected it should enter the new value to the database. The script required for it is written in the edit_designation.php page.
I would like to know whether what I am doing is correct or wrong. Because for some reason it doesn't carryout required action when the 'Update' button is selected. In the browser console it doesn't throw any exceptions either. What am I doing wrong?
Is it something that happen due to some kind of version issue? Or am I really doing something wrong?
echo "<table>";
while($row_tay = mysql_fetch_array($result_tay)){
$temp++;
$designation = $row_tay['designation'];
$designationID = $row_tay['designationID'];
echo "<tr ";
if($temp%2 == 1)
{
echo " bgcolor =\"#F3F3F3\" ";
}
else
{
echo " bgcolor =\"#DBDBDB\" ";
}
echo " >";
echo "<form id=\"myform".$designationID."\" method=\"post\">";
echo "<td class=\"bodytext\">";
echo "<input type=\"text\" id=\"designation\" size=\"50\" value=\"".htmlentities($designation)."\" name=\"designation\" />";
echo "</td>";
echo "<td width=\"120\" align=\"center\">";
echo "<input name=\"addcat\" id=\"addcat\" type=\"submit\" value=\"Update\" class=\"Submit_Button_long\">";
echo "<input type=\"hidden\" name=\"designationID\" value=\"".$designationID."\">";
echo "</td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
Because, same thing happened to me in another page also, where I added seperate form tags as above. In that case, previously it had given all the Update buttons under same tag. When I remove that earlier form tag and add only the new tags as above, they didn't work as now. But if I add that previously available tag, by wrapping all those forms created with the table, Update buttons worked. What could be the reason. Please help?
I am attempting to create an order form on our website but I have hit a dead end and am not sure where to go next.
I have a php file that queries a database and returns a list of item numbers and the quantity we have in stock. For each item number that shows up in the table there is an input box next to it. At the moment these input boxes all have the same Id.
My guess would be that first I need to have it so they don't all have the same ID?
Here is the form in action: http://synergysystems.com.au/soh/testmysql.php
From there, the user will fill out the form and click a submit button. This will then need to get the values from the input boxes (if greater than or equal to 1) and the corresponding item number and create a CSV file. The CSV file would just be 1st column - item number 2nd column - quantity.
I have attempted to do this with code I have found on the internet but just cant get my head around how I would go through each input box and get the values as well as the item number.
I think I may be going about it the wrong way.
Here is my current php to create the form:
<?php
echo "<div id=header>";
echo "<h1>Synergy Order Form</h1>";
echo "<p>Stock is updated every hour during business hours</p>";
echo "</div>";
echo "<br />";
echo "<link rel='stylesheet' href='table.css' type='text/css'>";
$conn = mysql_connect('localhost', '*****', '*****');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('****',$conn);
/* select only items with stock on hand */
$result = mysql_query("SELECT * FROM TABLE_SOH where (((TABLE_SOH.ITM_ONHAND) >0))",$conn);
echo "<table border='0'>
<tr>
<th>Item</th>
<th>Description</th>
<th>On Hand</th>
<th>Order Quantity</>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ITM_NO'] . "</td>";
echo "<td>" . $row['ITM_DES'] . "</td>";
echo "<td align=right>" . $row['ITM_ONHAND'] . "</td>";
echo "<td>". ("<input type ='text' name ='Item'.$i >") . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($conn);
?>
<html>
<body>
<form action="submit_order.php">
<input type="submit" name="select" value="select"/>
</form>
I don't have anything in the submit_order.php file as I have gutted it out of frustration trying to get something to work.
Even if someone could point me in the right direction.
Thank you,
Matt
If all you need to do is get all the data in all the input fields, you can just loop through $_POST in submit_order.php and toss them into the CSV.
// unset the stuff you dont want
unset($_POST['submit']);
// open a file
$fp = fopen('file.csv', 'w');
// put the items in the file
foreach($_POST as $key => $val)
if($key > 1)
fputcsv($fp, array($key, $val));
// Close the file
fclose($fp);
I am opening a file (formatted in rows and columns) with PHP, looping through the rows on the file and echoing out rows into a table that meet a certain criteria. On each row that I echo out I am wrapping it in form tags. Ultimately, if I echo out 20 rows I will echo out 20 forms, each of them having a button. I am also echoing out a column with a comments box. I want the end user to be able to go to this page, enter comments on each row, and then hit the submit button to store the results of the row into a database. I have 2 problems that I do not know how to overcome.
1.) When echoing out the results to the table I do not create a static variable to reference when I go to create my SQL insert statement. How do I label each piece of data so I can call it later?
2.) There are going to be multiple forms, and the user will only be using one at a time. How do I ensure that when the user clicks submit, it only submits the data fields from the same row?
Hopefully this makes sense. If not Im happy to add as much clarification as needed. Im definitely a newbie when it comes to PHP, so I'm certain this is a poor design but Im on a tight timeline and I just need a working product for now. Next week I can go back and perhaps implement a better solution I'm sure one you kind people will suggest:)
<?php
if (($handle = fopen("name of file to open here", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if ($data[4] == $login){
$num = count($data);
echo "<form action='analyzer.php' method='post'>";
echo "<tr>";
echo "<td>";
echo $data[1];
echo "</td>";
echo "<td>";
echo $data[2];
echo "</td>";
echo "<td>";
echo $data[3];
echo "</td>";
echo "<td>";
echo $data[4];
echo "</td>";
echo "<td>";
echo $data[5];
echo "</td>";
echo "<td>";
echo $data[6];
echo "</td>";
echo "<td>";
echo $data[7];
echo "</td>";
echo "<td>";
echo $data[8];
echo "</td>";
echo "<td>";
echo "<input type='text' name='comments' />";
echo "</td>";
echo "<td>";
echo "<input class='mybutton' type='submit' name='#' value='Submit' />";
echo "</td>";
echo "</tr>";
echo "</form>";
}
fclose($handle);
}
}
?>
1.) When echoing out the results to the table I do not create a static variable to reference when I go to create my SQL insert statement. How do I label each piece of data so I can call it later?
You need some way of uniquely identifying the piece of data. You can do this using hidden input variables.
2.) There are going to be multiple forms, and the user will only be using one at a time. How do I ensure that when the user clicks submit, it only submits the data fields from the same row?
You can name the forms or assign them a sequence in an array. For example:
<input type="submit" name="form[5]" />
You can also simplify your code by using a loop like this:
foreach( $data as $key=>$val ) {
echo "<td>$val</td>";
}
I know there is already an accepted answer here, but I feel like suggesting another way of doing this. Having a bunch of forms on one page each with their own submit button and requiring a page load every time is kind of cumbersome from a UI perspective.
My suggestion is that you start the long, painful, and rewarding process of learning Ajax. I have been learning jQuery AJAX and I use it to do similar things.
Example:
I have a table with 30 rows, each of which represent a process.
Each row has a checkbox.
I have a submit button down at the bottom, where a user can submit all the rows with checked boxes.
But, I also have buttons in each row (right next to the checkboxes actually). The buttons are simply labeled "now", and when you click them, that row is processed in the background without a page reload.
So I have allowed users to submit many rows with the normal form submit button if they want, OR to submit any individual row and not have to wait for the page to reload.
I can perhaps add some code here if anyone is ever interested.
I'm currently facing a strange issue whereby I did not get any errors from my debugging page. My table consists of several rows and only the first row of the table can't be deleted.
Sample form:
$DB = new PDO('sqlite:database/Sample.db');
$result = $DB->query("select * from staff");
foreach ($result as $row)
{
$StaffNo= $row['StaffNo'];
$Name= $row['Name'];
$TelNo= $row ['TelNo'];
echo "<tr>";
//Go to remove.php to remove
echo "<form action=\"Remove.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"StaffNo\" value=\"$StaffNo\">";
echo "<input type=\"submit\" onclick=\"return confirm('Yes/No')\"/>";
echo "</form>";
echo "</td>";
echo '<td data-column-name="Name" char="data">'.$Name.'</td>';
echo '<td data-column-name="TelNo" char="data">'.$TelNo.'</td>';
</tr>
}
Remove.php:
$StaffNo= $_POST["StaffNo"];
$DB = new PDO('sqlite:database/Sample.db');
$DB->query("DELETE FROM Staff WHERE StaffNo=".$StaffNo);
#header("location:view.php");
From my code above, I can delete all my sample records except for the first row. It doesn't get deleted... Kindly advise if i did wrong somewhere....
I've tried your code and apart from the broken table code, everything seems fine. Make sure your table is correct (<table><tr><td>Content</td></tr></table>). In your question, you're missing an opening <td> on line 9 of the first file, as well as missing <table> tags. Some browsers don't handle broken tables very well and that might mess up your form.
Your query will also break if $StaffNo is an empty string, so double check that.
You can also try removing the header() call and print out errors using $DB->errorInfo().
To inject your variable i the hidden field you should type
".$StaffNo."
instead of
"$StaffNo".
probably it doesn't delete the first row of your table becouse it's the only one with a StaffNo defined.