Sending php in a name value which is an html form - php

<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'];

Related

hidden values are not passing in form action

When I save this form all type="text" fields are working fine. But hidden field values are not passing.
If I changed from "hidden" to "text" it works fine. I don't know why I am getting this problem.
<form action="../model/meter-reading-model" method="POST">
<input type='hidden' name='hdnTakenDate' id='hdnTakenDate' value='<?php echo $TakenDate; ?>' />
<input type='hidden' name='hdnSiteHeadDbKey' id='hdnSiteHeadDbKey' value='<?php echo $SiteHeadDbKey; ?>' />
<button type='submit' name="btnSaveData" id="btnSaveData" class='btn btn-lg btn-danger'> Save Reading Data </button>
</form>
Receiving in another page
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($SaveData)){
$TakenDate = mysqli_real_escape_string($conn, $_POST['hdnTakenDate']);
$SiteHeadDbKey = mysqli_real_escape_string($conn, $_POST['hdnSiteHeadDbKey']);
}
}
It solved for me when i change this
<input type='hidden' name='hdnTakenDate' id='hdnTakenDate' value='<?php echo $TakenDate; ?>' />
<input type='hidden' name='hdnSiteHeadDbKey' id='hdnSiteHeadDbKey' value='<?php echo $SiteHeadDbKey; ?>' />
to this
<input type='hidden' name='hdnTakenDate' id='hdnTakenDate' value='<?php echo $TakenDate; ?>' **/>**
<input type='hidden' name='hdnSiteHeadDbKey' id='hdnSiteHeadDbKey' value='<?php echo $SiteHeadDbKey; ?>' **/>**

PHP: How to use method GET, if action already contains GET value

I have a form, something like this:
<form action='index.php?page=search&filter=1'>
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='submit'>
</form>
If I submit form, it will go to page:
index.php?ID=value1&number=value2
I would like to have
index.php?page=search&filter=1&ID=value1&number=value2
How can I append extra fields to url ?
Thank you for help.
you should put these values in hidden inputs:
<form action='index.php'>
<input type='hidden' name='page' value='search'>
<input type='hidden' name='filter' value='1'>
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='submit'>
</form>
You can submit additional GET value as a hidden field value in the form which will be submit along with form submission.
<form action="index.php" method="get">
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='hidden' name='page' value='search'>
<input type='hidden' name='filter' value='1'>
<input type='submit'>
</form>
Use hidden fields with your form and you will get exact url you want.
Try below code :
<form action="index.php" method="get">
<input type='hidden' name='page' value='search'>
<input type='hidden' name='filter' value='1'>
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='submit'>
</form>
Try using some hidden input fields and use get method in form submitting.
Try this:
<form action="index.php" method="GET">
<input type='hidden' name='page' value='search' />
<input type='hidden' name='filter' value='1' />
<input type='text' name='ID' value='value1' />
<input type='text' name='number' value='value2' />
<input type='submit' value='Submit' />
</form>

When set value of action, post/get don't work

I have form:
echo "
<form action='http://domain.com/' method='get'>
<input type='hidden' name='order_id' value='".$_SESSION['shoppingcart_id']."' />
<input type='hidden' name='account_id' value='".$account_id."' />
<input type='hidden' name='action' value='add' />
<input type='submit' class='form-button' value='Buy' />
</form>
";
and get don't get any results.
http://domain.com/
If I set value of action to empty like:
<input type='hidden' name='action' value='' />
the result is:
http://domain.com/?order_id=xxxxxxxx&acount_id=xxxxxxxxx$action=
I'm confused why with empty action get results, but with action don't get anything!?
p.s. With "post" is the same.

Removing the variables in url

Before I am told to look at other posts that are similar, please note that I have tried to apply them but to no avail.
I need to clear the variables in the url after a series of if statements are true. I have the following code:
<?php
$_SESSION['passlevel']='0';
if ($_SESSION['passlevel'] == '0')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$_SESSION['passlevel'] = '1';
// add something that removes url string
}
else
{
echo "you didn't select purple.";
}
}
else echo "login with your colorlock passcode.";
}
if ($_SESSION['passlevel'] == '1')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#DD7500")
{
echo "you have passed step two.";
$_SESSION['passlevel']='2';
// add something that removes url string
}
else
{
echo "you didn't select orange.";
}
}
else echo "enter remaining colors.";
}
?>
This is for color dependent password that has the background of each of 10 divs change to one of 10 colors after a div is clicked on (note that some of the echoes are only there for now as a method of debugging). The information is sent to the url through multiple forms:
<form>
<div id="box1" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box1color ?>'/>
<input type='hidden' name='name' value='box1color'/>
</form>
<form>
<div id="box2" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box2color ?>'/>
<input type='hidden' name='name' value='box2color'/>
</form>
<form>
<div id="box3" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box3color ?>'/>
<input type='hidden' name='name' value='box3color'/>
</form>
<form>
<div id="box4" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box4color ?>'/>
<input type='hidden' name='name' value='box4color'/>
</form>
<form>
<div id="box5" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box5color ?>'/>
<input type='hidden' name='name' value='box5color'/>
</form>
<form>
<div id="box6" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box6color ?>'/>
<input type='hidden' name='name' value='box6color'/>
</form>
<form>
<div id="box7" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box7color ?>'/>
<input type='hidden' name='name' value='box7color'/>
</form>
<form>
<div id="box8" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box8color ?>'/>
<input type='hidden' name='name' value='box8color'/>
</form>
<form>
<div id="box9" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box9color ?>'/>
<input type='hidden' name='name' value='box9color'/>
</form>
<form>
<div id="box10" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box10color ?>'/>
<input type='hidden' name='name' value='box10color'/>
</form>
An example of the url after one of the colors (purple) is selected:
http://localhost/colorlock/index.php?box_1_color=%23800080&name=box9color
So I want to remove the ?box_1_color=%23800080&name=box9color part. How could i remove it where the code says "// add something that removes url string" so that when the next block of code is initiated it doesnt run immediately with the same color code in the url before a user can select their next color?
I hope my question is clear (i understand that it a confusing) and i hope that it is answerable. Thank you.
You need to do one/some of 3 things:
Use POST. This is a good idea but it would still be best combined with...
Redirect to the same page without the URL arguments after processing the form.
Use ajax to process the user input.
The easiest of these to integrate with your existing code is probably a combination of 1 and 2. To do this, add these lines where your comment is:
header("HTTP/1.1 303 See Other");
header("Location: http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}");
exit;
...and change all the <form> elements in your HTML so that they read:
<form method="post">
...and obviously change all occurrences of $_GET in your PHP code to $_POST
Could you use POST instead of GET? You use $_POST in your PHP instead of $_GET and you never have to worry about what's in the URL - it's always clean.
Try
unset($_GET['box_1_color']);
in aproriate place of your code

