$_POST["action"] == "edit" - php

How can I go to this if (isset($_POST["action"]) && $_POST["action"] == "edit") statement?
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["action"]) && $_POST["action"] == "edit") {
//Assemble the the postData
);
//Call the RestClient with PUT
RestClient::call("PUT",$postData);
}
}
When I click the 'edit' button like this?
<h4>Edit Customer - <?php echo $c->getCustomerID(); ?></h4>
<!-- The above form looks like this -->
<form method="POST" ACTION="<?php echo $_SERVER["PHP_SELF"]; ?>">
<!-- Would some hidden fields help to route the data? Probably -->
<div class="row">
<div class="six columns">
<label for="name">name</label>
<input type="text" name="ename" value=<?php echo $c->getName();?>>
</div>
<div class="six columns">
<label for="name">address</label>
<input type="text" name="eaddress" value=<?php echo $c->getAddress();?>>
</div>
<div class="six columns">
<label for="name">City</label>
<input type="text" name="ecity" value=<?php echo $c->getCity();?>>
</div>
</div>
<input class="button-primary" type="submit" value="Edit">
</form>

If you meant to catch your submit button's value, the first thing you have to do is give it a name with the value of "action":
<input class="button-primary" name="action" type="submit" value="Edit">
And then match your button's value exactly (meaning case-sensitive):
if (isset($_POST["action"]) && $_POST["action"] === "Edit")
That's how you would do it, although it doesn't make sense unless you're going to have multiple forms where each submit button's name is "action", and doing so is not a good practice. A much more sensible approach is just giving the submit a name that reflects the action:
<input class="button-primary" name="edit" type="submit" value="Edit">
And then simply check if that name is set:
if (isset($_POST["edit"]))
Also note that ACTION="<?php echo $_SERVER["PHP_SELF"]; ?>" is unnecessary, as the default action is to submit to self, so you can just drop the entire attribute.

You just need to add a hidden input
<input type="hidden" name="action" value="edit"/>

Related

PHP form action is not working and is not updating querystring [duplicate]

This question already has answers here:
When submitting a GET form, the query string is removed from the action URL
(13 answers)
Closed 1 year ago.
I'm trying to make a filter using a MySQL database and PHP for a school project, but whenever I press my filter button, it doesn't execute the action so the querystring is not updated.
The reason it's referring to my homepage is because of this block code:
if (empty($_GET['page'])) {
$_GET['page'] = 'home';
}
if (empty($routes[$_GET['page']])) {
header('Location: index.php');
exit();
}
This is when there is no $GET['page'] passed, and it will just refer to my homepage.
So the problem probably lies with my form action, which is clearly correct: action="index.php?page=agenda"
<form class="filter_form" method="get" action="index.php?page=agenda">
<div class="location">
<p class="filter_by">filter by:</p>
<label for="location">location</label>
<select name="location" id="location">
<option value="all">-- Locations --</option>
<?php foreach($locations as $location): ?>
<option value="<?php echo $location['location']; ?>"
<?php
if(!empty($_GET['location'])){
if($_GET['location'] == $location['location']){
echo ' selected';
}
}
?>
>
<?php echo $location['location']; ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="checker_wrapper">
<div>
<input type="checkbox" name="check1" id="check1">
<label for="check1">skills only</label>
</div>
<div class="bottom_row">
<input type="radio" name="group" id="radio1">
<label for="radio1">day</label>
</div>
</div>
<div class="radio_wrapper">
<div>
<input type="checkbox" name="check2" id="check2">
<label for="check1">in group</label>
</div>
<div class="bottom_row">
<input type="radio" name="group" id="radio2">
<label for="radio2">evening</label>
</div>
</div>
<input class="button filter_button" type="submit" value="filter" />
</form>
When I press the filter button, I'm in the homepage and the querystring is like this: index.php?location=all, so the $_GET[location] for my project works. It's just not adding the correct page in the string.
Your form is overriding the query string parameters in your action attribute.
Instead of putting the query string in the action, add a hidden field
<form class="filter_form" method="get" action="index.php">
<!-- ... the rest of your form code -->
<input class="button filter_button" type="submit" value="filter" />
<input type="hidden" name="page" id="page" value="agenda" />
</form>
Related post: submitting a GET form with query string params and hidden params disappear
You should use a hidden input element in your form rather than adding your parameters in the URL:
<input id="page" name="page" type="hidden" value="agenda">
You should add a hidden input field into the form. Instead attach it into the action string,
<input type="hidden" name="page" value="agenda" />
Add a hidden field:
<input type="hidden" name="page" value="agenda" />
The get parameter in your action is overruled by the method "GET".

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";
}
?>

Can't retrieve values from other page (Stored As Label)

I am new to PHP and I am trying to get information from a form to another page, but the data won't transfer over when I hit submit. What am I doing wrong? Should I be trying to use GET instead of POST? What is the best way to debug something like this?
The path to information.php is definitely correct.
<form action="information.php" method="post" type="post">
<div class="row" style="padding-bottom: 20px;">
<label name="tempID"><?php echo $number; ?></label>
<button class="btn" name="submit" type="submit">More Details</button>
</div>
</form>
This file is in a different page (information.php)
if (isset($_POST["tempID"]))
{
$infoID = $_POST['tempID'];
}
echo $infoID;
<input type="hidden" name="tempID" value="<?php echo $number; ?>" />
Add this next to your original echo, post variables can't be stored in a label. Also remove the name value from the label
You need a submit input instead of a button with the name submit. Change the button's html to:
<input type='submit' value='More Details'>
Change Label in input
<form action="information.php" method="post">
<div class="row" style="padding-bottom: 20px;">
<input name="tempID" value="<?php echo htmlentities($number); ?>"/>
<button class="btn" name="submit" type="submit">More Details</button>
</div>
</form>
labels are only to display information. they are not submitted during form submission.
<form action="information.php" method="post" type="post">
<div class="row" style="padding-bottom: 20px;">
<label>Number:</label>
<input name="tempID" value="<?php echo $number; ?>"/>
<input class="btn" id="submit" name="submit" type="submit" value="More Detail" />
</div>
</form>

