PHP - post foreach content - php

So, i have this foreach loop that runs a sql query. Every row is printed in a option. I have two forum posts. One that 'deletes' the row and one that 'uses' the row. But when I post the form the content inside the option remains empty. Here is my code:
Post file
<?php
try {
$DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);
$sql = "SELECT * FROM message";
?>
<html>
<form action="action.php" method="post">
<select><?php
foreach ($DB->query($sql) as $row)
{
?>
<option name="content" value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
<?php } ?>
</select>
<br /><input type="submit" value="use" name="use">
<input type="submit" value="delete" name="delete">
</form>
</html>
Action.php
<?php
require_once 'hidden/session.php';
$delete = $_POST['delete'];
$use = $_POST['use'];
$content = $_POST['content'];
try {
$DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);
$delete = "DELETE FROM message WHERE title='$content'";
if (isset($delete)){
$DB->exec($delete);
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}

In order to post content you have to give name to your control. You have specified name property for option but there is no name property specified in select. Which is why the value of that control is not getting posted. Hence, when you try to access it as $content = $_POST['content']; , it gives you empty value.
Try this:
<html>
<form action="action.php" method="post">
<select id="content" name="content">
<?php foreach ($DB->query($sql) as $row) { ?>
<option value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
<?php } ?>
</select>
<br />
<input type="submit" value="use" name="use">
<input type="submit" value="delete" name="delete">
</form>
</html>
Hope it helps!!

Related

How to add caption field for every image preview and post all caption through one submit button

This is my code I want to add one input field for every image preview and save it to db.. the field is coming but I'm not getting any data.. can anyone suggest how can I post them???
$fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'");
$fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC);
$fetch_imgid->execute();
?>
<ul class="reorder_ul reorder-photos-list" id="previewImg">
<?php
while($row = $fetch_imgid->fetch()):
$delid = $row['id'];
//echo $row['id'].' '.$row['name'].'<br/>';?>
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle" data-image-id="<?php echo $delid; ?>">
<img src="uploads/<?php echo $row['name']; ?>" alt="">
<input type="submit" class="del_btn" value="Delete Image" />
<input type="text" id="cap" name="cap[]" placeholder="Enter Caption Here" />
<input type="hidden" id="cap_id" value="<?php echo $row['id']; ?>" />
<?php
endWhile;
?>
</ul>
<input type="submit" value="Add Caption" name="addcap" /> <?php include('addcap.php'); ?>
and this is addcap.php
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if(isset($_POST['addcap'])){
foreach($_POST['cap'])
{
$imgcap = $_POST['cap'];
if($imgcap!=empty())
{
try
{
$con=new PDO("mysql:host=localhost;dbname=newimg","root","");
$sql=$con->prepare("UPDATE attempt010 SET caption='$imgcap' WHERE id='$cap_id'");
$sql->execute();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
}
?>
<input type="hidden" id="cap_id" value="<?php echo $row['id']; ?>" />
Must have unique id. You can't send multiple fields with same id. You will get only last one.
For example:
$fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'");
$fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC);
$fetch_imgid->execute();
?>
<form action="addcap.php" method="post">
<ul class="reorder_ul reorder-photos-list" id="previewImg">
<?php
$id_array="";
while($row = $fetch_imgid->fetch()):
$id_array = $id_array.$row['id'].",";
$delid = $row['id'];
//echo $row['id'].' '.$row['name'].'<br/>';?>
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle" data-image-id="<?php echo $delid; ?>">
<img src="uploads/<?php echo $row['name']; ?>" alt="">
<input type="text" id="cap_<?php echo $row['id']; ?>" placeholder="Enter Caption Here" />
<?php
endWhile;
$id_array = substr($id_array, 0, -1);
?>
<input type="hidden" id="cap_ids" value="<?php echo $id_array ; ?>" />
</ul>
<input type="submit" value="Add Caption" name="addcap" />
</form>
<!--addcap.php-->
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if(isset($_POST['addcap'])){
if(isset($_POST['cap_ids'])){
$ids_array = explode(",", $_POST['cap_ids']);
foreach($ids_array as $ids)
{
$idcap = 'cap_'.$ids;
$imgcap = $_POST[$idcap];
if($imgcap!=empty())
{
try
{
$con=new
PDO("mysql:host=localhost;dbname=newimg","root","");
$query = "UPDATE attempt010 SET
caption='$imgcap' WHERE id='$ids'";
echo $query;
$sql=$con->prepare($query);
$sql->execute();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
}
}
?>
This code looks like it can't work. Because you have submit and form handling code in same page. The idea behind form is to post data to different page(set in form action) and this page will do something with this data and display results to the user. For your example to work make form in your first file like:
<form action="addcap.php">
<inputs here>
</form>
Nowadays it is common that database operations are done asynchronic on server side, when user can continue using the page/app.
So learn how to use jQuery and AJAX. Maybe nodeJS or other new stuff.

PHP Why does my select option show empty

When I submit the form I don't get the value of the select option I tried using POST and session but it always show nothing
main.php
<form role="form" method="POST" action="test.php">
<?php if($id == 1 OR $id==2){
echo" <p> No data</p> ";}else{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata )
or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data )) {
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
$_SESSION['data_id']= $data_id;
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
$dataID = isset($_POST['data_id']) ? $_POST['data_id'] : '';
echo "data is $dataID";
Name of your input type select is data and you are accessing it with data_id so you have to use $_POST['data'] instead of $_POST['data_id']
get value of select box using name "data" as you have set name="data" in <select> box in html:
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is".$dataID;
main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
}
?>
</select>
<?php
}
?>
<button type="submit" class="btn btn-primary">show</button>
test.php
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is $dataID";
try this one..
Check below points its may be creating issue.
Check first your <select class="form-control" name="data"> name is data so you can access it using $_POST['data'] not data_id.
Check for <option value="'.$row['data_id'].'"> may be data_id not giving correct value try to check with static value.
I modified this code for you,please use this code.Its work for me,i hope this code will work also for you.
main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
?>
<option value="<?php echo $row['data_id']; ?>"><?php echo $dataName ?></option>
$_SESSION['data_id']= $row['data_id'];
<?php
}
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
<?php
if(isset($_POST['data']))
{
echo $_POST['data'];
}
?>

How do I identify the selected value from a dropdown list in the $_POST array?

I have a droplist <select> list in html. How do I identify the selected value in the $_POST array after the user submits the form ?
<form action="subj_exec.php">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<br>
<select name = "subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value=';
echo $subject_id;
echo '> ';
echo $name;
echo '</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
</form>
The subject_id is blank in another php file
echo $_POST['subject_id'] is blank.
Please help to identify the issue in the code.
Thanks,
The standard method for forms is GET, so you need to add method="POST" to your form.
get: Default. Appends the form-data to the URL in name/value pairs: URL?name=value&name=value
post: Sends the form-data as an HTTP post transaction
<form action="subj_exec.php" method="POST"> //<<<< added method
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<br>
<select name = "subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
?>
<option value="<?= $subject_id; ?>"><?= $name; ?></option>
<?php
}
?>
</select>
<input type="submit" value="submit" name="submit" />
In your file subj_exec.php, you can output the selected value with
echo $_POST['subj_id'];
Here you go :
index.php
<form action="subj_exec.php" method="POST">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<select name="subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value="'.$subject_id.'">'.$name.'</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
subj_exec.php
<?php
error_reporting(E_ALL ^ E_NOTICE);
if(isset($_POST['submit'])) {
if(strlen($_POST['subj_id']) >= 1) {
$option = htmlentities($_POST['subj_id'], ENT_QUOTES, "UTF-8");
// Do Something here with $option
echo $option;
}else {
echo 'nothing selected.';
}
}
?>

