Run different code depending on which form is submitted

I have this code to be run when a form is submitted

if ($_SERVER['REQUEST_METHOD'] == "POST"){
...
...
}

How canI further specify which form is submitted (if he 2 form have different names)
something like

if ($_SERVER['REQUEST_METHOD'] == "POST"){
   if (isset($_POST['myform_a'])) {
   ...
   }
...
}

… like that.

2 Likes

different forms can lead to different URLs by the formaction attribute within the button, so you have different files to proces or different routes.

Try and use PHP to reveal the $_POST contents and perhaps it would enable you to make a decision:

<?php
declare(strict_types=1); // fail fast validation 
echo '<pre>'; // cosmetic, adds line feeds to each element
  print_r($_POST); 
echo '</pre>'
...
// form stuff goes here
...

I did that, and got


But the form I submitted has the name attribute

<form name="new" method="POST" action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>">

so why is the name not showing in the POST?
I have this which seems not to work

if ($_SERVER['REQUEST_METHOD'] == "POST"){
	
	echo '<pre>';
	print_r($_POST);
	echo '</pre>';
	
   if (isset($_POST['new'])) {

	$width = isset($_POST['width']) ? $_POST['width'] : NULL;

	$beginning_x = isset($_POST['beginning_x']) ? $_POST['beginning_x'] : NULL;

   $sql = "INSERT INTO devices (back,beginning_slot,ending_slot,device,width,beginning_x) VSLUES (".$_POST[back].",".$_POST[beginning_slot].",
	".$_POST[ending_slot].",'".$_POST[device]."',".$width.",".$beginning_x.")";
	
		if ($conn->query($sql) === TRUE) {
			$last_id = $conn->insert_id;
			echo "New record created successfully. Last inserted ID is: " . $last_id;
		} else {
			echo "Error: " . $sql . "<br>" . $conn->error;
		}
		
	} else {
		
	$panel_id = $_POST['panel_id'];

	$width = isset($_POST['width']) ? $_POST['width'] : NULL;

	$beginning_x = isset($_POST['beginning_x']) ? $_POST['beginning_x'] : NULL;
	
		  // Attempt update query execution
	$sql = "UPDATE devices SET back = '".$_POST[back]."',  beginning_slot = '".$_POST[beginning_slot]."',
	 ending_slot = '".$_POST[ending_slot]."', device = '".$_POST[device]."',  width = '".$width."', beginning_x = '".$beginning_x."'
	WHERE panel_id = ".$panel_id;
   }

Here’s the question I would be asking myself:

“Why am I using the same form field names for multiple forms?”
“Why do I have two forms that have identical fields? Could I do this with a single form instead, but with A: different submit buttons or B: a selection indicator?”

I would suggest targeting different files. Don’t stuff everything into 1 single file. It’s going to be a nightmare for you to manage. You may say it’s “easy” to manage now, but later down the line you’ll actually regret it. Single files are the most hardest ones to maintain. Physically they aren’t because you’re dealing with 1 single file. But mentally, it’s a nightmare.

Also, looking at your queries, consider looking at the syntax for an INSERT...ON DUPLICATE KEY UPDATE query that could unify your efforts.

Is the form sending rack_id but the script looking for $_POST['panel_id'] ?

I didn’t know that was an option, so it’ll be loads easier if I deal with only 1 form instead of 2 then I could handle it like

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST"){
	
	echo '<pre>';
	print_r($_POST);
	echo '</pre>';
	

	$width = isset($_POST['width']) ? $_POST['width'] : NULL;

	$beginning_x = isset($_POST['beginning_x']) ? $_POST['beginning_x'] : NULL;

   $sql = "INSERT INTO devices (back,beginning_slot,ending_slot,device,width,beginning_x) VSLUES (".$_POST[back].",".$_POST[beginning_slot].",
	".$_POST[ending_slot].",'".$_POST[device]."',".$width.",".$beginning_x.")
	ON DUPLICATE KEY UPDATE devices SET back = '".$_POST[back]."',  beginning_slot = '".$_POST[beginning_slot]."',
	 ending_slot = '".$_POST[ending_slot]."', device = '".$_POST[device]."',  width = '".$width."', beginning_x = '".$beginning_x."'
	WHERE panel_id = ".$_POST[panel_id];


	if(mysqli_query($conn, $sql)){
    echo "<h1 class='text-center'style='margin:20px'><img src='images/loading.gif'></h1>";
?>

Then, I can run either query from 1 form

			  <form method="POST" action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>">
				<div class="form-row">
					<div class="form-group col-sm-2">
						<div class="input-group">
							<div class="input-group-prepend">
								<div class="input-group-text">#</div>
							</div>
						  <input type="text" name="beginning_slot" class="form-control" value="<?=$row['beginning_slot']?>">
						  <input type="text" name="ending_slot" class="form-control" value="<?=$row['ending_slot']?>">
						</div>
					</div>
					<div class="form-group col-sm-2">
						<div class="input-group">
							<div class="input-group-prepend">
								<div class="input-group-text">#</div>
							</div>
						  <input type="text" name="beginning_x" class="form-control" value="<?=$beginning_x?>">
						</div>
					</div>
					<div class="form-group col-sm-2">
						<div class="input-group">
							<div class="input-group-prepend">
								<div class="input-group-text">#</div>
							</div>
						  <input type="text" name="width" class="form-control" value="<?=$width?>">
						</div>
					</div>
					<div class="form-group col-sm-2">
					  <select name="back" class="form-control">
					  <option value="<?=$row['back']?>"><?=$orientation?></option>
					  <option value="1">Back</option>
					  <option value="0">Front</option>
					  </select>
					</div>
					<div class="form-group col-sm-3">
					  <input type="text" name="device" class="form-control" value="<?=$row['device']?>">
					</div>
					<div class="form-group col-sm-1">
					  <button type="submit" class="btn btn-outline-dark" style="margin-left:5px">Update</button>						
					</div>
				</div>
			  <input type="hidden" name="panel_id" value="<?=$row['panel_id']?>">
			  <input type="hidden" name="rack_id" value="<?=$rack_id?>">
			  </form>

I do have 2 questions though,
This form, if a new record is being added, it wont give me an error if I have value=“” on some of the inputs

<input type="text" name="device" class="form-control" value="">

and manually enter a name
Also, my table is sett up like

create table devices (
   panel_id INT NOT NULL AUTO_INCREMENT,
   rack_id INT NOT NULL,
   back CHAR(1) DEFAULT '1',
   beginning_slot DECIMAL(3,1) NOT NULL,
   ending_slot DECIMAL(3,1) NOT NULL,
   device VARCHAR(100) NOT NULL,
   width INT DEFAULT NULL,
   beginning_x INT DEFAULT NULL,
   FOREIGN KEY ( rack_id ) REFERENCES racks ( rack_id) ,
   PRIMARY KEY ( panel_id )
);

In order for Better query to work, don I need to set up a unique key so I can test if a DUPLICATE one exhists?

Yes I kind of made that confusing
I want the form to either insert a panel, or update an existing panel, and a rack can be assigned man panels.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.