PHP - HTML - Displaying input form even after submit

I have two input forms and would like the second one to stay on the page even when it is submitted.
<div id="first">
<form method="POST">
Number: <input type="text" name="number"><br>
<input type="submit" value="Submit">
</form><br>
</div>
<div id="second">
<?php
if (isset($_POST['number']) && !empty($_POST['number'])){
?>
<form method="POST">
Name: <input type="text" name="name"><br>
<input type="submit" value="Submit">
</form><br>
<?php
}
?>
</div>
<div id="third">
<?php
if (isset($_POST['name']) && !empty($_POST['name'])){
echo "TEST";
}
?>
</div>
When I submit my first form, the second form appears correctly since $_POST['number'] is not empty. However, the content of 'number' disappears as soon as I submit it.
Then, when I submit the second form, the word "TEST" appears correctly but the form itself disappears since $_POST['number'] from the first form is now empty.
I need to find a way to somehow save the value of number in the first form so that the second form does not disappear.
Any suggestions?
You can add hidden field:
<input type="hidden" name="number" value="<?php echo $_POST['number']; ?>">
Then your second form will be changed to:
<form method="POST">
Name: <input type="text" name="name"><br>
<input type="hidden" name="number" value="<?php echo $_POST['number']; ?>">
<input type="submit" value="Submit">
</form

one form multiply tasks with php

I am making an CMS, and the pages that are going to be made by the clients, they are going to use the same form to edit the page, add new page, add new subpage, edit the subpage.. I dont want to copy that form multiply times for each action. I would like to know if anyone of you has an idea how to use that form for all of the actions in the best way.
<div class="containerholder">
<div class="container">
<div id="contain">
<form method="post" id="customForm" action="edited.php">
<div>
<label for="name">Navigation</label>
<input id="name" name="navigation" type="text" value="<?php echo $navigation ?>" />
</div>
<div>
<label for="message">Content</label>
<textarea id="content" name="content" cols="" rows="" ><?php echo $content ?></textarea>
</div>
<div>
<input type="hidden" name="id" id="id" value="<?php echo $id ?>">
<input id="send" name="send" type="submit" value="Save and update" />
</div>
</form>
</div>
</div></div>
this is the form if you want to see it, I have an idea how to make it but it doesnt look to me like it is the best way (professional).
Thank you for your time!
Use multiple submit-buttons for every task and identify the requested task by the name of the submit-button(usually only a clicked submit-button is submitted)
Professional: it's not "professional" in my eyes to use a single form for all tasks.
You could use multiple submit buttons, as google does with its search/feeling lucky choice:
<input id="send" name="send" type="submit" value="edit" />
<input id="send" name="send" type="submit" value="new page" />
<input id="send" name="send" type="submit" value="edit subpage" />
<input id="send" name="send" type="submit" value="new subpage" />
The name=value pair for the submit button is transmitted as it is for the rest of the form data, so at the server it is collected using the same functionality ($_GET or $_POST etc.)
Alternatively, you could use form controls and javascript events to control the communication to the server via AJAX (it's more flexible, you can control exactly what data is transmitted via javascript, and it's seamless for the end-user who isn't waiting for page reloads). See http://www.w3schools.com/ajax for simple source code.
add two or more radio button to select you function, for example :
<form action="" method="post">
...
<input type="radio" name="function" value="Add" />
<input type="radio" name="function" value="Delete" />
<input type="radio" name="function" value="Edit" />
..
</form>
and in php :
<?PHP
...
if( $_POST['function'] == 'Add' )
{
//Add Function
} else
if( $_POST['function'] == 'Delete' )
{
//DeleteFunction
} else
if( $_POST['function'] == 'Edit' )
{
//EditFunction
}
...
?>
I would suggest making a single form, and then using scripting to switch it between modes and datafill the fields as required.
For links directing to the form.
/* To create a New Record */
New Record
/* To delete an Existing Record */
Delete #123
/* To edit an Existing Record */
Delete #123
For the form file (in my example thisfile.php itself).
<?php
if( isset( $_GET['action'] ) ){
switch( strtolower( $_GET['action'] ) ){
case 'delete' :
/* Perform Deletion Action Here */
break;
case 'edit' :
if( isset( $_POST['save'] ) ){
/* Perform Modification of Existing Values Here */
}else{
/* Perform Query for Existing Values Here */
}
break;
case 'new' :
if( isset( $_POST['save'] ) ){
/* Perform Creation of New Record Here */
}else{
/* Set Default Values */
$navigation = '';
$content = '';
}
break;
}
}
?>
<div class="containerholder">
<div class="container">
<div id="contain">
<form method="post" id="customForm">
<?php if( isset( $_GET['action'] ) ){ ?>
<input type="hidden" name="action" value="<?php echo $_GET['action']; ?>" />
<?php } ?>
<?php if( isset( $_GET['record'] ) ){ ?>
<input type="hidden" name="record" value="<?php echo $_GET['record']; ?>" />
<?php } ?>
<div>
<label for="name">Navigation</label>
<input id="name" name="navigation" type="text" value="<?php echo $navigation ?>" />
</div>
<div>
<label for="message">Content</label>
<textarea id="content" name="content" cols="" rows="" ><?php echo $content ?></textarea>
</div>
<div>
<input id="send" name="send" type="submit" value="Save and update" />
</div>
</form>
</div>
</div>
</div>

Categories