The right post variable - php

I have a submit buttom like this:
<?php
echo "<form id='abottom' method='post'>
<button name='".$row3[$ww]."' id='".$row3[$ww]."' type='button'>More Details</button>
<input type='hidden' name='action' value='".$row3[$ww]."' />
</form>";
?>
and ids are working right of the buttons of the table rows (by Firebug). But when I want to output $_POST in a query while loop, then no one of those works for me:
<?php
echo $_POST[$row3[$ww]]." <br />";
echo $_POST['$row3[$ww]']." <br />";
echo $_POST[$row3['$ww']]." <br />";
echo $_POST["$row3[$ww]"]." <br />";
echo $_POST[$row3["$ww"]]." <br />";
echo $_POST[$row3['".$ww."']]." <br />";
?>
Which one will be the right? Those above didn't work for me.
$row3 // is a fetch result of sql3
$ww // is table rows name on which one is selected
Raw HTML output example:
<form name ='dets' method='POST'>
<input class = 'showt' name ='6' id ='6' type='button' value= 'More Details'></input>
<input type='hidden' name='data' value='6' />
<noscript><input type='submit' value='Submit'></noscript>
</form>

Much better for debugging and finding the correct variable would be to use
<?php
echo '<pre>';
var_dump($_REQUEST);
echo '</pre>';
to see which variables arrive at your script.
Update:
I think, what you need is:
<form method="post">
<input type="hidden" name="data" value="<?php echo $row3[$ww]; ?>"/>
<input type="submit" value="More Details"/>
</form>
<?php
echo $_POST['data']];
And if you have several values in $row3 then add extra forms like this:
<form method="post">
Second Data
<input type="hidden" name="data" value="<?php echo $row4[$ww]; ?>"/>
<input type="submit" value="More Details"/>
</form>
Always keep the name of the hidden input the same (data)!

Your button has type="button". This should be type="submit" or it won't submit the form it is in.
Your PHP code should look like this:
<?php
echo "<form id='abottom' method='post'>
<button name='".$row3[$ww]."' id='".$row3[$ww]."' type='submit'>More Details</button>
<input type='hidden' name='action' value='".$row3[$ww]."' />
</form>";
?>

Related

Sending php in a name value which is an html form

<form action='itemEdit.php' method='post' />
<input type='hidden' name='<?php $row['id'];?>' value='' />
<input type='submit' name='submit' value='Edit'
</form>
is it possible to send this php script to the 'itemEdit.php' page and put the row output into a php variable? I've tried this and haven't had any success. I can't figure this out by just using mySql. Thanks
You should do the following..
<form action='itemEdit.php' method='post' />
<input type='hidden' name="YouFieldNameHERE" value="<?php $row['id'];?>" />
<input type='submit' name='submit' value='Edit'/>
</form>
Then in your itemEdit.php you can access it like ...
$fieldValue = $_POST['YouFieldNameHERE'];
You need to use the name attribute to indicate what value you are passing and either use echo or a shorttag or no value will be in the name attribute:
<input type='hidden' name='id' value='<?= htmlentities($row['id']) ?>'>
<input type='hidden' name='id' value='<?php echo htmlentities($row['id']) ?>'>
If you are naming your attribute with the id, it would look like this:
<input type='hidden' name='<?= htmlentities($row['id']) ?>' value=''>
<input type='hidden' name='<?php echo htmlentities($row['id']) ?>' value=''>
The htmlentities helps protect against XSS.
Also be sure to close the submit tag.
If it is HTML5, you don't need the />
two thing one use value for not name for input value.
another use echo or <?= to print value in input
instead of this
<input type='hidden' name='<?php $row['id'];?>' value='' />
use
<input type='hidden' name='inputname' value='<?php echo $row['id'];?>' />
to get value in next page
echo $_POST['inputname'];

Radio Button with PHP

