Replace html button value with php and store it in database? - php

//Following code to generate 25
Button with unique name for each
button
// 25 buttons contains value
from 1 to 25
// like
btn1='1',btn2='2' ... btn25='25'
<?php
$j=0;
for($i=1;$i<=25;$i++)
{
$j++;
?>
<td><input type='button' id='btn' name="btn<?php echo $i;?>" value='<?php echo $i;?>' onclick="cnt(btn<?php echo $i;?>);" ></td>
<?php
if($j==5)
{
echo "</tr>";
echo "<tr>";
$j=0;
}
}
?>
// and using concantation i'm inserting button values seqhence into database
// like user will click on first 5 button i will insert seq=1-2-3-4-5 which are html button values
// and then using php insert query i'm inserting the sequene in db
// concantation code i'm not given here
$con="INSERT INTO `vector`.`signup` (`userid` ,`seq`)VALUES ('".$usr."', '".$seq."')";
mysql_query($con) or die ('Bad Input');
?>
see this databse picture
http://i53.tinypic.com/1zudjo.jpg
Now my prb is i want to store different values in database .. for each buton
like btn1='1' has value 1 when user will click i want to store diffvalue like 'a1' . which should not
available to user on client side .. i want to do with php ..
how can i change html button value with php code to store in databse...
i want btn1='1' to be btn1='a1' in database..

$databasevalue = 'a' . $_POST['btn1'];
Seems rather obvious... just prepend whatever you want to the value coming out of the form.

Related

creating dynamic variables in php to update multiple rows in MYSQL

I am currently working on an update profile section of a website I am creating for fun. In this part of the update profile business owners can update their restaurant menu (menu consists of category (appetizers, entrees, etc), menu item and allergens).
Right now the website is printing out what the business owner has previously submitted in input box format, this way business owners can simply just erase and re-enter their new information. However, we don't know exactly how many menu items each restaurant has so I devised a system to dynamically update each item being altered row by row.
<?php
$sql="SELECT * from menu_item as m, allergen as a WHERE a.restaurant_id=m.restaurant_id AND m.menu_item_id=a.menu_item_id AND m.restaurant_id='".$rest_id."'";
$result11=mysqli_query($con,$sql);
if (mysqli_num_rows($result)==0){
echo "<strong>You have not submitted any menu information</strong><br><br>";
echo "Please enter your menu information here: ";
echo 'Create Menu';
}
else{
$i=0;
echo "<table border='1' cellpadding='10'><tr><th>Category</th><th>Menu Item</th><th>Allergen</th></tr>";
while($rows=mysqli_fetch_assoc($result11)) {
echo '<tr><td><input type="text" name="category'.$i.'"value="'. $rows['category']. '"</td><br>';
echo '<td><input type="text" name="menu_item'.$i.'" value="'. $rows['menu_item']. '"</td><br>';
echo '<td><input type="text" name="allergen'.$i.'" value="'. $rows['allergen']. '"</td><br>';
echo '<td><input type="hidden" name="id'.$i.'" value="'. $rows['menu_item_id']. '"</td></tr><br>';
$i++;
}
}
echo "</table>";
var_dump(mysqli_num_rows($result11));
// var_dump($_POST);
$count=0;
while ($count<=mysqli_num_rows($result11)){
${"category".$count}=mysqli_real_escape_string($con, $_POST['category'.'$count']);
$count++;
}
var_dump($category0);
?>
This is where the while loop near the bottom comes into play. I want to be able to dynamically create variables for category, menu item, and allergen. Then I want to be able to create $result variables within this same while loop (mysqli_query) and then update rows accordingly. However, right now my very last var_dump is returning a value of "" which tells me I'm either concatenating the html name attribute wrong(first while loop) or there is something wrong with concatenation in my last while loop. Does anyone know what I'm doing wrong?
$_POST['category'.'$count'] in your second loop should be $_POST['category'.$count]. You also only want to run this code after the form has been submitted (I'm assuming the code you've posted is not your full script so it's not clear if that's what is happening) - otherwise you'll get the original values rather than any changes the user has made in the form.
In general you'll have an easier time if you can get the submitted data into a multi-dimensional array which you can loop through, instead of having to dynamically create variables. See the section "How do I create arrays in a HTML form?" on http://php.net/manual/en/faq.html.php. In your case I'd do something like:
// (in your first while loop)
echo '<input type="text" name="menu['.$i.'][category]" value="'. htmlspecialchars($rows['category']). '"><br>';
echo '<input type="text" name="menu['.$i.'][menu_item]" value="'. htmlspecialchars($rows['menu_item']). '"><br>';
// (etc.)
You then loop through this with something like:
foreach ($_POST['menu'] as $menuRow) {
// you now have:
// $menuRow['category']
// $menuRow['menu_item']
// ...and so on
}
You also want to escape the input values you are outputting with htmlspecialchars() as I have above.

