How to Post $variable - php

I'm trying to post a variable to another page but when i give a manual value it works. As;
echo '<input type="hidden" name="productid" value="8" />';
and on the second page i can get it with;
$p_id = $_POST['productid'];
What i want to do is;
instead of manually written value, writing something like;
$product_id = $this->product->pr_id;
and using the $product_id in value.
I tried many kind of things like
value="<?php echo $product_id ; ?>
but didn't work. Or what is the right way to send a variable?
Any help will be appriciated.
EDIT:
1st Page;
<?php
$product_id = $this->product->virtuemart_product_id;
echo $product_id;
echo ' <input type="file" name="files[]" id="upload" size="50" class="inputbox" multiple/><br />';
echo '<input type="hidden" name="productid" value="' . $product_id . '" />';
var_dump($product_id);
?>
2nd Page;
$p_id = $_POST['productid'];
$query2=mysql_query("INSERT into jos_virtuemart_product_medias (`virtuemart_product_id`,`virtuemart_media_id`) VALUES ($p_id,$media_id) ");
this works only if i write a number manually on 1st page to value.

If $product_id is actually set, this should work:
echo '<input type="hidden" name="productid" value="' . $product_id . '" />';
or alternatively:
<input type="hidden" name="productid" value="<?php echo $product_id ; ?>" />

Related

Retrieve two GET parameters from a url and echo out in PHP

I have a form with multiple checkboxes and hidden inputs, which I'm passing to a second page using GET.
I'm then trying to retrieve the value of each checkbox and the input in a loop and echo out the combined value.
HTML:
<form action="criteria.php" method="GET">
<input name="id[]" type="hidden" value="<? echo $criteria_id; ?>" />
<input type="checkbox" name="checked[]" class="checkbox-md" id="<? echo $criteria_id; ?>" value="Y">
<button type="submit" class="btn btn-lilac" role="button">Complete</button>
</form>
PHP:
$criteria_id = $_GET['id']; //get all criteria id
$criteria_checked = $_GET['checked']; //get checked criteria id
foreach($criteria_id as $id) //get id of all checkboxes {
echo "<BR>Criteria = ".$id."Checked = ".$criteria_checked; //returns id + array?
if ($checked='Y')//check if checked {
echo "<BR>Criteria =".$id." Checked = Y";
} else {
echo "<BR>Criteria =".$id." Checked = N";
}
}
You will need to make sure that the inputs have matching array keys:
<input name="id[0]" type="hidden" . . .
<input name="checked[0]" type="checkbox" . . .
<input name="id[1]" type="hidden" . . .
<input name="checked[1]" type="checkbox" . . .
Depending on how you create these you could use the $criteria_id:
<input name="id[<? echo $criteria_id; ?>]" type="hidden" . . .
<input name="checked[<? echo $criteria_id; ?>]" type="checkbox" . . .
This way the id and checked array keys will match. All hidden inputs will be passed from the form but only the checked checkboxes, so check if the key of the id is set in the checked array:
foreach($_GET['id'] as $key => $id) {
if (isset($_GET['checked'][$key])) {
echo "<BR>Criteria =".$id." Checked = Y";
} else {
echo "<BR>Criteria =".$id." Checked = N";
}
}

Dynamic form radio buttons mess up foreach loop mysql insert

