using a table name by php in a submit form - mysql

I want the user choose a table name in mysql database and then I can add a data by textbox of a form inside the field.
I used PHP $_POST[ ] to send table name to the adding form. but in the adding form; when I submit the add button; the table name has been lost.
I have searched a lot and tested session and constant to hold the table name value; but
didn't get the desired result.
Any help will be greatly appreciated.
here is the code of the page which selecting the table name:
<form action="change_products.php" method="post" name="select_table">
<select name="edit_products" >
<option <?php if($_POST['edit_products'] == "cabin") echo "selected='selected'"; ?> value="cabin">Cabin case</option>
<option <?php if($_POST['edit_products'] == "cabin_door") echo "selected='selected'"; ?> value="cabin_door">Cabin door</option>
</select>
<input type="submit" value="OK">
</form>
and this is the code of the adding form in "change_products.php" page.
<?
include('functions.php'); //the database info
define('selected_table', $_POST['edit_products']);
?>
<form method="post">
<table>
<tr>
<td>Mark:</td>
<td><input type="text" name="mark" /></td>
</tr>
<tr>
<td><input type="submit" name="add" value="New" /></td>
</tr>
</table>
<?
if (isset($_POST['add']))
{
$mark=$_POST['mark'];
mysql_query("INSERT INTO ".selected_table." (mark) VALUES ('$mark')");
}
?>
</form>

You didn't set $_POST['add'], thus, the last part of PHP code in change_products.php does not run.

Related

HTML - Issue with form submission

Issue with submitting form, I don't see anything wrong with the form section.
This is an example of my code, The ticket number and user ID is being pushed into from MYSQL. When it gets to the server side after submitting, I am getting only 2 of 21 items. The readonly is only on the top 2 items, the rest is entered by end user.
<form action="<?php echo $NAV_includes_Research_Upload; ?>" method="POST">
<table class="Table_Center">
<tr>
<td>Ticket number:</td>
<td><input type="text" id="ticket" class="all" value="<?php echo "1"; ?>" readonly></td>
</tr>
<tr>
<td>User ID:</td>
<td><input type="text" id="user" class="all" value="<?php echo "test"; ?>" readonly>
</tr>
<tr>
<td>Site:</td>
<td><input type="url" id="website" class="all" autofocus required <?php if ($testing === 'ON') {echo 'value="http://www.google.com"';}?>></td>
</tr>
</table>
<table class="Table_Center">
<tr>
<td><input type="reset" value="Reset"></td>
<td><input type="submit" class="f_right" value="Submit"></td>
</tr>
</table>
</form>
Thank you, it was the name section that was missing.
I will have to remember that.
In your form code, I am unable to find name attribute of input fields. In an HTML form Post, values entered by the user and submitted are called by the name attribute.

Proper way to post a new form in each row of a table?

I have a page where there is a table. In each row of the table, there is data about an online grocery delivery order. At the end of each row, there is a submit button about action to be performed on the order and the form ends there. A new form begins with the next row <tr>
Where exactly should I put the <form> and </form> tags? Should I put the <form> before each <tr> and </form> after the </tr> or should I introduce them in the first data cell <td> of each row and close in the last <td>?
I mean, the page would probably function all right in both ways, but what is "proper" way of doing this? Right now mozilla code view is showing the table tag in red color.
This is how it looks right now.
And this is the relevant part of the php code I am using to dynamically generate a new form for each table row. I am posting this just for the sake of reference, because this does not matter at all here. My question is basic HTML based, not php based.
for($i=0;$i<$count;++$i){
$res.='<form action="" method="post">' ."\n" .'<input type="hidden" name="orderid" value="' .$orders[$i]->idcode .'" />';
$res.="$nt<tr><td align=\"center\">" .$orders[$i]->display("pe","</td><td align=\"center\">") ."</td>$nt\t<td align=\"center\"><select name=\"agent\">$alistcode</select></td>$nt\t<td align=\"center\"><select name=\"vendor\">$vlistcode</select></td>";
$res.="$nt\t<td align=\"center\"><input type=\"submit\" value=\"PROCESS\" /></td>\n</tr>\n</form>\n";
}
$res.="</table>";
echo $res;
HTML5 offers decent solution for your problem. And its name is form attribute. Just put the form into last <td> and refer to its id from other inputs. See example.
<table>
<tr>
<td>
<input type="text" name="inp1" form="form1" />
</td> <!-- ^^^^ ^^^^^ -->
<td>
<form id="form1" method="get" action="/">
<!-- ^^ ^^^^^ -->
<input type="submit" name="submit1" value="submit" />
</form>
</td>
</tr>
<tr>
<td>
<input type="text" name="inp2" form="form2" />
</td>
<td>
<form id="form2" method="get" action="/"><input type="submit" name="submit2" value="submit" /></form>
</td>
</tr>
</table>
If using javascript is an option, you can work without <form> tags?
I used jQuery in this example, data is posted to itself (first argument of $.post()).
Table
<table>
<tbody>
<tr>
<td><input name="inputfield[]" type="text" value="input1" /></td>
<td><select name="selectfield[]">
<option selected value="select1-option1">select1-option1</option>
<option value="select1-option2">select1-option2</option>
<option value="select1-option3">select1-option3</option>
</select></td>
<td><a class="submit">Update</a></td>
</tr>
<tr>
<td><input name="inputfield[]" type="text" value="input2" /></td>
<td><select name="selectfield[]">
<option selected value="select2-option1">select2-option1</option>
<option value="select2-option2">select2-option2</option>
<option value="select2-option3">select2-option3</option>
</select></td>
<td><a class="submit">Update</a></td>
</tr>
<tr>
<td><input name="inputfield[]" type="text" value="input3" /></td>
<td><select name="selectfield[]">
<option selected value="select3-option1">select3-option1</option>
<option value="select3-option2">select3-option2</option>
<option value="select3-option3">select3-option3</option>
</select></td>
<td><a class="submit">Update</a></td>
</tr>
</tbody>
</table>
Script
$(".submit").on("click", function(){
var row = $(this).parents("tr");
$.post("", row.find("input, select, radio").serialize(), function(data){ console.log(data); });
});
JSFiddle

