UPDATE through selected radio button - php

Hi guys and sorry for my english,
I try to make a simple UPDATE through selected radio buttons, but it's not working. I have a 3 <input type='radio'> buttons in while loop and every button should do something else which I want to filtered.
$result=mysql_query("SELECT id FROM table WHERE state=0");
while ($record=mysql_fetch_array($result)):
<form method='post' action=''>
<input type='radio' name='state' value='1' /> 1
<input type='radio' name='state' value='2' /> 2
<input type='radio' name='state' value='3' /> 3
<input type='submit' value='Send' />
</form>
endwhile;
If I choose one of the three button and click on "Send", then one row (based on ID) should be UPDATE a MySQL db:
mysql_query ("UPDATE table SET state=(selected radio button number 1-3) WHERE id=$record[id]");
But, the script it must be out of the while loop and if I make it this way, it's not working because the script can't recognize which ID was chosen.
If I make it like this:
$result=mysql_query("SELECT id FROM table WHERE state=0");
while ($record=mysql_fetch_array($result)):
<form method='post' action=''>
<input type='radio' name='state' value='$record[id]' /> 1
<input type='radio' name='state' value='$record[id]' /> 2
<input type='radio' name='state' value='$record[id]' /> 3
<input type='submit' value='Send' />
</form>
endwhile;
then ID is recognize by script, but I don't know, how can I filtered which number (1, 2 or 3) is sending to UPDATE query for "state" column. It's a big dilemma for me and I can't figure out. I need to recognized ID and filter the chosen number (1-3). Can you help me out ?

There is two options.
First:
$result=mysql_query("SELECT id FROM table WHERE state=0");
while ($record=mysql_fetch_array($result)):
<form method='post' action=''>
<input type='hidden' name='recordId' value='$record[id]' />
<input type='radio' name='state' value='1' />
<input type='radio' name='state' value='2' />
<input type='radio' name='state' value='3' />
<input type='submit' value='Send' />
</form>
endwhile;
and
$recordId = (int)$_POST['recordId'];
$state = (int)$_POST['state'];
mysql_query ("UPDATE table SET state=$state WHERE id=$recordId");
Second:
$result=mysql_query("SELECT id FROM table WHERE state=0");
<form method='post' action=''>
while ($record=mysql_fetch_array($result)):
<input type='radio' name='state[$record[id]]' value='1' />
<input type='radio' name='state[$record[id]]' value='2' />
<input type='radio' name='state[$record[id]]' value='3' />
endwhile;
<input type='submit' value='Send' />
</form>
and
foreach($_POST['state'] as $key => $val) {
$recordId = (int)$key;
$state = (int)$val;
mysql_query ("UPDATE table SET state=$state WHERE id=$recordId");
}
(note that the code above is probably not syntactically correct, and given as an illustration to the idea)
Also, mixing up code and data is a bad practice; consider using prepared statements instead.

Related

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>

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

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.

load part of page when cash page in server side

Page contains product details and rate them.
Rating is 5 stars that is works with ajax.
I want to cache these pages in server side but how can i cache product details without rating.
<div class='product-container' >
<div class='product-container-right' >
<h3>product-name</h3><br/>
<p>some detail about product</p>
</div>
<div class='vote-request'>rate this product:</div><br/>
<div id='productvote-20' class='product-vote' >
<input type='checkbox' name='vote' value='1' />
<input type='checkbox' name='vote' value='2' />
<input type='checkbox' name='vote' value='3' />
<input type='checkbox' name='vote' value='4' />
<input type='checkbox' name='vote' value='5' />
</div>
</div>
I want to cache div class='product-container-right' in server side.
how every time page (div class='product-container-right') is loaded from cache read div id='productvote-20' class='product-vote' by javascript( or jquery).
you need to add AJAX request that will run once on document-ready to get rating from PHP page and update the rating
$(document).ready( function(){
$('#productvote-20').load('Rating.php?productId=1');
})
And in that PHP file, you need to output this.. but don't cache that page
<input type='checkbox' name='vote' value='1' />
<input type='checkbox' name='vote' value='2' />
<input type='checkbox' name='vote' value='3' />
<input type='checkbox' name='vote' value='4' />
<input type='checkbox' name='vote' value='5' />

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