I have a dynamic form that has all dynamic arrays being set to my php mysql insert script. The records only get inserted correctly if I have all of my radio buttons checked. If I leave any unchecked random records get inserted.
It's an attendance table and the inputs below repeat for each person. I'm checking their option with the radio button.
I've looked at the js hack of assigning a hidden input and disabling it if one of my radio buttons get checked but I find that a little weird.
My radio buttons have a value of 1 attend or 2 dive and I'm checking for that value in my script. I have a +1 set on the [$key+1] but that only works if all radios are checked. I appreciate any help pointing me in the correct direction as to setting my loop to only grab the data in the array when the radio is checked.
Thanks for looking
The two offending radio buttons are setup like this with the rest off the variables being set from hidden fields and are all filled with actual values. Everything gets insert if I don't use radio buttons in the form or if they're all checked.
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].'][dive_attend]" value="'.$dive_points.'">
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].'][dive_attend]" value="'.$attend_points.'">
<input type="hidden" name="_rowid_[]" value="' .$row1['_rowid_']. '">
<input type="hidden" name="location_date[]" value="' .$location_date. '">
<input type="hidden" name="location_name[]" value="' .$location_name. '">
<input type="hidden" name="first_name[]" value="' .$row1['first_name']. '">
<input type="hidden" name="last_name[]" value="' .$row1['last_name']. '">
<input type="hidden" name="cert_agency[]" value="' .$row1['cert_agency']. '">
<input type="hidden" name="high_cert[]" value="' .$row1['high_cert']. '">
<input type="hidden" name="email[]" value="' .$row1['email']. '">
<input type="hidden" name="phone_number[]" value="' .$row1['phone_number']. '">
<input type="hidden" name="photo_release[]" value="' .$row1['photo_release']. '">
<input type="hidden" name="diver_pic[]" value="' .$row1['diver_pic']. '">
<input type="hidden" name="submitted_email[]" value="' . $submitted_email . '">
<input type="hidden" name="food_option[]" value="' .$row2['food_option']. '">
My php loop is setup like this.
foreach ($_POST['location_date'] as $key => $value) {
$_rowid_ = $_POST['_rowid_'][$key];
$location_date = $_POST['location_date'][$key];
$location_name = $_POST['location_name'][$key];
$first_name = $_POST['first_name'][$key];
$last_name = $_POST['last_name'][$key];
$cert_agency = $_POST['cert_agency'][$key];
$high_cert = $_POST['high_cert'][$key];
$email = $_POST['email'][$key];
$phone_number= $_POST['phone_number'][$key];
$photo_release = $_POST['photo_release'][$key];
$diver_pic = $_POST['diver_pic'][$key];
$submitted_email = $_POST['submitted_email'][$key];
$food_option = $_POST['food_option'][$key];
foreach ($_POST['dive_attend_points'][$key+1] as $row) {
$dive_attend = $row['dive_attend'];
if (!empty($dive_attend)) {
if($dive_attend == 1) {
$dive_points = 0;
$attend_points = 1;
} elseif($dive_attend == 2) {
$dive_points = 2;
$attend_points = 0;
}
}
}
if($dive_attend == 1 || $dive_attend == 2){
//mysql insert here.
}
}
I'm not sure why I've never seen or heard of this until I saw it in a post while doing research. To keep your arrays aligned with the same indexes is you assign an index dynamically or manually. In my case I used the same '.$row1['_rowid_'].' I was using to give my radio button group a unique name so they grouped properly. I placed it inside the empty [] for each input. This technique allowed me to not use those clunky js hacks.
HTML
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].']" value="'.$dive_points.'">
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].']" value="'.$attend_points.'">
<input type="hidden" name="_rowid_['.$row1['_rowid_'].']" value="' .$row1['_rowid_']. '">
<input type="hidden" name="location_date['.$row1['_rowid_'].']" value="' .$location_date. '">
<input type="hidden" name="location_name['.$row1['_rowid_'].']" value="' .$location_name. '">
<input type="hidden" name="first_name['.$row1['_rowid_'].']" value="' .$row1['first_name']. '">
<input type="hidden" name="last_name['.$row1['_rowid_'].']" value="' .$row1['last_name']. '">
<input type="hidden" name="cert_agency['.$row1['_rowid_'].']" value="' .$row1['cert_agency']. '">
<input type="hidden" name="high_cert['.$row1['_rowid_'].']" value="' .$row1['high_cert']. '">
<input type="hidden" name="email['.$row1['_rowid_'].']" value="' .$row1['email']. '">
<input type="hidden" name="phone_number['.$row1['_rowid_'].']" value="' .$row1['phone_number']. '">
<input type="hidden" name="photo_release['.$row1['_rowid_'].']" value="' .$row1['photo_release']. '">
<input type="hidden" name="diver_pic['.$row1['_rowid_'].']" value="' .$row1['diver_pic']. '">
<input type="hidden" name="submitted_email['.$row1['_rowid_'].']" value="' . $submitted_email . '">
<input type="hidden" name="food_option['.$row1['_rowid_'].']" value="' .$row2['food_option']. '">
The adjusted php
foreach ($_POST['location_date'] as $key => $value) {
$_rowid_ = $_POST['_rowid_'][$key];
$location_date = $_POST['location_date'][$key];
$location_name = $_POST['location_name'][$key];
$first_name = $_POST['first_name'][$key];
$last_name = $_POST['last_name'][$key];
$cert_agency = $_POST['cert_agency'][$key];
$high_cert = $_POST['high_cert'][$key];
$email = $_POST['email'][$key];
$phone_number= $_POST['phone_number'][$key];
$photo_release = $_POST['photo_release'][$key];
$diver_pic = $_POST['diver_pic'][$key];
$submitted_email = $_POST['submitted_email'][$key];
$food_option = $_POST['food_option'][$key];
$dive_attend = $_POST['dive_attend_points'][$key]
if($dive_attend == 1) {
$dive_points = 0;
$attend_points = 1;
} elseif($dive_attend == 2) {
$dive_points = 2;
$attend_points = 0;
}
//mysql insert here.
} //end of for each

