PHP two forms, one page - php

So in school we're learning about OOP in PHP and for our assignment we need to use 2 forms. this is the first time I'm using 2 forms in one page and I've been trying to figure out how to check which form is being submitted and create the according object.
Apparently, afer looking at some other questions, just using if (!empty($_POST['SubmitButtonName'])) should work, but it doesnt.
Hope someone can help me and tell me what I'm doing wrong :)
PHP:
if (!empty($_POST['sportwgn']))
{
try
{
$sport->Merk = $_POST['merk'];
$sport->AantalPassagiers = $_POST['AantalPassagiers'];
$sport->AantalDeuren = $_POST['AantalDeuren'];
$sport->Stereo = isset($_POST['stereo']) ? true : false;
$sport->Save();
$succes= "Uw sportwagen is gereserveerd!";
}
catch( Exception $e)
{
$error = $e->getMessage();
}
}
if (!empty($_POST['vrachtwgn']))
{
try
{
$vracht->Merk = $_POST['merk'];
$vracht->AantalPassagiers = $_POST['AantalPassagiers'];
$vracht->AantalDeuren = $_POST['AantalDeuren'];
$vracht->MaxLast = $_POST['MaxLast'];
$vracht->Save();
$succes= "Uw vrachtwagen is gereserveerd!";
}
catch( Exception $e)
{
$error = $e->getMessage();
}
}
Forms:
<form action="" method="post">
<label for="merk">merk</label>
<input type="text" id="merk" name="merk">
<br>
<label for="AantalPassagiers">Aantal passagiers</label>
<input type="number" min="2" max="4" id="AantalPassagiers" name="AantalPassagiers">
<br>
<label for="AantalDeuren">Aantal deuren</label>
<input type="number" min="1" max="5" id="AantalDeuren" name="AantalDeuren">
<br>
<label for="stereo">Stereo?</label>
<input type="checkbox" name="stereo" id="stereo" value="stereo"><br>
<br></div><div class="box">
<button type="submit" name="sportwgn">Reserveer</button></div>
</form>
</div>
</div>
<div id="container">
<h1 class="box">Reserveer een Vrachtwagen!</h1>
<div id="content">
<form action="" method="post">
<label for="merk">merk</label>
<input type="text" id="merk" name="merk">
<br>
<label for="AantalPassagiers">Aantal passagiers</label>
<input type="number" min="2" max="4" id="AantalPassagiers" name="AantalPassagiers">
<br>
<label for="AantalDeuren">Aantal deuren</label>
<input type="number" min="1" max="5" id="AantalDeuren" name="AantalDeuren">
<br>
<label for="MaxLast">Max last</label>
<input type="number" min="1" max="5" id="MaxLast" name="MaxLast"><br>
<br></div><div class="box">
<button type="submit" name="vrachtwgn">Reserveer</button></div>
</form>

Since your forms post on the same page (...action=""...) divide your code in php side to two actions by the submit button.
for the forms with
<button type="submit" name="sportwgn">Reserveer</button></div>
use
if(isset($_POST['sportwgn'])) {
// your code
}
and for
<button type="submit" name="vrachtwgn">Reserveer</button></div>
use
if(isset($_POST['vrachtwgn'])) {
// your code
}

You can use if(isset($_POST['buttonName'])) to check if it's in the post values.

Use action attribute to submit forms to different destinations.
<form action="firstForm.php" method="post">
...
</form>
<form action="secondForm.php" method="post">
...
</form>
And create 2 files for handling form posting.

Related

How to make a form disappear after submit in Wordpress plugin

