I use this snippet to get vehicle data from a external database:
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>" class="vwe-kenteken-widget">
<p><input type="text" name="open_data_rdw_kenteken" value="<?php echo $_POST['open_data_rdw_kenteken'] ?>" maxlength="8"></p>
<p><input name="submit" type="submit" id="submit" value="<?php _e('Kenteken opzoeken', 'open_data_rdw') ?>"></p>
</form>
<?php if($data): ?>
<h3><?php _e('Voertuiggegevens', 'open_data_rdw') ?></h3>
<table>
<?php
$categories = array();
foreach ($data as $d) {
if( !is_array($fields) || in_array($d['name'], $fields) ) {
if( !in_array($d['category'], $categories) ) {
$categories[] = $d['category'];
echo '<tr class="open-rdw-header">';
echo '<td colspan="2" style="font-weight: bold;">';
echo ''.$d['category'].'';
echo '</td>';
echo '</tr>';
}
echo '<tr style="display:none">';
echo '<td>'.$d['label'].'</td>';
echo '<td>'.$d['value'].'</td>';
echo '</tr>';
}
}
?>
</table>
<?php endif; ?>
What i want to accomplish is that the data is loaded without the user have to enter a value and hit the submit button. The input value will get loaded based on the product page the user is viewing.
EDIT:
The data is loaded based on:
public function get_json() {
if ( isset( $_POST['kenteken'] ) ) {
$data = $this->rdw->get_formatted($_POST['kenteken']);
foreach ($data as $row) {
$json['result'][$row['name']] = $row['value'];
}
if ($_POST['kenteken']) {
if ($data[0]['value'] == '') {
$json['errors'] = __( 'No license plates found', 'open_data_rdw' );
}
else {
$json['errors'] = false;
}
}
else {
$json['errors'] = __( 'No license plate entered', 'open_data_rdw' );
}
header('Content-type: application/json');
echo json_encode($json);
die();
}
}
So instead of a $_POST action just get the data based on a pre-declared value that is different on each page.
Hard to answer - but I'll try to use my crystal ball.
$data comes from a database query, right?
I assume further, that the query takes the value from the open_data_rdw_kenteken form field to gather $data.
To have the table rendered, you have to fill $data with the right data. That implies that you must have a kind of default value for open_data_rdw_kenteken to get the data out of the DB. The default can be "all" which should reflect on your SQL Query or as your wrote "defined by product page".
Pseudo Code
$data = getData('BT-VP-41');
function getData($open_data_rdw_kenteken="")
{
$where = "";
if(!empty($open_data_rdw_kenteken)) {
$where = 'WHERE rdw_kenteken = "'.mysqli_real_escape_string($open_data_rdw_kenteken)';
}
$data = myslqi->query("SELECT * FROM dbTbl ".$where)
return $data;
}
As I wrote - this is pseudo code and will not run out of the box. You'll have to adapt that to your environment.
TL;DR: The line
<?php if($data): ?>
keeps you from rendering the table. To render you need $data filled with the right data.
Hope that will get you in the right direction.
Related
$product = '';
$stmt = $verbinding->query("SELECT
product_category, product_id, parent FROM
Productcategory ORDER BY product_category");
$categorien = $stmt->fetchAll();
foreach($categorien as $cats){
if($cats['parent'] === $producten){
echo '<form action="product-list.php" method="post">
<input type="submit" name="cats" value="'. $cats['product_category'] .'"></form>';
echo $producten;
//var_dump($_POST);
if(isset($_POST['cats'])){
echo $_POST['cats'];
echo $cats['product_category'];
echo $cats['product_id'];
if($_POST['cats'] === $cats['product_category']){
$product = $cats['product_id'];
echo $product;
echo "gelukt!!";
}
}
}
}
So to update my question this is code that does partly work but does not get past the if($_POST['cats'] === $cats['product_id']){ statement. The problem is that my $_POST['cats'] just sends NO information. I can't understand why though. I should say though that I use the same "name" value in another form, but if I don't it wont even go through the if(isset($_POST['cats'])){ statement.
From your form the action page is "product-list.php", is it the same page you are handling the form logic in?
If not then your you need to write the code below in the product-list.php you created.
....
if(isset($_POST['categorien'])){
echo "pastcategory?";
if($_POST['categorien'] === $cat['product_category']){
$producten = $cat['product_id'];
echo "pastcategory!!!!";
echo $producten;
}
}
....
I have a form that submits images and their titles. It is set up to check for errors via PHP validations on the image title form input elements and output any errors inside each image component (i.e. the instance in the while loop that represents a certain image). This all works OK.
What I would like to do is have it so that when one or more title is filled out correctly, but there are errors on one or more other titles, the $_POST value of the incorrect input is echoed out in the input element so the user doesn't have to re-type it. This has been easy to do on other forms on the site because there is no loop involved, e.g. on a sign up form etc. On a singular instance I would just do <?php echo $image_title; ?> inside the HTML value attribute, which is set referencing $image_title = $_POST['image-title'];]
So my question is, how do I have it so the $image_title $_POST value instances that pass the validations are outputted in their respective <input> elements, when other instances of the $image_title variable fail. If all the checks pass and there are no errors the form is then processed with PDO statements. The form submission button is placed outside of the main loop so all images are processed in one go when all the information is correct. I have a hidden input element that outputs the database image ID for each image, which can be used as a key in a foreach loop of some type. The problem of course being I can't get a foreach loop to work as I would like.
NOTE: To make the code simpler I've removed all the code relating to the outputting the images themselves.
<?php
if(isset($_POST['upload-submit'])) {
$image_title = $_POST['image-title'];
$image_id = $_POST['image-id']; // value attribute from hidden form element
// checks for errors that are later outputted on each image component
forEach($image_title as $index => $title) {
$id=$_POST['image-id'][ $index ];
if(empty(trim($title))){
$error[$id] = "Title cannot be empty";
}
}
if (!isset($error)) {
try {
// update MySQL database with PDO statements
header("Location: upload-details.php?username={$username}");
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
} else {
// prevents error being thrown before form submission if input elements are empty
$image_title = "";
}
?>
<form method="post" enctype="multipart/form-data">
<!-- IMAGE DETAILS COMPONENT - START -->
<?php
$user_id = $_SESSION['logged_in'];
$stmt = $connection->prepare("SELECT * FROM lj_imageposts WHERE user_id = :user_id");
$stmt->execute([
':user_id' => $user_id
]);
while ($row = $stmt->fetch()) {
$db_image_id = htmlspecialchars($row['image_id']);
$db_image_title = htmlspecialchars($row['image_title']);
?>
<div class="upload-details-component">
<?php
// output error messages from the validation above
if(isset($error)) {
foreach($error as $element_id => $msg) {
if($element_id == $id) {
echo "<p>** ERROR: {$msg}</p>";
}
}
}
?>
<div class="edit-zone">
<div class="form-row">
<!-- ** THIS IS THE INPUT ELEMENT WHERE I WOULD LIKE THE TITLE OUTPUTTED IF IT DOESN'T FAIL THE VALIDATION ** -->
<input value="<?php echo $image_title; ?>" type="text" name="image-title[]">
</div>
<div class="form-row">
<input type="hidden" name="image-id[]" value="<?php echo $db_image_id; ?>">
</div>
</div>
</div>
<?php } ?> <!-- // end of while loop -->
<!-- submit form button is outside of the loop so that it submits all of the images inside the loop in on go -->
<button type="submit" name="upload-submit">COMPLETE UPLOAD</button>
</form>
i think this is what you want:
<input value="<?php echo htmlentities($image_title); ?>" type="text" name="image-title[<?php echo $db_image_id; ?>]">
foreach ($_POST['image-title'] as $key => $value) {
$stmt->execute([
':image_id' => $key,
':image_title' => $value
]);
}
Edit: I have now created a minimized version and was able to test it successfully. I created the mysql table on my test-maschine to try it out. I hope this will help you now.
With <input name="img[id]" value="title"> an array will created with the following structure: $_POST['img'][id] = title. The id as array key and the title as array value. So the title is uniquely assigned to each id. The array can be walk through with a foreach key=>value loop.
Edit2: I added error checking. If the title is empty or is "testfail" for example, the title will not be written to the database. In addition, the input field keeps the last input (restore $_POST string).
<?php
$connection = new PDO(...);
$error = []; // declare empty array
if(isset($_POST['img'])) {
try {
$q = "UPDATE lj_imageposts SET image_title=:image_title
WHERE image_id = :image_id AND user_id = :user_id";
$stmt = $connection->prepare($q);
foreach($_POST['img'] as $key => $value) {
// here the error check, so that the wrong title
// is not written into the database
if(trim($value) === '' || $value === 'testfail') {
echo "Error image id $key title $value <br>";
$error[$key] = TRUE; // set error var with img-id
continue; // skip to next img, do not execute sql-stmt
}
$stmt->execute([
':image_id' => $key,
':image_title' => $value,
':user_id' => $_SESSION['logged_in']
]);
echo "Update image id $key title $value <br>";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
?>
<form method="post" enctype="multipart/form-data">
<?php
$q = "SELECT * FROM lj_imageposts WHERE user_id = :user_id";
$stmt = $connection->prepare($q);
$stmt->execute([':user_id' => $_SESSION['logged_in']]);
while ($row = $stmt->fetch()) {
$id = htmlentities($row['image_id']);
// check if error with img-id is set above
if(isset($error[$row['image_id']])) {
// if error, fill in title from previous post
$title = htmlentities($_POST['img'][$row['image_id']]);
} else {
// if no error, fill in title from database
$title = htmlentities($row['image_title']);
}
?>
<input type="text"
name="img[<?php echo $id; ?>]"
value="<?php echo $title; ?>"><br>
<?php
// alternative version
//echo '<input type="text" name="img['.$id.']" value="'.$title.'"><br>';
}
?>
<button type="submit" name="upload-submit">Test</button>
</form>
This is harder than it needs to be, because you can't uniquely identify a particular title input except by the order it appears on the form, with the associated hidden input beside it. You need to do some careful juggling to correlate the IDs from the database with the $index of the $_POST arrays, to make sure your tiles and IDs match. You do have that all working, but this seems fragile and not a good solution IMO. I'd suggest using the database IDs as your form input index values, rather than relying on the order they appear on the form, as I did in my answer to a previous question of yours.
You're already tracking errors with the associated DB ID. So when it comes to displaying the input values, you can just check if there is an error for that DB ID. If there is not, this input passed validation, and you can re-display the value from the current $_POST:
<form method="post" enctype="multipart/form-data">
<!-- ... your code ... -->
while ($row = $stmt->fetch()) {
$db_image_id = htmlspecialchars($row['image_id']);
$db_image_title = htmlspecialchars($row['image_title']);
// Start with the value in the DB, this will display first time
// through.
$value = $db_image_title;
// If there's been a POST, and there is no $error for this ID,
// we know it passed validation.
if (sizeof($_POST) && ! isset($error[$db_image_id])) {
$value = $_POST['image-title'][$db_image_id];
}
?>
<!-- ... your code ... -->
<div class="edit-zone">
<div class="form-row">
<!-- Now we can use whatever value we set above -->
<input value="<?php echo $value; ?>" type="text" name="image-title[]">
</div>
Side Note
There's no need to iterate over all your errors to find the one you want. If you have its ID, you can address it directly. Instead of this:
if(isset($error)) {
foreach($error as $element_id => $msg) {
// Note your code above does not show what $id is here,
// AFAICT it is the same as $row['image_id']
if($element_id == $id) {
echo "<p>** ERROR: {$msg}</p>";
}
}
}
You can simply do:
if (isset($error) && isset($error[$row['image_id']])) {
echo "<p>** ERROR: " . $error[$row['image_id']] . "</p>";
}
I have a list of favorite cars which i have added to each favorite car a checkbox for letting the user to remove the favorite car from his favorite car list. The problem is that the checkbox is working in a different way: If I check any car (1st, second.. last or multiple cars) and after hit submit the car that will get removed is the last one added instead of removing the selected one. If I check multiple cars, happens same thing, removes only the last car added.
PHP
public function GetFavoriteCars() {
include("inc/membersite_config.php");
$email = $fgmembersite->UserEmail(); // this is how I take the e-mail of the
global $base_path;
$FavoriteCars = $this->QueryResult("SELECT * FROM favoritecar WHERE email='$email'");
if (count($FavoriteCars)) {
$mystring='http://';
echo '<form action="" class="deletebutton" method="post">';
echo '<input type="submit" name="deletebtn" id="deletebtn" value="Submit">';
echo '<div class="roster_slideri-login">';
foreach ($FavoriteCars as $FavoriteCar) {
$carlink = $FavoriteCar->favoritecarlink;
echo '<div class="car-info-col-login">';
echo '<input type="checkbox" name="checkbox" value="'.$carlink.'" class="checkbox-login">';
$val=strpos($FavoriteCar->favoritecarimg,$mystring);
if ($val !== false) {
if($FavoriteCar->favoritecarimg!='') {
echo '<a href="'.$base_path.'detail-page_'.$FavoriteCar->favoritecarlink.'">';
echo '<img src="'.$FavoriteCar->favoritecarimg.'" alt="'.$FavoriteCar->favoritecartitle.'" width="160" height="120" />';
echo '</a>';
echo '<div class="name">'.substr($FavoriteCar->favoritecartitle,0,20).'</div>';
echo '</div>'; //car-info-col-login
}
} else {
echo '<a href="'.$base_path.'detail-page_'.$FavoriteCar->favoritecarlink.'">';
echo '<img src="'.$base_path.'uploads/no-img.jpg" alt="'.$FavoriteCar->favoritecartitle.'" width="160" height="120" />';
echo '</a>';
echo '<div class="name">'.substr($FavoriteCar->favoritecartitle,0,20).'</div>';
echo '</div>';
}
}
echo '</form>';
if (isset($_POST["checkbox"])) {
$this->QueryResult("DELETE from favoritecar WHERE email='$email' AND favoritecarlink='$carlink'");
echo '<script type="text/javascript">alert("Car had been deleted");</script>';
}
echo '</div>'; // div roster_slideri-login
}
}
Explaning:
$email = $fgmembersite->UserEmail(); - this is how I take the e-mail of the current logged in user. It will echo "email_of_logged_in_user#domain.com"
QueryResult is a custom function that looks like this. I usually use it for SELECTING purposes but it seams that is working for deleting purposes too.
abstract class DBDetails {
protected $link = NULL;
protected function connector() {
global $DBHOSTNAME;
global $DBUSERNAME;
global $DBPASSWORD;
global $DBNAME;
$this->link = mysqli_connect($DBHOSTNAME, $DBUSERNAME, $DBPASSWORD, $DBNAME) or die("Can't connect to MySQL server on localhost");
}
protected function close() {
mysqli_close($this->link);
}
}
abstract class N2 extends DBDetails {
public function QueryResult($strQuery) {
$this->connector();
$query = mysqli_query($this->link, $strQuery);
$arr = array();
if ($query) {
while ($result = mysqli_fetch_object($query)) {
array_push($arr, $result);
}
}
$this->close();
return $arr;
}
}
Expected output
When I check the checkbox of a car, it should delete only that car. If I check the checkboxes of multiple cars, should delete the specific cars that I checked.
Please help, I am quite a noob in checkboxes. I have checked lots of questions from here, but did not find my answer.
In this line :
echo '<input type="checkbox" name="checkbox" value="'.$carlink.'" class="checkbox-login">';
--------------
When using multiple checkboxes with same name , you would need to include [] in the name :
echo '<input type="checkbox" name="checkbox[]" value="'.$carlink.'" class="checkbox-login">';
----------------
Then $_POST["checkbox"] will be an array and you can use foreach on it to get all the checked values .
if( isset( $_POST["checkbox"] ) )
{
foreach( $_POST["checkbox"] as $value )
{
/* $value contains $carlink */
echo $value; // For test purpose
/* Sanitize and use it to identify and delete the corresponding row */
}
}
( Rather than name="checkbox[]" it might be better to choose another name . )
I have two tables ::: tbl_product and tbl_featured_product. I did view all product (from tbl_product) information in view pages by checkbox system. So that I can submit data to tbl_featured_product by checked as I need.
I can view all information to a view page in checkbox.. BUT can't save them in database. saving only last one row. please help me out to save multiple data in same time:::
view:::
<?php foreach($all_product as $values) { ?>
<input type="checkbox" name="product_name[]" value="<?php echo $values->product_name;?>" /> <?php echo $values->product_name;?> <br>
<input hidden="hidden" name="product_id[]" value="<?php echo $values->product_id;?>" />
<input hidden="hidden" name="product_price[]" value="<?php echo $values->product_price;?>" />
<?php } ?>
<input type="submit" name="btn" value="Save">
My Controller:::::
public function save_featured_product()
{
$data=array();
if ($this->input->post()) {
$data['featured_id']=$this->input->post('featured_id',true);
$data['product_id']=$this->input->post('product_id',true);
$data['product_name']=$this->input->post('product_name',true);
$data['product_price']=$this->input->post('product_price',true);
$this->sa_model->save_featured_product_info($data);
$sdata=array();
$sdata['message']='Save product Information Successfully !';
$this->session->set_userdata($sdata);
redirect('super_admin/add_featured_product');
}
My Model ::::
public function save_featured_product_info($data)
{
$this->db->insert('tbl_featured_products',$data);
}
Please let me know the solutions from your side. Thank you
Your problem is that the input $this->input->post('product_name') are arrays, so you need to insert one row for each, like:
(in the model)
public function save_featured_product_info($data)
{
if (isset($data['product_name']) && is_array($data['product_name'])):
foreach ( $data['product_name'] as $key=>$value ):
$this->db->insert('tbl_featured_products', array(
'product_id'=>$data['product_id'][$key],
'product_name'=>$data['product_name'][$key],
'product_price'=>$data['product_price'][$key],
'featured_id'=>$data['featured_id'] // assuming this are the same for all rows?
));
endforeach;
endif;
}
Do something like this, you may need to do some changes accordingly:
function save_featured_product_info($data){
if( isset( $data['product_id'] ) && is_array( $data['product_id'] ) ){
foreach( $data['product_id'] as $key => $each ){
$temp[] = array(
'featured_id' =>$data['featured_id'][$key],
'product_id' =>$data['product_id'][$key],
'product_name' =>$data['product_name'][$key],
'product_price'=>$data['product_price'][$key],
);
}
if( isset( $temp ) ){
$this->db->insert_batch('tbl_featured_products', $temp);
}
}
}
You're trying to save arrays, you either need to implode the values or loop and save them individually or batch insert them
$this->db->insert_batch('your_table', $temp);
I try to create an order page on php. I have created a form which stores data into the database. The form number is based on the user and it is created dynamic.
<?php
if(isset($_POST['submit_num']))
{
$number=$_POST['sky'];
for($i=0;$i<$number;$i++)
{
$item = $_SESSION['item'];
echo $item;
$rec_query = "SELECT * FROM ylika";
$rec_result= mysql_query($rec_query) or die("my eroors");
echo '<form action="user_order_form.php" method="POST">';
echo '<html>';
while($row_rec = mysql_fetch_array($rec_result))
{
echo '<input type="checkbox" name="yliko[]" value='.$row_rec['onoma'].'> '.$row_rec['onoma'].'';
echo '<br>';
}
echo '<br>';
echo '<input type="submit" name="submit" value="ORDER">';
echo '</form>';
}
}
?>
So I have many forms and 1 script to handle them. If I submit 1 form then it applies the data to the database but the other forms dissapear. This is the second php 'handler'.
<?php
if (isset($_POST['submit']))
{
$max_id = "SELECT MAX(id_order) FROM id_of_orders";
$x=mysql_query($max_id) or die("my eroors");
$id= mysql_fetch_array($x);
$xyz = $id['MAX(id_order)'];
$item = $_SESSION['item'];
$temp = $_POST['yliko'];
$temp2 = implode(",", $temp);
$inserts = ("INSERT INTO orders (order_id,product,ulika) VALUES ('$xyz' , '$item','$temp2')");
$inc_prod=("UPDATE proion SET Counter = Counter + 1 WHERE proion.onomasia='$item'");
mysql_query($inserts) or die(mysql_error());
mysql_query($inc_prod) or die(mysql_error());
}
?>
I want to submit all the forms. Should I try to create one submit button for all of the forms or is there a way to handle all of them separately ?
You can have many submit forms, but simply define one big <form> with all of them, then you will not lose any information.
<?php
if(isset($_POST['submit_num']))
{
$number=$_POST['sky'];
echo '<form action="user_order_form.php" method="POST">';
for($i=0;$i<$number;$i++)
{
$item = $_SESSION['item'];
echo $item;
$rec_query = "SELECT * FROM ylika";
$rec_result= mysql_query($rec_query) or die("my eroors");
echo '<html>';
while($row_rec = mysql_fetch_array($rec_result))
{
echo '<input type="checkbox" name="yliko[]" value='.$row_rec['onoma'].'> '.$row_rec['onoma'].'';
echo '<br>';
}
echo '<br>';
echo '<input type="submit" name="submit" value="ORDER">';
}
echo '</form>';
}
?>
now you can also add the ~$i~ identifier to input names, so you can easily distinguish which is which.