Multiple submissions from one page - php

am trying to allow a user to delete a certain image with is in a certain directory. I need them to first specify a directory and then the image which they want to delete.
I have been able to accomplish what I thought would be the hard part, of having their Gallery selection result in a drop-down populated only with the name of those images in their selected gallery.
My issue is that I cannot seem to get the submit button for my second form to trigger the if statement that will delete the files and remove the row from the table. I thought someone here might peruse my code and tell me what I am missing :(
thanks a ton!
<form action ='' name="form1" method='POST' enctype="multipart/form-data">
<p>Select Gallery: <select name="gal" style="color:black;">
<option value="" style="color:black;"><?php echo $gal; ?></option>
<option value="graphicdesign/logos" style="color:black;">GD logos</option>
<option value="graphicdesign/webdesign" style="color:black;">GD website design</option>
<option value="graphicdesign/advertisements" style="color:black;">GD Advertisements</option>
<option value="fineart/nature" style="color:black;">FA Nature</option>
<option value="fineart/people" style="color:black;">FA People</option>
<option value="fineart/landscapes" style="color:black;">FA Landscapes</option>
<option value="photography/misc" style="color:black;">PG Misc</option>
<option value="photography/nature" style="color:black;">PG Nature</option>
<option value="photography/people" style="color:black;">PG People</option>
</select></p>
<input type="submit" value="Set Galery" style="color:black;"></p>
<?php
$connect = mysql_select_db("cynthie") or die("couln't find db on 2 :(");
$metaData = mysql_query("SELECT `name`, `path_full`, `path_thumb`, `galId` FROM `images`") or die("couln't find table :(");
$gal = $_POST['gal'];
if (isset($_POST['gal']))
{
echo '<form action ="" method="POST" enctype="multipart/form-data"><p>Select Image: <select name="toDelete" style="color:black;"><p><option style="color:black;">select img</option></p>';
while ($displayData = mysql_fetch_assoc($metaData))
{
$names = $displayData['name'];
$path_full = $displayData['path_full'];
$path_thumb = $displayData['path_thumb'];
$galDb = $displayData['galId'];
if ($galDb != $gal)
{}
else
{
echo '<p><option style="color:black;">'.$names.'</option></p>';
}
}
echo '</select><p><input type="submit" value="Delete" style="color:black;"></p></form>';
$connect = mysql_select_db("cynthie") or die("couln't find db on 4 :(");
$delete = mysql_query("SELECT `id` FROM images WHERE name='$toDelete'") or die("couln't find table on line 81 :(");
$toDelete = $_POST['toDelete'];
if (isset($_POST['toDelete']))
{
unlink($path_full);
unlink($path_thumb);
mysql_query("DELETE FROM images WHERE `id`=$delete");
unset($delete);
echo '<p>removed!</p>';
}
else
{}
}
?>

You have your toDelete check inside the gal check, so you need to have $_POST['gal'] and $_POST['toDelete'] being sent. I don't see gal in your second form, so just add <input type="hidden" name="gal" value="{$_POST['gal']}" />.

Related

Updating specific data in MySql table through form

I am making a online application used for storing user data and order information. I have successfully managed to create a system to which you first add customers and then add orders to specific customers.
I now want to create a function that would let the administrator of the page edit a certain line in the table.
I managed to figure out how to manually change it through a MySql code but I cannot figure out how to change it through a form? Right now I have this, but I do not think this is the correct way to do it... It works, but is very limited. For example, I am not sure what to do, if admin selects date?
PHP AND HTML
<?php
$id = $_GET['id'];
?>
<form action="update.php" method="POST" >
<input type="text" name="id" style="display:none;" value="<?php echo $id; ?> placeholder="Hidden id">
</select>
<select name="change"style="width:32.9%;" required>
<option selected hidden value="">Select a entry you want to edit</option>
<option value="narocnik">Customer</option>
<option value="naslov">Adress</option>
<option value="kraj">City</option>
<option value="enaslov">E-mail</option>
<option value="telefon">Number</option>
<option value="datumzacetka">Start date</option>
<option value="datumkonca">due date</option>
<option value="material">Material</option>
</select>
<input type="text" name="new" style="width:32.5%;" placeholder="New entry">
<input type="submit" value="PoĊĦlji">
</form>
AND THE MYSQL PART
<?php
include 'baza.php';
$change = $_POST['change'];
$new = $_POST['new'];
$id = $_GET['id'];
$id = $_POST['id'];
$sql = "UPDATE projekti SET $change='$new' where id='$id'";
if ($conn->query($sql) === TRUE) {
header("Location: projekti.php?id=$id");
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
As I said, the function works, but it is far from perfect, so any suggestions would be very welcome :) The most important thing would be the ability to have all the fields displayed but only update the newly inputted ones.

Calculate total price of two books using PHP and HTML

I want to know what can be the way using php and html such that I am provided a dropdown of books, I need to select exactly two choices out of that drop down and then calculate sum of price of both the books.
Assuming I had hardcoded the Books say:
Book 1 - $5
Book 2 - $15
Book 3 - $50
I know with if it was to select only one book. But no idea for this one. Please help
Code :
<?php
if(isset($_POST['formSubmit']))
{
$varCurrentBook = $_POST['formBook'];
$errorMessage = "";
if(empty($varCurrentBook))
{
$errorMessage = "<li>You forgot to select a Book!</li>";
}
if($errorMessage != "")
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
else
{
switch($varCurrentBook)
{
//Can use here to find what option is clicked
}
exit();
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<label for='formBook'>Select a Book</label><br>
<select name="formBook">
<option value="0">Select a Book...</option>
<option value="15">The Secret</option>
<option value="10">The Fairy Tales</option>
<option value="5">All about words</option>
<option value="100">Pinaacle Studio</option>
<option value="120">Harry Potter</option>
<option value="200">Thinking in Java</option>
</select>
<input type="submit" name="formSubmit" value="Submit" />
</form>
You need to use a multiple select as:
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<label for='formBook'>Select a Book</label><br>
<select name="formBook[]" multiple><!--The multiple attribute-->
<option value="0">Select a Book...</option>
<option value="15">The Secret</option>
<option value="10">The Fairy Tales</option>
<option value="5">All about words</option>
<option value="100">Pinaacle Studio</option>
<option value="120">Harry Potter</option>
<option value="200">Thinking in Java</option>
</select>
<input type="submit" name="formSubmit" value="Submit" />
</form>
Also note how I changed name from formBook to formBook[]. This changes $_POST['formBook'] to an array of options.
Then you can access it as:
<?php
foreach ($_POST['formBook'] as $names)
{
print "You have selected $names<br/>";
/*Your also need to change your code here accordingly.*/
}
?>
You have to use multiple select with jquery
see: HTML Multiselect Limit
<select name="formBook">
have to become:
<select name="formBook[]" multiple">
Then you can sum all selected elements
if(isset($_POST['formSubmit']))
{
$sum = array_sum($_POST['formBook']); // Sum of all selected elements
...
}
As ARBY answered correct you have to add the multiple attribute to your select element:
<select name="formBook" multiple>
This sends the value attribute. (E.g. 0 / 15 / 10)
So you can check if you have exactly two books like this:
//Check if the user has selected exactly two books
$formBooks = $_POST['formBook'];
if(count($formBooks) !== 2){
//TODO: return error message that user has to select two books
}
//Calculate the value
$result = array_sum($formBooks);
//TODO: return value

send the selected value from a dropdown menu to another page

I have a dropdownlist populated by a MySql database that shows the titles of books stored in my database.
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
I want the option I choose to send it to another php page search.php
This search.php I want it to get the title and search for this specific book details.(title, price, author.... etc) .I tried to do it with but it ruins the page.
Add below code in form that should work for you.
<form action='search.php' method='post'>
<select id="titles" name='titles'>
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
<input type='submit' value='submit'>
</form>
in search.php:
$title = $_POST['titles'];
You just need to add the form above the select tag and need to give the NAME attribute in the select tag to post the data on another page. You can try with the following code:
<form method="post" action="search.php">
<select id="titles" name="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
<input type="submit" name="submit"/>
</form>
and on the search.php page, you can get the value of the dropdown by this:
$title = $_POST['titles'];
Try as below :
<form method="post" action="YOURPAGEPATH">
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
<input type="submit" name="submit"/>
</form>
Without submit button :
<form method="post">
<select id="titles" onchange="this.form.submit();">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
</form>
Surround that with a form with the appropriate action and add a submit button. Otherwise use something like jQuery to listen for the value of that to change and submit the form.
For example:
<form action="search.php" method="GET">
<select id="titles" name="title">
<?php /* put your stuff here */ ?>
</select>
</form>
And then in jQuery:
$(function(){
$('#titles').on('change', function(){
$(this).closest('form').submit();
});
});
Or you could go real old-school and attach the event listener to the select like this:
<form action="search.php" method="GET">
<select id="titles" name="title" onchange="this.parentNode.submit()">
<?php /* put your stuff here */ ?>
</select>
</form>
Just in case if you don't want to use the Submit Button
<script language="Javascript">
function books(book)
{
var url="http://www.example.com/search.php/?q="+book;
window.open(url, "_self");
}
</script>
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option onClick="books('.$row['title'].')" value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
Here the list will call the Books function on Click and pass the arguments to the function which will redirect you to search.php
To retrieve the book name use
$_GET["q"]
Change the URL as required.
And if the problem is solved don't forget to Mark the answer.

keep php result as content in div

EDIT!!! : LINK = http://i299291.iris.fhict.nl/PHP31/DV3/DV3.php
My problem:
I've made two dropdown boxes with several options. The php code is working and the query gets the right result from the database. But now i want to compare two options.
This is what i've got so far, the problem now is the entire page refreshes when i enter the second value from the other dropdown box.
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form id = "leftDropdown" action= "" method="post">
<select name="objectLinks">
<option value="school">School</option>
<option value="klas">Klas</option>
<option value="geslacht">Geslacht</option>
<option value="lengte">Lengte (CM)</option>
<option value="kg">Gewicht (KG)</option>
<option value="opleiding">Opleiding Ouders</option>
<option value="leeftijdJaar">Leeftijd</option>
<option value="interventie">Deelname interventie?</option>
<option value="pestenVoor">Pestincidenten voor interventie</option>
<option value="pestenNa">Pestincidenten na interventie</option>
<option value="bmi">BMI waarde</option>
<option value="overgewicht">Overgewicht</option>
<option value="allochtonenPerc">Percentage Allochtonen</option>
</select>
<input type="submit" name="sendLinks" value="Go!">
</form>
<form id = "rightDropdown" action= "" method="post">
<select name="objectRechts">
<option value="school">School</option>
<option value="klas">Klas</option>
<option value="geslacht">Geslacht</option>
<option value="lengte">Lengte (CM)</option>
<option value="kg">Gewicht (KG)</option>
<option value="opleiding">Opleiding Ouders</option>
<option value="leeftijdJaar">Leeftijd</option>
<option value="interventie">Deelname interventie?</option>
<option value="pestenVoor">Pestincidenten voor interventie</option>
<option value="pestenNa">Pestincidenten na interventie</option>
<option value="bmi">BMI waarde</option>
<option value="overgewicht">Overgewicht</option>
<option value="allochtonenPerc">Percentage Allochtonen</option>
</select>
<input type="submit" name="sendRechts" value="Vergelijk!">
</form>
<div id = "leftDiv">
<?php
include_once 'dv3ToDB.php'; // connect to database *local or at school's server*
session_start();
if(isset($_POST['sendLinks'])){
$selectedValLinks = $_POST['objectLinks'];
// echo "Jij selecteerde: ".$selectedVal;
echo "<script>console.log('$selectedValLinks');</script>";
// $_SESSION["valLinks"] = $selectedValLinks;
// echo $_SESSION["valLinks"];
$query = "SELECT ($selectedValLinks) FROM pesten ORDER BY ($selectedValLinks) * 1";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Gevonden data in ".$selectedValLinks.": " . $row["$selectedValLinks"] . "<br>" ;
}
}else{
echo "0 results";
}
}
?>
</div>
<div id = "rightDiv">
<?php
if(isset($_POST['sendRechts'])){
$selectedValRechts = $_POST['objectRechts'];
// echo "Jij selecteerde: ".$selectedVal;
echo "<script>console.log('$selectedValRechts');</script>";
// $_SESSION["valRechts"] = $selectedValRechts;
// echo $_SESSION["valRechts"];
$conn = mysqli_connect($host,$username,$password,$database)
or die("verbinding mislukt:".mysqli_connect_error());
$query = "SELECT ($selectedValRechts) FROM pesten ORDER BY ($selectedValRechts) * 1";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Gevonden data in ".$selectedValRechts.": " . $row["$selectedValRechts"] . "<br>" ;
}
}else{
echo "0 results";
}
}
?>
</div>
</body>
</html>
i'm running it local now, let me know if you need me to build a database online to help me out.
How can i code it so the PHP output stays in de left DIV and/or the right DIV?
Thank you so much guys and girls! :)
If you need to use only PHP, without any JavaScript (which could allow you to display the result from the database after selecting each option without reloading the page), I'd suggest that, after submitting one form, you add it's result as a hidden input for the other form, so you could read this variable after submitting the 2nd form.
Like this:
<? // Your code for getting the result of the 1st form above returns a variable $res1
// Now insert this into the 2nd form:?>
<input type="hidden" name="stored2" value="<?echo $res1;?>"/>
This way, you can read the variable $_POST["stored2"] after submitting the 2nd form to get what the 1st form returned before.
Do the same for the 1st form to store the results of the 2nd form if it was filled first.
This way, you can compare the results of submitting 2 forms while using only PHP.
EDIT:
You'll need to place all the database requests before the forms to use this method, and just echo the result in the div later.
You want to make a new <iFrame> and then set that <iFrame>'s id as the target for your form.
e.g.:
<form ... target="newframe">
...
</form>
<iFrame id="newframe"></iFrame>
As Lal mentioned you don't need two forms, just one will work.
You need some code that updates the option to selected, if you don't want it to "stick" on page refresh.
Further reading here;
http://www.w3schools.com/tags/att_option_selected.asp
and here
html select option SELECTED
Even try Jquery;
http://forum.jquery.com/topic/how-to-dynamically-select-option-in-dropdown-menu