Working with checkbox from form array in php

Am looping products out from product table to add them to cart table. only the selected product by checking the checkbox should be added, but if you select, the selected ones do not correspond..
HERE IS THE HTML GETTING THE PRODUCTS OUT
<html>
<form action="#" id="" class="horizontal-form" method="post">
<?php
$LISTP = "SELECT * FROM products ORDER BY id";
$sn = 0;
$stmt = $pdo->prepare($LISTP);
$stmt->execute();
while($list = $stmt->fetch(PDO::FETCH_ASSOC)){
$sn = $sn + 1;
$ID = $list['id'];
$NAME = $list['name'];
?>
<input type="checkbox" name="slected[]" class="checkboxes" value="1" />
<input type="hidden" name="productid[]" class="" value="<?php echo $ID;?>" />
<input type="text" name="name[]" class="" value="<?php echo $NAME;?>" />
<?php }?> </form>
<?php
// now when we submot the form
$slected = $_POST['slected'];
$prod = $_POST['productid'];
$name = $_POST['name'];
foreach($prod as $key => $product){
if($slected[$key]>0){
echo $product.' '.$name[$key].' '.#$slected[#$key].'--<br>';
}
// the problem is here, if you check all product it will work well, but if you check the second one
// it would echo the second one giving it the name of the first one which was not checked at all
?>
I used to do this the same way in the past, and have discovered what I think is a better way. Rather than having a hidden input that stores the ID, just use the ID as the index for all of the form variable keys:
<input type="checkbox" name="slected[<?php echo $ID; ?>]" class="checkboxes" value="1" />
<input type="text" name="name[<?php echo $ID; ?>]" class="" value="<?php echo $NAME;?>" />
Then, your PHP can be simplified:
// now when we submot the form
$slected = $_POST['slected'];
$name = $_POST['name'];
foreach( (array)$slected as $ID => $on ) {
echo $product . ' ' . $name[$ID] . ' ' . $ID . '--<br>';
}
So - basically, your $slected variable will contain an array of only items that are selected, AND you have the product ID built in to the $slected array.

Update multiple MySQL rows with one PHP submit button

I am having the hardest time figuring out something that I think should be simple. I need to update multiple rows in my database with one submit button. I have it working with a submit for each row now, but I need to combine it. Here's what I'm trying. Where have I gone wrong? (I've been going off of multiple tutorials I found online and I think I have things all mixed up).
Here's the form:
<?php foreach ($teams as $team):
$id[]=$team['id'];?>
<form action="?update" method="post">
<div class="team-box">
<h2><?php echo $team['name'] ?></h2>
<label for="name">Name:</label>
<input type="text" name="name" value="<?php echo $team['name'] ?>" />
<label for="name">Match Wins:</label>
<input type="text" name="mwins" value="<?php echo $team['mwins'] ?>" />
<label for="name">Match Losses:</label>
<input type="text" name="mlosses" value="<?php echo $team['mlosses'] ?>" />
<label for="name">Match Ties:</label>
<input type="text" name="mties" value="<?php echo $team['mties'] ?>" />
<label for="name">Game Wins:</label>
<input type="text" name="gwins" value="<?php echo $team['gwins'] ?>" />
<label for="name">Game Losses:</label>
<input type="text" name="glosses" value="<?php echo $team['glosses'] ?>" />
<input type="hidden" name="id" value="<?php echo $team['id'] ?>" />
</div>
Here's the PHP to handle the UPDATE:
try
{
foreach($_POST['id'] as $id) {
$sql = 'UPDATE teams SET
name = "' . $_POST['name'.$id] . '",
mwins = "' . $_POST['mwins'.$id] . '",
mlosses = "' . $_POST['mlosses'.$id] . '",
mties = "' . $_POST['mties'.$id] . '",
gwins = "' . $_POST['gwins'.$id] . '",
glosses = "' . $_POST['glosses'.$id] . '"
WHERE id = "' . $_POST['id'.$id] . '"';
$pdo->exec($sql);
}
}
catch (PDOException $e)
{
$error = 'Error adding submitted team: ' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
Thanks in advance!
There are a couple of things that need fixing.
The FORM must be outside the foreach.
The '...name="name" value="...', to agree with the _POST code, should read instead:
... name="name" value="...
This way, a single POST will submit, say,
name123="Rangers"
mwins174="123"
Then you need all IDs. You can do that by issuing, in the foreach, this:
<input type="hidden" name="id[]" value="<?php print $team['id']; ?>" />
This will result in HTML:
<input type="hidden" name="id[]" value="123" />
...
<input type="hidden" name="id[]" value="456" />
and in $_POST['id'] being an array containing 123, 456 and so on.
You could also put in HTML:
<input type="text" name="name[<?php print $team['id']; ?>]" value="...
so that $_POST['name'] would be a vector with the same keys as the values of id, and therefore:
foreach($id as $team_id)
{
// Pseudocode
UPDATE... SET name=$name[$team_id]... WHERE id = $team_id;
}
This way you have one SUBMIT, and multiple UPDATEs.
Since all your field names change for each UPDATE query, you will need to execute separate queries as you already do. I don't think you would be able to perform a single UPDATE query.
I don't understand, what's wrong with executing several update queries?

changing a lists sort order inline

I have a list
Each row has a common input field "sort_order" that's stored in MySQL db.
<input name="sort_order" type="text" value="<?php echo $cat['sort_order']; ?>" size="3" />
I want to be able to change the sort order inline, without going into the edit form.
How do I get all the values into the database. I think I need to loop through each row adding the sort_order[row_id] and value to an array. But I am stuck on how to achieve this.
all the values are posting according to firePHP. sort_order[50] 1, sort_order[51] 2 etc.
New Attempt at explaining:
I have a list view with 2 input fields.
<input name="cat_id" type="hidden" value="<?php echo $cat['cat_id']; ?>" />
<input name="sort_order" type="text" value="<?php echo $cat['sort_order']; ?>" size="3" />
I have a function that's called on post in the controller:
public function sortOrderUpdate(){
//collect the values of cat_id and sort_order from each input into a array
//$this->request->post['sort_order']
//$this->request->post['cat_id']
//send to the model
$this->model_cat->updateCatSortOrder($sortorderarray);
}
And the database model file function:
public function updateCatSortOrder($sortorderarray){
foreach((int)$sortorderarray as $sort){
$this->db->query("UPDATE cat SET sort_order='" . (int)$sort['sort_order'] . "' WHERE cat_id = '" . (int)$sort['cat_id'] . "'");
}
}
Whats the best way to achieve this?
Just use empty square brackets:
<input type="text" name="sort_order[]" value"<?php echo $sort_order; ?>" />
You can then access the input as an array, saving you from building it yourself. It'll be stored as $_POST['sort_order'] (or $_GET, depending on the method attribute specified in your <form> tag).
On a related note, you should probably escape $sort_order when echoing it:
<input type="text" name="sort_order[]" value"<?php echo htmlspecialchars($sort_order); ?>" />
i would not recommend using [ ] in html names.
<input type="text" name="sort_order_<?php echo $row_id; ?>" value"<?php echo $sort_order; ?>" />
say, you have 50 input fields, when posted, you can build an array this way:
$sortorder = array();
for($i=0; $i<50; $i++){
$sortorder[] = $_REQUEST['sort_order_' . $i];
}
Ok this seems to work:
<input name="cat_id[]" type="hidden" value="<?php echo $cat['cat_id']; ?>" />
<input name="sort_order[]" type="text" value="<?php echo $cat['sort_order']; ?>" size="3" />
Controller:
public function updateCatSortOrder(){
$sortvals = $this->request->post['sort_order']; //$this->request->post same as $_POST
$row = $this->request->post['cat_id'];
$n = count($this->request->post['cat_id']);
$sortorder = array();
for($i=0; $i<$n; $i++){
$sortorder[] = array(
'cat_id' => $row[$i],
'sort_order' => $sortvals[$i]
);
}
$this->model_cat->updateCatSortOrder($sortorder);
}
Model:
//UPDATE CAT SORT ORDER
public function updateCatSortOrder($sortorder){
foreach($sortorder as $sort){
$this->db->query("UPDATE cat SET sort_order='" . $this->db->escape($sort['sort_order']) . "' WHERE cat_id = '" . $this->db->escape($sort['cat_id']). "'");
}
}

Categories