php form and session assignment in the same file

I am trying to use post in a form to save form data from a dropdown as a session in the same file as the form
<?php
session_start();
if(isset($_SESSION['post-data']['surnameid']))
unset($_SESSION['post-data']['surnameid']);
?>
Then in the body of html
<form action="" method="post">
<?php
include 'connect.inc';
$sql = "SELECT surnameid FROM customer";
$result = mysql_query($sql);
echo "<select name='surnameid'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['surnameid'] . "'>" . $row['surnameid'] . </option>";
}
include 'close.inc';
?>
<br/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
$_SESSION['post-data'] = $_POST;
echo $_SESSION['post-data']['surnameid'];
?>
The assiginment to $_SESSION does not work
Try this :
You forgot a double quote when you are putting echoing
make sure that you have the rows in the database ( we can't test that ) but i tested with predefined data and this version works
form.php
<?php
session_start();
?>
<form action="" method="post">
<?php
echo "<select name='surnameid'>";
echo "<option value='1'>test</option>";
echo "<option value='2'>test2</option>";
?>
<br/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if( !empty($_POST)){
$_SESSION['post-data'] = $_POST;
}
if(isset($_SESSION['post-data']['surnameid']))
echo $_SESSION['post-data']['surnameid'];
?>
test_session.php
<?php
session_start();
print_r($_SESSION['post-data']['surnameid']);
?>

Updating Post not working

I recently made a code that updates my posts on my blog. It worked perfectly on localhost. But when i uploaded it online it did not work any more. The weird thing is it doesn't even display a error so i have no idea where to look. Can someone please help me ?
require('config.php');
$query = "SELECT * FROM project ORDER BY idproject DESC";
$result = mysqli_query($verbinding, $query ) or die (mysqli_error('kan geen verbinding maken met de database'));
if(isset($_POST['editBut'])){
$editTitle = $_POST['editName'];
$editThis = mysqli_query($verbinding, "SELECT * FROM project WHERE title = '".$editTitle."'");
$values = mysqli_fetch_assoc($editThis);
}
if(isset($_POST['update'])){
$editedTitle = $_POST['newTitle'];
$editedText = $_POST['newTekst'];
$oldTitle = $_POST['oldTitle'];
$date = $_POST['datum'];
$updater = mysqli_query($verbinding, "UPDATE Project SET title='".$editedTitle."', content='".$editedText."' WHERE title='".$oldTitle."' AND datum='".$date."'");
echo $updater;
header('location:editPost.php?id=1');
}
if(isset($_GET['id'])){
echo 'post has been succesfully updated';
}
<?php if(isset($_POST['editBut'])){ ?>
<form action="" method="post">
Title: <input type="text" name="newTitle" value="<?php echo $values['title'] ?>"><br>
Text: <textarea type="text" name="newTekst" id="newTekst"><?php echo $values['content'] ?></textarea><br>
<input type="hidden" value="<?php echo $values['title'] ?>" name="oldTitle">
<input type="hidden" value="<?php echo $values['datum'] ?>" name="datum">
<input type="submit" name="update" value="Edit post">
</form>
<?php } else { ?>
<p>Find the post you want to edit:</p>
<form action="" method="post">
<select name="editName">
<?php
while ($row = mysqli_fetch_assoc($result)) {
?> <option value="<?php echo $row['title'] ?>"><?php echo $row['title'] ?></option>
<?php } ?>
</select>
<input type="submit" name="editBut" value="Choose">
</form>
<?php } ?>
In update query replace your table name with small letter.
replace Project with project

Categories