I am building a WordPress plugin for my livechat. When someone downloads the plugin, I want them to fill out some information (name, e-mail, etc). After submitting that info, the form has to disappear/hide. For some reason I am not successful and imo I've tried everything. At the moment I'm trying to do it with an if-statement checking if the submit-button isset(). Unfortunately that didn't work.
Can someone please help me? The code for display the form and the page after submitting:
<?php
public function display_plugin_setup_page()
{
if (isset($_POST['submitForm'])) {
?>
<form action="options.php" method="post">
<?php
settings_fields('mister_chat_options');
do_settings_sections($this->plugin_name); ?>
<input name="submit" class="button button-primary" type="submit" value="<?php esc_attr_e('Save'); ?>" />
</form>
<?php
} else {
// create the form
?>
<form method="post" action="sendmail.php">
<input type="hidden" name="formSent">
<fieldset>
<input placeholder="Voornaam" type="text" id="vnaam" name="vnaam">
</fieldset>
<fieldset>
<input placeholder="Achternaam" type="text" id="anaam" name="anaam">
</fieldset>
<fieldset>
<input placeholder="Bedrijfsnaam" type="text" id="bnaam" name="bnaam">
</fieldset>
<fieldset>
<input placeholder="E-mailadres" type="email" id="email" name="email">
</fieldset>
<fieldset>
<input placeholder="Telefoonnummer" type="tel" id="telef" name="telef">
</fieldset>
<fieldset>
<input type="submit" name="submitForm" id="contact-submit" data-submit="...Verzenden">
</fieldset>
</form>
<?php
}
}
I placed the sendmail.php file inside the file above and that fixed my problem.

checking users html form info with PHP on same page