Using <select> tag to update database in PHP with a submit button

I tried to find ways how to update my database for hours now and it's hurting my brain.
Please, can anyone show me how to update data inside a database by using <select>?
If I select 'Checked-Out' in the form and then click update the StatusID in the database will be updated to '2' but I just can't seem to work it.
<?php
include 'connection.php';
if(isset($_POST['submit'])){
$_var1 = $_POST['status'];
$query3 = mysql_query("UPDATE reservation SET ReservationStatusID = '$_var1' WHERE `ReservationID` = '$id' ");
if($query3)
{
header('location:viewReservedroomsGuest1.php');
}
else
{
echo "Error";
}
}
?>
Here's my select.
<select id="status">
<option value="1">Checked-In</option>
<option value="2">Checked-Out</option>
<option value="3">Pending</option>
<option value="4">Cancelled</option>
</select>
<input type="submit" name="submit" value="Update" />
I'm sorry, I don't make any sense. My brain is tired out already.
<form action="sampleupdate.php" method="post">
<select id="status" name="status">
<option value="1">Checked-In</option>
<option value="2">Checked-Out</option>
<option value="3">Pending</option>
<option value="4">Cancelled</option>
</select>
<input type="submit" name="submit" value="Update" />
</form>
in sampleupdate.php
$val = $_POST['status'];
// and write and execute the update Query here..
Put the select in a <form> with method="post"
Give the select a name attribute
Read the data from $_POST['the name you used']
Use the database API of your choice

Categories