I wanted to know why I am not able to fetch the value of radio button into the PHP Variable. Thanks in advance!
PHP CODE:
$Type = (isset($_GET['type'])?$_GET['type'] : null);
echo "<b>Type</b>";
echo $Type;
Here no value in the type variable is getting displayed.
HTML CODE:
<input type = 'radio' name='type' value='Residential' checked<?PHP print $Type;>> Residential<br>
<input type = "radio" name="type" value="Commercial" <?PHP print $Type;?>> Commercial<br>
First, you need a form with a method (whether you want to use post or get) and an action, which will be the URL where to go to, in this case it's the same page (<?php echo $_SERVER['PHP_SELF']; ?>)
<?php
$Type = (isset($_GET['type'])?$_GET['type'] : null);
echo "<b>Type</b>";
echo $Type;
?>
<form name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type='radio' name='type' value='Residential' checked<?php echo $Type;?>> Residential<br>
<input type="radio" name="type" value="Commercial" <?php echo $Type;?>> Commercial<br>
<input type="submit" value="submit" />
</form>
And you forgot the submit button to submit the form? Or you didn't, but just posted incomplete code. Anyway, above code should work.
<?php
print_r($_GET);//print values from form
?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type='radio' name='type' value='Residential'> Residential<br>
<input type="radio" name="type" value="Commercial"> Commercial<br>
<input type="submit" value="submit" />
</form>

How to echo multiple select dropdown into a textarea with PHP

I have working on this for hours and searched the net since last night but to no avail. I am trying to collect multiple options selected from a dropdown menu in a form and echo it into the textarea of the form on the next page.
I am able to echo the multiple options but cannot figure out how to echo the selectted options directly into the textarea of the form on the next page. Please help.
Here is the form..
<html><head>
</head><body>
<center><form action="92.php" method="post">
<select name="cat[]" size="9" required multiple><option value="Africa">Africa</option>
<option value="App">App</option>
<option value="Art">Art</option></select><br /><br />
<input type="submit" name="submit" value="Submit">
</form></center>
</body></html>
Here is the second form 92.php...
<?php
$cat = $_POST['cat'];
foreach ($cat as $s) {
echo "<center><form action=max21.php method=post>
Your ID: <input class=text size=9 name=us type=text readonly><br /><br />
<textarea rows=4 cols=35 id=ans name=ans maxlength=140>$s</textarea><br /><br />
<input type=submit id=submit value=Submit></form></center>";
}
?>
Thanks a ton in advance!
<center><form action=max21.php method=post>
Your ID: <input class=text size=9 name=us type=text readonly><br /><br />
<textarea rows=4 cols=35 id=ans name=ans maxlength=140>
<?php
$cat = $_POST['cat'];
foreach ($cat as $s) {echo $s. ' ';
}
?>
</textarea><br /><br />
<input type=submit id=submit value=Submit></form></center>
This should work. Simple, start loop INSIDE textarea. The rest on 92.php page is ordinary HTML - no need to echo form.

Post values to form fields?