Reset a form, without javascript? (input type=reset not working)

Well, I guess the title says it all.
I'm looking for a way to reset all the fields within a form.
I've tried some of the following:
<input type="reset" value="Clear all fields">
And
<button type="reset">Clear all fields</button>
Yet, none of this seems to be working.
This is a stripped version of my form.
<form id="form2" action="mainframe.php?paso=21" method="POST">
<input type="reset" value="Reset">
<button type="reset" form="form2">Reset</button>
<?php while($row=$resultado->fetch_assoc()){ ?>
<p>Number of <?php echo $row['name']; ?>
<input type="text" name="saldo[<?php echo $row['id']; ?>]" value="<?php echo $row['saldo']; ?>" maxlength="30" />
<?php } ?>
</form>
<button type="submit" form="form2">Send</button>
Edit: Apparently the reset button will replace the values of all inputs with the values they had on page load. The button would clear all fields and leave them blank only if the input's value property aren't declared or are null on page load.
Guess what. It actually DOES work. I didn't change anything at all. I promise:
<form id="form2" action="mainframe.php?paso=21" method="POST">
<input type="reset" value="Reset">
<button type="reset" form="form2">Reset</button>
<?php while($row=$resultado->fetch_assoc()){ ?>
<p>Number of <?php echo $row['name']; ?>
<input type="text" name="saldo[<?php echo $row['id']; ?>]" value="<?php echo $row['saldo']; ?>" maxlength="30" />
<?php } ?>
</form>
<button type="submit" form="form2">Send</button>
If it's not working on your site, then you may have another syntax error.
get rid of what u echo to the input value and it should work...
The previous answers are correct. The reset button resets the form's inputs to their initial values, not to blank.
In my case, I wanted to blank all of my inputs. I thought about writing some JavaScript that sets all the inputs to "", but I decided that just reloading the page would be easier. In my case, reloading the page blanks the form.
Try this:
<input type="button" onclick="window.location='http://www.yourwebsite.com/form.php'" value="Reset" />

Populate form with customer data, if known

I am using Magento, and I have a form.html that asks for customer data. When the customer is logged in, I want to prepopulate the relevant fields with known data.
I have used the following constructs, but none work.
The listing begins with the (commented out) line that just asks for filling in, then my two modifications, the first one commented out. Both are not working. What's wrong?
<!--<td valign="top"><input maxlength="30" name="name" size="20" type="text"></td>-->
<!--<td valign="top"><echo Mage::getSingleton('customer/session')->getCustomer()->getName();></td>-->
<td valign="top">
<input type="text" name="name" value="<?php echo Mage::app()->getRequest()->getPost("name");?>" />
</td>
Your first modification (second line commented out) won't work because you haven't wrapped the PHP in its tags.
Try writing a statement to check if the customer is logged in.
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) :
//Write out the customer's full name
echo Mage::getSingleton('customer/session')->getCustomer()->getName();
else :
echo 'Customer not logged in';
endif;
?>
If there is still no value being returned it might be because you haven't got the customer's session ID.
<?php
$customer = new Varien_Object();
if (Mage::getSingleton('customer/session')->isLoggedIn()){
$customer = Mage::getSingleton('customer/session')->getCustomer();
}
?>
<td valign="top">
<input type="text" name="name" value="<?php echo $customer->getData('name') ?>" />
</td>

Item delete buttons in a table?

