Im trying to update a drop down list with the countries of the world from a db query, which is working fine. I want to set the country of the user as selected, based on their IP. I am collecting their IP fine and working out their country, but Im struggling to set the selected in the output based on this.
<?php
include_once 'database.php';
$result = mysqli_query($conn,"SELECT *
FROM tbl_fm_countries
order by Country Asc");
$ipcountry = "United Kingdom";
?>
<form action="results.php" method="post" id="foodmilesTracker">
<fieldset>
<legend>Where are you?</legend>
<ol>
<li>
<label for="countryTo">Your Location</label>
<select name="countryTo">
<?php
// output data of each row
while($row = $result->fetch_assoc()){
echo "<option value='" . $row['CountryId'] . "'>" . $row['Country'] . "</option>";
}
?>
</select>
</li>
</ol>
</fieldset>
I have tried lots of options and seem to get all set as selected or none. For the example I am setting ipcountry manually.
I recommend doing it following way
<?php while($row = $result->fetch_assoc()){ ?>
<option value="<?php echo $row['CountryId']; ?>" <?php echo $row['Country'] == $ipcountry ? 'selected':''; ?>><?php echo $row['Country']; ?></option>
<?php } ?>
I have tried to solve it and look around but not even sure what I should be searching for.
I have made a product grid through a while loop, in the loop with each product and input-tag has been used for users to mark how many items of each product is wanted.
The product grid
However I have trouble distinguishing the value of each input field and what "name" it should be stored under to be able to retrieve it when running the second script of processing the order? I also need to be able to connect an id with each value.
The code for the grid:
I know I need to make a unique name in the input name, however how and which makes sense?
<div id="content">
<h1>Products</h1>
<form action="processorder.php" method="post">
<table align="center">
<?php
$db = include "connect2db.php";
mysqli_set_charset($db,"utf8");
$query = 'SELECT * FROM products_josie';
$result = $db->query($query);
$count = 0;
while($res=$result->fetch_assoc())
{
if($count==3)
{
echo '</tr>';
$count = 0;
}
if($count==0)
echo '<tr>';
echo '<td>';
?>
<a href="productsdetails.php?clickedid=<?php echo $res['product_id']?>">
<img src="products/<?php echo $res['photo']; ?>" width="200" height="150"/>
</a>
<br/>
<?php
echo '<p>';
echo $res['product_name'];
echo '</br>';
echo 'DKK ';
echo $res['price'];
echo '</p>';
echo '<p>';
echo '<input type="number" name="amount" min="0"';
echo '</p>';
$count++;
print '</td>';
}
if($count>0)
print '</tr>';
?>
</table>
<input type="submit" value="Submit Order">
</form>
</div>
I think what you are looking for is how to get the amounts for different products. If so, something like this might do it:
$prod = $res['product_name'];
echo '<input type="number" name="amount[$prod]" min="0"';
I start with the following:
<form action="" method="post" enctype="multipart/form-data" id="form">
<?php foreach($attributes as $attribute){ ?>
<input type="checkbox" name="attribute[][attr_id]" value="<?php echo $attribute['attribute_id']; ?>">
<input type="text" name="attribute[][attr_name]"> value="<?php echo $attribute['attribute_name']; ?>">
<?php } ?>
</form>
So each $attribute has a checkbox and a text input; whenever someone would check (one or more boxes) and would insert text (only to the checked items) I want to get in the DB the [attr_id] and the [attr_name] for the specific item.
So I continue with the following:
if(isset($_POST['attribute'])){
foreach($_POST['attribute'] as $attribute){
$attr_id = $attribute['attr_id'];
$attr_name = $attribute['attr_name'];
"INSERT INTO " . DB_PREFIX . "attribute_xref SET productid = '" . $productid . "', attribute_id='". $attr_id ."', attribute_name='" . $attr_name . "'";
}
}
But, the result is a little different as of I would have expected. Every time a box is checked and its text input is typed, their values are sent to two different DB rows:
productid -- attribute_id -- attribute_name
10 -- 102 -- empty
10 -- 0 -- somename
On the above second row the attribute_id has zero value for not being checked.
I cannot get the whole picture where is my mistake.
Finally. The tricky answer was to add an identical numerical index to the associative inputs like this:
<form action="" method="post" enctype="multipart/form-data" id="form">
<?php foreach($attributes as $attribute){ ?>
<input type="checkbox" name="attribute[i][attr_id]" value="<?php echo $attribute['attribute_id']; ?>">
<input type="text" name="attribute[i][attr_name]"> value="<?php echo $attribute['attribute_name']; ?>">
<?php } ?>
</form>
where "i" in my case would take the variable number from 'attribute_id':
<input type="checkbox" name="attribute[<?php echo $attribute['attribute_id']; ?>][attr_id]" value="<?php echo $attribute['attribute_id']; ?>">
<input type="text" name="attribute[<?php echo $attribute['attribute_id']; ?>][attr_name]"> value="<?php echo $attribute['attribute_name']; ?>">
Hopefully my answer would help somebody in the future, too.
I am getting data from a mysql DB using PHP and then displaying it inside a DIV on a web page.
the data Im fetching is a list of task and I want the users to be able to delete a task once completed.
Image here:
http://www.picpaste.com/Tasks-esmvwRPX.jpg
(apparently I need 10 reputation points before I can embed a picture in my post!)
This is my PHP code used to display the date:
<section class="tasks">
<div id="all-tasks">
<fieldset class="tasks-list">
<?php
$strSQL = "SELECT Tasks.bolComplete, Tasks.strTask, Tasks.datDueDate FROM Tasks WHERE Tasks.idPeople ='" . $_SESSION['UserID'] . "' AND Tasks.bolComplete ='0' ORDER BY Tasks.datDueDate";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo '<label class="tasks-list-item left">';
if ($row['bolComplete'] == 1) {
echo '<input type="checkbox" name="task_1" value="' . $row['bolComplete'] . '" class="tasks-list-cb" checked>';
}
else {
echo '<input type="checkbox" name="task_1" value="' . $row['bolComplete'] . '" class="tasks-list-cb" unchecked>';
}
echo '<span class="tasks-list-mark"></span>';
echo '<span class="tasks-list-desc">' . $row['strTask'] . '</span>';
// Convert Date format
$originalDate = $row['datDueDate'];
$newDate = date("d-m-y", strtotime($originalDate));
echo '<span class="tasks-list-date">' . $newDate . '</span>';
echo '</label>';
$i++;
}
?>
</fieldset>
</div>
</section>
How can I capture the index of the row displayed so I can do my delete query against the task that is selected?
I have a primary key on idTask (not displayed in the task list) which I need to capture for my delete statement.
Any help is greatly appreciated.
Thanks in advance.
M.
you can assign you idTask to your lable i.e.
<label id="idTaskXXX" class="tasks-list-item left">
...
<lable>
then where ever your code is which is checking for what row is clicked read the id field and use that for deletion
here is an example of how to get id from element: click here
<label id="task1" value="Change" onclick="get_id(this.id)">
Task1
</label>
<label id="task2" value="Change" onclick="get_id(this.id)">
Task2
</label>
javascript to get id:
function get_id(id){
alert('My Id is ' + id);
}
I am developing an ordering page in PHP for some products. The user needs to be able to select multiple types of products, so checkboxes will be necessary in the HTML form.
I've established an array for the checkboxes via the "name" attribute.
My problem is, I want to display what the user has selected on a confirmation page, so I'd like the "value" of those checkboxes to ultimately return a product name AND a product price. As far as I can tell I'm going to be stuck with one value only, so I've attempted to get around this:
<?php
//variables for the array and the count
$parray = $_POST['product'];
$pcount = count($parray);
//arrays for each product's information; i've only listed one for simplicity
$br = array("Big Red", 575);
/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
if (!empty($parray))
{
for ($i=0; $i < $pcount; $i++)
{
$key = $parray[i];
echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
}
}
?>
Is there anyway to make my $key variable actually act as if it is the array's name it is set to?
If not, is there any good way to do this?
So in your HTML table, you'll need to render each checkbox like this:
<input type="checkbox" name="selectedIDs[]" value="$key" />
where $key is the item number.
Then, in your PHP, you'll have a $_POST["selectedIDs"] variable, which is an array of all the item numbers that were checked.
Assuming you've got a product list array like this:
$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));
You can then print a list of the selected products using a loop like this:
for($i = 0; $i < count($_POST["selectedIDs"]); $i++) {
$key = $_POST["selectedIDs"][$i];
$product = $products[$key];
echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>";
}
The only real difference between this and what you wrote is that my $products array is two-dimensional, and my $key is used to grab the relevant product from the $products array.
You can give the value of the options the value of the ID of the corresponding item. Then you'll receive an array of ID's, which you can then again map to the $br array, where you look up the name and price of an item by its ID. You could use the array key as ID, so:
<!-- Form: -->
<input type="checkbox" name="product[]" value="1" />
<input type="checkbox" name="product[]" value="..." />
<input type="checkbox" name="product[]" value="15" />
[...]
<?php
$product = array();
$product[1] = array('name' => "Big Red", 'price' => 575);
$product[...] = array('name' => "...", 'price' => ...);
$product[15] = array('name' => "Small Green", 'price' => 475);
foreach ($_POST['product'] as $pId)
{
echo $products[$pId]['name'] . " costs " . $products[$pId]['price'];
}
?>
If of course your array originates from a database, you can use the table's ID's as values for the checkboxes. You could then implode the $_POST['product'] (of course after escaping it) with a comma, and use it in a SELECT ... WHERE ID IN ($productIds) query.
If i'm understanding you correctly, you'll have checkboxes something like this:
<input type="checkbox" name="product_id[]" value="1">Product #1<br/>
<input type="checkbox" name="product_id[]" value="2">Product #2<br/>
<input type="checkbox" name="product_id[]" value="3">Product #3<br/>
That should post whatever is checked as an array to your server.
gen_paper1.php
<div style="overflow: auto; height: 500px; border: 1px solid;">
<form action="" method="post">
<fieldset style="font-size: 15px; font-weight: bolder;">
<h4><u>Your Existing Header's are listed below</u> </h4>
<hr / >
<?php
$xhdr = mysql_query("SELECT * from s_user_header ORDER BY 'ASC'");
while($rheader = mysql_fetch_array($xhdr)){
$h = $rheader['h_content'];
$hid = $rheader['h_id'];?>
<script>
$(document).ready(function(){
//$('input[type=checkbox]').val('<?php echo $hid ; ?>').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
//$('#c').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
$('').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
});
</script>
<table>
<td><input type="checkbox" id="c" name="u_hdr[]" value="<?php echo $hid; ?>"></td>
<td style="font-weight: bolder"><?php echo $h."<br />"; ?></td>
</table>
<?php } ?>
</fieldset>
</div>
gen_paper2.....3.....
i need all this values in further page, so have taken hidden variables
<input type="hidden" name="hstdid" value="<?php echo $stdid; ?>">
<input type="hidden" name="hsubid" value="<?php echo $subid; ?>">
<input type="hidden" name="hdifid" value="<?php echo $difid; ?>">
<input type="hidden" name="hmarks" value="<?php echo $t_marks; ?>">
<input type="hidden" name="h_hid[]" value="<?php echo $hh; ?>">
gen_paper4.php
$getstdid = $_POST['hstdid3'] ;
$getsubid = $_POST['hsubid3'] ;
$getdifid = $_POST['hdifid3'] ;
$gethmarks = $_POST['htmarks'] ;
$hdr= $_POST['h_hdrid'] ;
$h = implode($hdr);
<table>
<?php
$h1 = explode(",",$h);
count($h1) . "<br />";
for($i=0;$i<count($h1);$i++){
$h1[$i];
$xheader = mysql_query("SELECT * from s_user_header WHERE h_id = ".$h1[$i]);
while($row = mysql_fetch_array($xheader)){ ?>
<tr>
<td><b>
<?php echo $i + 1 . ".   "; ?>
</b></td><td>
<?php echo $header = $row['h_content'] . "<br />";
?></td>
</tr>
<?php
}
}
//echo $header; ?>
</table>