I have a form for adding products and their detail to a mysql db.
I have another form that shows the products where I can remove the product from the db, I can also hide or show the product in the website.
I need to be able to edite the product details too. Is there any way that when I choose to edit a products I can get it details to appear in the first form again? The form is for adding details so can it be used to edit them too?
This is my code for first product adding form.
<form class='productsaddform' action='productsadd.php' method='post'
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<?php
include '../inc/categorydropdown.php';?>
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file"
id="image_upload_box" /></p>
<b>Name</b><br /><input type=text name="aname" /><br />
<b>Brand</b><br /><input type=text name="abrand" /><br />
<b>Code</b><br /><input type=text name="acode" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea>
<br />
<b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
<b>Price</b><br /><input type=text name="aprice" /><br />
<p><label for="cat">Category</label>
<select name="cat" id="cat">
<?php echo $op;?>
</select><br />
<label for="subcat">Subcategory</label>
<select name="subcat" id="subcat"> </select></p>
<br />
<br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/add_products.php'; ?>
</form>
And this is the form to display the products
<form class='productsremoveform' action='productsadd.php' method='post'>
<?php include '../inc/categorydropdown.php'; ?>
<?php include '../inc/remove_products.php'; ?>
<span class='formheading'>Remove/Hide/Show Products</span><br /><br />
<p><label for="cat">Category</label>
<select name="cat" id="removecat"> <?php echo $op;?> </select><br />
<label for="subcat">Subcategory</label>
<select name="subcat" id="removesubcat"> </select>
<input type='submit' name='show' value='Show' /> </p>
<?php
include '../inc/connect.php';
if(isset($_POST['show'])){
$subcat = intval($_POST['subcat']);
$q = "SELECT * FROM products WHERE subcat = $subcat";
$result = $link->query($q);
if($result){
while($row=mysqli_fetch_array($result)){
echo "<div class='removeproducts'>";
echo "<input type='checkbox' name='remove[{$row['id']}]' value='Remove'>Remove";
echo "<br />";
echo "<input type='checkbox' name='edit[{$row['id']}]' value='Edit'>Edit";
echo "<br />";
if ($row['status'] == 1){
echo"<input type='checkbox' name='hide[{$row['id']}]' value='Hide'>Hide";
echo "<br />";
}
if ($row['status'] == 2){
echo"<input type='checkbox' name='show[{$row['id']}]' value='Show'>Show";
echo "<br />";
}
echo "<br />",
"<span class='productthumbcode'>",
"{$row['code']}",
"</span>",
"<div id='thumb'>",
"<img class='resizedimage' src='{$row['image']}' alt='{$row['name']}' />",
"</div>",
"</div>";
}
echo "<br style='clear:both' />";
}
else{
echo mysqli_error();
}
}
?>
<input type='submit' id='submit' name='submit' value='Submit' />
</form>
EDIT;
This is the form on my new edit page.How do i get the relevent details from the db into the input values?
<?php if (isset($_GET['pid'])) { ?>
<form class='productsaddform' action='edit_products.php' method='post'
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<span class='formheading'>Edit Product</span><br /><br />
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file"
id="image_upload_box" /></p>
<b>Name</b><br /><input type="text" name="aname" />
<br />
<b>Brand</b><br /><input type=text name="abrand" /><br />
<b>Code</b><br /><input type=text name="acode" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea>
<br />
<b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
b>Price</b><br /><input type=text name="aprice" /><br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/edit_products.php'; ?>
</form>
<?php } ?>
This is what i have now, but the form doesnt display now
<?php if (isset($_GET['pid'])) {
$q = mysqli_query($link, "SELECT * FROM products WHERE id = '".$_GET['pid']."'") or
die (mysqli_error());
$row = mysqli_fetch_array($q); ?>
<form class='productsaddform' action='edit_products.php' method='post'
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<span class='formheading'>Edit Product</span><br /><br />
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file"
id="image_upload_box" /></p>
<b>Name</b><br /><input type="text" name="aname" value=<?php"$row['name'];"?> />
<br />
<b>Brand</b><br /><input type=text name="abrand" /><br />
<b>Code</b><br /><input type=text name="acode" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea>
<br />
<b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
<b>Price</b><br /><input type=text name="aprice" /><br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/edit_products.php'; ?>
</form>
<?php } ?>
EDIT:
Now I want to be able to search products by code in my first form then have that products details displayed in the form in my edit_product page as it is when i edit a product as we sorted. Any idea how to go about this?
This wouldn't be the necessarily the cleanest way to do it, but you could get the information from the db (as you would normally), put it into an array and for each input have something like this:
<input type="text" name="aname" value="<?php echo (isset($array['name'])) ? $array['name'] : ''; ?>" />
Basically, it's an inline if statement that checks to see if the variable is set and if it is, it sets the value of the input to that.
Hope this helps!
If you have any questions on this, let me know :)
EDIT
To answer your question from the comments you could have a link for each product which then takes you to an edit product page e.g.
Link:
AND then just have a page similar to you first page that checks to see if the get variable is set.

Submitting a PHP form with multiple submit buttons