this is a simple html question but I would like to know the correct way of doing it.
I have a table in my page, with tabular data, and it's a display of items in the database. I build the table dynamically with php, and just output html.
I want to add a column "delete this entry" with a button/link/picture on every line, and when it's clicked, my page refreshes and php knows which itemID should be deleted.
The question is not about php, but about the html part.
How can I make the delete-button compliant with standards? Here's an incorrect solution:
<table>
<form action='#' method='post'>
<tr>
<td>Item 1</td>
<td><input type='hidden' name='id' value='1'><input type='submit' value='X'></td>
</tr>
</form>
<form action='#' method='post'>
<tr>
<td>Item 2</td>
<td><input type='hidden' name='id' value='2'><input type='submit' value='X'></td>
</tr>
</form>
</table>
Anyone an idea on a good solution? I'd prefer not to use much javascript, if it can be avoided.
Thanks in advance!
Edit: actually, I have a secret second question too: what if i were to want to do inline editing (after pressing an Edit button), as in: make the cells with the values contain boxes, and a submit button at the last cell?
Why not isolate the forms and pass the ID to the action page?
<table>
<tr>
<td>Item 1</td>
<td><form action='action.php?id=1' method='post'><input type='submit' value='X'></form></td>
</tr>
<tr>
<td>Item 2</td>
<td><form action='action.php?id=2' method='post'><input type='submit' value='X'></form></td>
</tr>
</table>
You can write names of submit buttons as set of facts and their values, for example name='set1(fact1:value2,fact2:value2,...)'. Then search for submitted set and get fact value. A PHP class PageFacts below can do this. You can check if set is posted using condition if (PageFacts::is('del')) and get value of fact with statement $itemID = PageFacts::get('del', 'id').
Form with submit buttons can look like:
<form method="post">
... item [0] text/input
<input type="submit" name="del(id:0)" value="Del">
... item [1] text/input
<input type="submit" name="del(id:1)" value="Del">
... item [2] text/input
<input type="submit" name="del(id:2)" value="Del">
...
...
<input type="submit" name="chng" value="Chng">
</form>
An example of PHP code that deletes table item with itemID can be:
if (PageFacts::is('del')) {
$itemID = PageFacts::get('del', 'id');
// Delete $itemID
deleteTableItem($itemID);
}
// or for multiple facts like del(cid:2,id:0)
if (PageFacts::is('del')) {
$cartID = PageFacts::get('del', 'cid');
$itemID = PageFacts::get('del', 'id');
// Delete $itemID from $cartID
deleteCartItem($cartID, $itemID);
}
PageFacts PHP code:
class PageFacts {
// Gets fact value from posted set
// $s ... name of a set
// $f ... name of a fact
// returns - value of fact $f in set $s
// - TRUE if set $s exists but fact $f doesn't exist
// - FALSE if set $s doesn't exist
public static function get($s, $f = NULL) {
// Regex pattern to search for set $s
$ps = "/^\s*$s\s*\((.*)\)\s*$/";
// Search POSTed names (variable $v is not used)
foreach ($_POST as $key => $v) {
$ok = preg_match($ps, $key, $matches);
if ($ok && isset($matches[1])) {
// If $f is not passed return TRUE, means set $s exists regardless of fact $f
if (!isset($f)) {
return TRUE;
}
// Otherwise return value of fact $f
// use regex pattern to search in list of fact:value items separated by a comma
$pf = "/^\s*$f\s*\:\s*(.+)\s*$/";
foreach (explode(',', $matches[1]) as $fv) {
$ok = preg_match($pf, $fv, $matches);
if ($ok && isset($matches[1])) {
return $matches[1];
}
}
}
}
return FALSE;
}
// Checks if set $s is posted
public static function is($s) {
return self::get($s);
}
}
It's kind of messy, but going off of what Agent said, you could do something like this to get a text input in there too:
<form action='action.php?id=1' method='POST'>
<table>
<tr>
<td>Item 1</td>
<td><input type="text" value="" /></td>
<td><input type='submit' value='X'></td>
</tr>
</table>
</form>
<form action='action.php?id=2' method='POST'>
<table>
<tr>
<td>Item 2</td>
<td><input type="text" value="" /></td>
<td><input type='submit' value='X'></td>
</tr>
</table>
</form>
Add a submit button to each form and send the operation you want to execute as value attribute of each button
<table>
<form action='#' method='post'>
<tr>
<td>Item 1</td>
<td><input type='hidden' name='id' value='1'><input type='submit' value='X'></td>
<td><button type="submit" value="remove" name="op">Remove</button></td>
<td><button type="submit" value="edit" name="op">Edit</button></td>
</tr>
</form>
</table>
You could go with a simple authenticated GET procedure, like this:
Delete
Note the action and item values in the URL. These would be evaluated before the database is called and the results are printed in showResults.php. Your PHP (on showResults.php) would start with a block of logic that checks to see if any actions were requested, such as delete. If an action is present, it checks to see if an item is as well. I will then do whatever authentication necessary to ensure the user has sufficient rights to do this action to this item.
Next it would query the database (now lacking said item assuming the deletion was successful) and redraw the page. The links would be built within your loop used to create the many table-rows:
<?php foreach ($products as $product) { ?>
<tr>
<td>
Delete
</td>
<td>
<p><?php print $product->title; ?></p>
</td>
</tr>
<?php } ?>