I am trying to write a simple html form which requires the user to enter the correct email and password, using $_SERVER as the action for my form. I do not want to send the POST info to another php page, but I want to do it on the same page instead.
I have set two variables, $correct_email and $correct_password.
Here is the form;
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="submit" value="Submit">
</form>
Here is what I am trying to get work in PHP
$correct_email = "(my personal email)";
$correct_password = "password"];
if ($_POST['eml']) == $correct_email && ($_POST['pwd']) == $correct_password {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
It is not working, please help! I am also unclear on whether the PHP should come before or after the form.
Also, I would like to have the form be cleared from view, on the page, when the correct info is entered.
Thanks so much
Change the name of the submit button - never call anything "submit" in a form
Put the test at the top
You had issues with the ( and ) in the test
Also an issue with a ] after "password"
<?php
if ($_POST["subBut"] === "Submit") {
$correct_email = "(my personal email)";
$correct_password = "password";
if ($_POST['eml'] == $correct_email && $_POST['pwd'] == $correct_password) {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
}
else {
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="subBut" value="Submit">
</form>
<?php } ?>

How to get two html forms working in one .php file?

Now i have 4 buttons on my page. They define different parameters. What i want to do, is use php switch case, and after pressing for example button1 i want the datetimepicker, and a chart appear as well. I read that i can manage with that problem using two html forms in one php file adding form id, but im only getting the first form to work properly while the second one seems to fail with sending the data using POST method. Below how i try to do this:
<form id="switch" action="data-from-database.php" method="post">
<h2 align="center">Wybierz interesujący Cię przedział
czasowy</h2></br>
<div id="pudlo">
<input type="submit" name="przycisk"
class="input_wykresy" value="temperatura" form="switch">
<input type="submit" name="przycisk"
class="input_wykresy" value="wilgotność" form="switch">
<input type="submit" name="przycisk"
class="input_wykresy" value="ciśnienie" form="switch">
<input type="submit" name="przycisk"
class="input_wykresy" value="irradiancja" form="switch">
</div>
</form>
<?php
//PHP part here is what i want to appear on the page after using switch
switch ($_REQUEST['przycisk'])
{
case "temperatura":
//Once again HTML part
?>
<form id="picker" action="data-from-database.php" method="post">
<input type="text" name="from_date"
id="from_date" class="input_wykresy1" placeholder="Od" autocomplete="off">
<input type="text" name="to_date" id="to_date"
class="input_wykresy1" placeholder="Do" autocomplete="off">
<input type="submit" name="filter" id="filter" value="Wyszukaj"
form="picker" class="input_wykresy"/>
</form>
<?php
if (isset($_POST["from_date"], $_POST["to_date"])) // php code which i want to work in case
{
$handle = $link->prepare("SELECT temperatura, czaspomiaru FROM Pomiary
WHERE czaspomiaru BETWEEN'" . $_POST["from_date"] . "' AND
'" . $_POST["to_date"] . "'");
}
$dataPoints = [];
//Best practice is to create a separate file for handling connection to database
try
{
// Creating a new connection.
// Replace your-hostname, your-db, your-username, your-password according to your database
$handle->execute();
$result = $handle->fetchAll(\PDO::FETCH_OBJ);
if ($result)
{
foreach ($result as $row)
{
array_push($dataPoints, ["label" => $row->czaspomiaru, "y" =>
$row->temperatura]);
}
}
else
{
?>
<div>
<h2>
<center>Nie znaleziono pasujących rezultatów</center>
<h2>
</div>
<?php
}
$link = null;
}
catch (\PDOException $ex)
{
print($ex->getMessage());
}
}
First form working properly, which i tested on buttons. Second one doesnt work, because when i press the submit button refering to datetimepickers, nothing shows on chart i assume no data been sent. I wonder how can i get the second form working?
You might want a hidden field holding the name/value pair of the button taking you to the subform since the button is not part of that form.
<form id="picker" action="data-from-database.php" method="post">
<input type="hidden" name="przycisk" value="temperatura">
<input type="text" name="from_date"
id="from_date" class="input_wykresy1" placeholder="Od" autocomplete="off">
<input type="text" name="to_date" id="to_date"
class="input_wykresy1" placeholder="Do" autocomplete="off">
<input type="submit" name="filter" id="filter" value="Wyszukaj"
form="picker" class="input_wykresy"/>
</form>
Or, since you do not use the submit button name="filter" in your PHP code, you could just alter that button.
<form id="picker" action="data-from-database.php" method="post">
<input type="text" name="from_date"
id="from_date" class="input_wykresy1" placeholder="Od" autocomplete="off">
<input type="text" name="to_date" id="to_date"
class="input_wykresy1" placeholder="Do" autocomplete="off">
<input type="submit" id="filter" name="przycisk" value="temperatura"
form="picker" class="input_wykresy"/>
</form>
Consider using four radio buttons (instead of four submit buttons). Your site visitor(s) can select which radio button and submit. The code could easily be handled with if, elseif, else conditions, and the output could appear in one designated area, regardless of which radio button was selected.
<form id="switch" action="data-from-database.php" method="post">
<h2 align="center">Wybierz interesujacy Cie przedzial
czasowy</h2></br>
<div id="pudlo">
<input type="radio" name="przycisk" class="input_wykresy" value="temperatura">
<input type="radio" name="przycisk" class="input_wykresy" value="wilgotnosc" >
<input type="radio" name="przycisk" class="input_wykresy" value="cisnienie" >
<input type="radio" name="przycisk" class="input_wykresy" value="irradiancja" >
<input type="submit" value="submit">
</div>
</form>
<?php
if (przycisk === "temperatura") {
echo "yada";
} elseif (przycisk === "wilgotnosc") {
echo "yada";
} elseif (przycisk === "cisnienie") {
echo "yada";
} else {
echo "yada";
}
?>

POST not working with no signs of error

haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...

PHP and HTML 5 validation message

I would like to know how do I edit in HTML5 validation message if there is error in form.
<form method="post" id="login" action="<?php echo $_SERVER['PHP_SELF']?>">
<div class="form_settings">
<label for="textfield">Meno:</label>
<input class="zaoblene" type="text" name="meno" value="Meno....." onclick="javascript: document.forms['login'].meno.value=''" required>
<br><br>
<label for="textfield">Heslo:</label>
<input class="zaoblene" type="password" name="heslo" value="heslo....." onclick="javascript: document.forms['login'].heslo.value=''" required>
<br><br>
<input class="prihlasit" type="submit" name="prihlasit" value="prihlásit" >
</div>
</form>
You can use javascript to do it!
Here you've the jquery plugin that does this work : http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Categories