How can I get multiple forms to use the same inputs in php

I have 15 identical PayPal "Buy Now" buttons each driven by its own form on a single php page. Each button has about 20 input variables but only 2 are unique to each item (item_name & item_number). Is there a way to clean up my code and have all the forms use the same input array? Not just the data, the whole string.
Thanks, Wayne
Example:
<form target='paypal' action='https://www.paypal.com/cgi-bin/webscr' method='post'>
<input type='hidden' name='cmd' value='_cart'>
<input type='hidden' name='business' value='myemail#mydomain.com'>
<input type='hidden' name='lc' value='US'>
<input type='hidden' name='item_name' value='Modern Art Print'>
<input type='hidden' name='item_number' value='MA024'>*
<input type='hidden' name='button_subtype' value='products'>
<input type='hidden' name='no_note' value='0'>
<input type='hidden' name='currency_code' value='USD'>
<input type='hidden' name='add' value='1'>
<input type='hidden' name='bn' value='PP-ShopCartBF:btn_cart_SM.gif:NonHostedGuest'>
<input type='hidden' name='on0' value='Select Size'>Buy Print
<select name='os0'>
<option value='11 x 14'>11 x 14 $30.00</option>
<option value='8 x 10'>8 x 10 $20.00</option>
</select>
<input type='hidden' name='currency_code' value='USD'>
<input type='hidden' name='option_select0' value='11 x 14'>
<input type='hidden' name='option_amount0' value='30.00'>
<input type='hidden' name='option_select1' value='8 x 10'>
<input type='hidden' name='option_amount1' value='20.00'>
<input type='hidden' name='option_index' value='0'>
<input type='image' src='https://www.paypalobjects.com/en_US/i/btn/btn_cart_SM.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'>
</form>
Create a function that builds the form. Example:
function paypalForm( $item_name, $item_number ) {
?>
<form target='paypal' action='https://www.paypal.com/cgi-bin/webscr' method='post'>
<input type='hidden' name='cmd' value='_cart'>
<input type='hidden' name='business' value='myemail#mydomain.com'>
<input type='hidden' name='lc' value='US'>
<input type='hidden' name='item_name' value='<?=$item_name?>'>
<input type='hidden' name='item_number' value='<?=$item_number?>'>*
<input type='hidden' name='button_subtype' value='products'>
<input type='hidden' name='no_note' value='0'>
<input type='hidden' name='currency_code' value='USD'>
<input type='hidden' name='add' value='1'>
<input type='hidden' name='bn' value='PP-ShopCartBF:btn_cart_SM.gif:NonHostedGuest'>
<input type='hidden' name='on0' value='Select Size'>Buy Print
<select name='os0'>
<option value='11 x 14'>11 x 14 $30.00</option>
<option value='8 x 10'>8 x 10 $20.00</option>
</select>
<input type='hidden' name='currency_code' value='USD'>
<input type='hidden' name='option_select0' value='11 x 14'>
<input type='hidden' name='option_amount0' value='30.00'>
<input type='hidden' name='option_select1' value='8 x 10'>
<input type='hidden' name='option_amount1' value='20.00'>
<input type='hidden' name='option_index' value='0'>
<input type='image' src='https://www.paypalobjects.com/en_US/i/btn/btn_cart_SM.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'>
</form>
<?
}
Assuming you are talking about cleaning the HTML markup, rather than generating the forms at the server side...
You could do it with Javascript, but it is not a good idea to rely on Javascript for your core site functionality, because it can be disabled by the user, and may break in a way you have not predicted in some browser or other.
I think that, since you are submitting the data directly to Paypal and therefore have no control over the server side, the answer to this is probably NO.
If you are talking about cleaning up the PHP code, Rijk van Wel's answer may help.
Please refer to this article: http://www.chami.com/tips/internet/042599i.html
You can create a single form with multiple submit buttons and assign both item_name & item_number as button's value. This way the inputs aren't repeated in the code and you can easily distinguish between submitted forms.
Note: the server-side code in the article is written in ASP, but in PHP it's the same. Just refer to $_POST['buttonNameHere']

Categories