Php check box array session

I am new to Php. I got a checkbox array and button(they are not <form>). Once the checkbox have checked and the user click the button. It will update the value of the session. Which display how many checkbox it clicked. My problem is the code is not really working.
It is my code:
<?php
session_start();
$add = 0;
$_SESSION["add"] = $add;
if(isset($_POST['checker']) && (isset($_POST['click']))){
if (is_array($_POST['checker'])){
foreach ($_POST['checker'] as $value) {
echo $value;
$add++;
}
}
echo "<b>You have clicked".$add." box.</b>";
echo "<tr><td><input type='checkbox' name='checker[]' value=''/></td></tr>";
}
P.S I use mysql to retrieve the row count into the table so there will be 10 checkbox appear.
when you click on button what happens? You have to use javaScript to capture the button click event or if the php code is in same file use this <form action="#"> it will load again and check if the $_POST['checker'] isset.

update quantity of input box specific to each id using session array

I am making a shopping cart using arrays and sessions.
I would like to update the quantity of an item that has a specific id number, without updating the other quantities which have different id number.
My code is bellow and to run it, you must get id from URL.
So just put ?id=1 in front of the .php in the address bar (as I am sure everyone is aware of more than me).
Cart.php
<?php
session_start();
// set the array and store in session
$_SESSION['items'][]=array(
'id'=>"'".$_GET['id']."'",
'qty'=> 1
);
// Display each array value stored in the session
foreach ($_SESSION['items'] as $value) {
// Display the id
echo 'The ID is ' ."''''" . $value['id'] ."''''" ;
// Display the Qty
echo 'the QTY ' ."''''" . $value['qty'] ."''''" ;
echo "<br/>";
//Echo a form that displays the Qty in a input box,
// My problem is that, I cant update the qty value in the input box where each id is different.
echo "<form method='post' action='id.php' name='edit'>
<p>Quantity: <input type=\"text\" size=\"20\" name='".$value['id']."' value='".$value['qty']."'/> </p><br>
<input class='save' name='update' value='update' type='submit'>
</form>";
}
// check if the update button is submited so that the qty value can be changed where the id number of the selected qty input box is edited
if (isset($_POST["update"])) {
$_SESSION['item'][$id]= $_POST["'".$value['id']."'"];
}
else
{
echo "quantity is updated";
}
?>
Unless you are making a $_SESSION['items'] Array of Arrays, change
$_SESSION['items'][]
to
$_SESSION['items']
Otherwise, you'll need a loop within your loop.
Right now, $value in
foreach ($_SESSION['items'] as $value) {
really represents an Array, which has your other Array in it. Additionally, you should not have more than one form with the same name. Don't put a form in a loop unless you're changing the name at each step of the loop.

Taking input from 3 different page forms in php and echo in 1 page

So i have 3 pages with 3 different forms to take input, each with a submit button to save the input values in session variables and echo them all in 1 page, but i end up just printing each page output alone, how can i save the session variable so that i could print them all in 1 page from 3 different pages. Is there a form action to save the input data on clicking? or the form actions is just used to redirect to a page/file.
I have each input form in 1 php file but the output session file is 3 in 1
This is the input form:
echo'<form name="Calzone" action="Submit.php" method="POST">';
echo $P1.' :'.'<br>'.'<br>' ;
echo 'Enter Quantity <input type="text" name="Calzone1">';
echo '<br>';
echo $P2.' :'.'<br>'.'<br>' ;
echo 'Enter Quantity <input type="text" name="Calzone2">';
echo '<input type="submit" value="Submit"></form>';
This is to get the input from only one of the pages:
<?PHP
/*Calzones*/
echo 'Calzones Order Details: '.'<br>'.'<br>';
/* First Calzone*/
foreach($xml->CategoryC as $CategoryC)
{
$Cprice1=$CategoryC->Calzone1->price;
$Cprice2=$CategoryC->Calzone2->price;
$_SESSION['Calzone1'] = isset($_POST['Calzone1']) ?$_POST['Calzone1'] : "";
$_SESSION['Sausage Total'] = 0;
$_SESSION['Grilled Chicken Total'] = 0;
$_SESSION['Calzone Total'] = 0;
So the way I understand your question, you have a wizard-type form.
There are a few options :
Store the values in sessions. You already posted code suggesting you are using sessions, so that may be great.
Store the values in a database and retrieve them when you need them.
Generate the second form with the values of form1 hidden, and so on - see the code segment below.
foreach ($_POST as $form_field => $form_value)
echo ';

submit all users values from form into db

This gives lists all people in the db and give a drop down for each one of them i want to make it so when i hit one submit button it enters individual values for each person.
so if you make yes for bobby no for mark and yes for dustin you can the pres submit and it will enter that for there values
$results = mysql_query("SELECT * FROM `$tabeluser` WHERE buss='$buss' ORDER BY id DESC LIMIT 1");
while($rows = mysql_fetch_array($results) )
{
fn = $_POST['firstname'];
echo $fn;
?>
<form>
<select name="check">
<option>no</option>
<option>yes</option>
</select>
<?php
<input type="submit" name="submit">
?>
<form>
<?php
}
mysql_query("INSERT INTO `$fn` (buss) VALUES ('$_POST[check]')");
First of all, you create a <form> and a submit button for each of the records you have. That is wrong, since you want to update multiple values at once.
What it should look like would be:
<form>
<?php
while($rows = mysql_fetch_array($results)) {
print '<select name="check[]"> .. </select>';
}
?>
<input type="submit" name="submit" />
</form>
Secondly, your code is formatted as if you are expecting to get $_POST[check] right after sending the code to the browser. That is not how PHP works. You need to separate the logic of having posted values, before printing the actual page contents. PHP is server side, which means that it won't get any data, unless the script is called with it from the beginning. So, that should look something like:
<?php
if (isset($_POST["check"])) {
// handle posted data.
}
else {
// show form
}
// or show form here, without an else, if you want to always show a form,
// even when you have posted values.
?>
Last but not least, you need to find a way to know which posted value belongs to each of your records. The way you have it now (<select name="check">') it will just return you one single value.
If you write it the way I intentionally did above (`) you will get all values, but still you won't be able to easily recognize which value is for each record.
Instead, you may want to do a final result of something like:
<?php
// get mysql records into an array (say $my_array)
if (isset($_POST["submit"])) {
foreach($my_array as $record) {
if (isset($_POST["select_of_id_".$record["id"])) {
// insert additional value into db
}
}
}
print '<form>';
foreach($my_array as $record) {
print '<select name="select_of_id_'.$record["id"].'">';
print '<option value="0">no</option><option value="1">yes</option>';
print '</select>';
}
print '<input type="submit" name="submit"/>';
print '</form>';
?>
Changes required in your code :-
<select name="check[]">
<option value="<?php echo $userId;?>_0">no</option>
<option value="<?php echo $userId;?>_1">yes</option>
</select>
You should make changes in you DB It help to easy maintaing your data.
Create new table where you can save multiple user check data with respective Post
for e.g post_id user_id check
101 111 0
101 112 1
How you can store data from you html
In you post array you will get check array in which you will get multiple check value like this
101_0 if no selected and 102_1 if yes selected. Now you need to explode this value.
$_POST['check'] = array(0 => `101_0`, 1 => 102_1);
Inside loop you can extract userid and respective checked value.
for e.g
$checked = explode('_','101_0');
$userId = $checked[0];
$check = $checked[1];

Categories