EDIT AGAIN: NEW REVISED VERSION
So I have corrected the lack of tag in my table, I have added id's onto my forms, I have added a hidden field to each form with the same value as the form id also (as suggested)
I was trying to modify my original if isset to include form id's but thats where I have came a little unstuck.
As mentioned, here is the HTML that the page creates:
<br><table border=1><tr><th>customer</th><th>product</th><th>employee</th><th>return_date</th><th>complete</th></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td> <FORM id="1" method ="post" action ="g.php">
<input type='hidden' name='x' value='1'>
<Input type = 'Radio' Name ='ir' value= '47'> 47 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td> <FORM id="2" method ="post" action ="g.php">
<input type='hidden' name='x' value='2'>
<Input type = 'Radio' Name ='ir' value= '48'> 48 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>02-10-12</td><td> <FORM id="3" method ="post" action ="g.php">
<input type='hidden' name='x' value='3'>
<Input type = 'Radio' Name ='ir' value= '49'> 49 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>29-09-12</td><td> <FORM id="4" method ="post" action ="g.php">
<input type='hidden' name='x' value='4'>
<Input type = 'Radio' Name ='ir' value= '50'> 50 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td> <FORM id="5" method ="post" action ="g.php">
<input type='hidden' name='x' value='5'>
<Input type = 'Radio' Name ='ir' value= '51'> 51 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td> <FORM id="6" method ="post" action ="g.php">
<input type='hidden' name='x' value='6'>
<Input type = 'Radio' Name ='ir' value= '52'> 52 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>02-10-12</td><td> <FORM id="7" method ="post" action ="g.php">
<input type='hidden' name='x' value='7'>
<Input type = 'Radio' Name ='ir' value= '60'> 60 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>02-10-12</td><td> <FORM id="8" method ="post" action ="g.php">
<input type='hidden' name='x' value='8'>
<Input type = 'Radio' Name ='ir' value= '61'> 61 <input type="submit" name="return"/>
</FORM>
</td></tr><br></table>
<fieldset>
<legend>???</legend>
<form action="z.php" method="post">
Customer Name: <input type="text" name="ir" /> <br><br>
<input type="submit" name="return"/>
</form>
This is the PHP page that is creating this HTML (with my porly attempted form id test):
<?php
$db_host = "localhost";
$db_username = "root";
$db_pass = "usbw";
$db_name = "test";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
$result = mysql_query("SELECT * FROM rental WHERE customer = '$_POST[customer]' AND complete = '0' ") or die (mysql_error());
?>
<?php
echo '<br>';
echo '<table border=1>';
echo '<tr><th>customer</th><th>product</th><th>employee</th><th>return_date</th><th>complete</th></tr>';
echo "<br>";
$x=1;
while($row=mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>';
echo $row ['customer'];
echo '</td><td>' ;
echo $row ['product'];
echo '</td><td>';
echo $row ['employee'];
echo '</td><td>';
echo $row ['return_date'];
echo '</td><td>';
$cur_return = $row ['rental_id'];
?>
<FORM id="<?php echo $x; ?>" method ="post" action ="g.php">
<input type='hidden' name='x' value='<?php echo $x; ?>'>
<Input type = 'Radio' Name ='ir' value= '<?php echo $cur_return ?>'> <?php echo $cur_return ?>
<input type="submit" name="return"/>
</FORM>
<?php
echo '</td></tr>';
echo '<br>';
$x++;
}
echo '</table>';
?>
<fieldset>
<legend>???</legend>
<form action="z.php" method="post">
Customer Name: <input type="text" name="ir" /> <br><br>
<input type="submit" name="return"/>
</form>
<?php
if (isset($_POST['return']))
{
if ($_POST['x']==1)
{
mysql_query("UPDATE rental SET complete = '1' WHERE rental_id = '$_POST[ir]'") or die (mysql_error());
}
}
?>
I really do appreciate all the help you guys are suggesting, and apologies for the time it's taken me to get this sorted :)
Rich
Wait wait. You have multiple rows of data here and you want to submit one specific row of data, but have it all in the same form? I'm a little confused. It looks like what you're really looking for here is to generate a different form element for each row. But you cannot have multiple submit buttons submit different parts of the same form, that's not how forms work. A submit button will submit all data belonging to the form.
In that case, just move the opening form tag to the very beginning of the inside of your while loop, and move your submit button and closing form tag into the bottom of your while loop. It will generate each form and still maintain the structure of your table. But you cant have one form that submits only certain parts of itself, that goes against basic HTML principles.
I'd strongly suggest slapping an ID on each form, for future reference and update purposes.
Good luck.
You can do this with javascript, but not with php. But it's overcomplicating for no reason. Make different forms and assign them to different IDs. You can use the same method="post" and the same action="g.php". To differ on g.php, just put this in each form:
<input type='hidden' name='x' value='1'>
and then
<input type='hidden' name='x' value='2'>
Then it's a simple if $_POST['x']==1
Also, make sure you protect g.php, that's a pretty insecure name for a file that's handling forms.

Categories