Keeping already set GET parameter values after form submission - php

My URL currently has:
/add_products_flavour.php?pro_id=2
in it. However, the pro_id value gets lost after I submit my form. I want to keep returning to the same page and keep the value of pro_id to be able to submit the form multiple times.
The form:
<form name="flavour_form" method="post" action="add_products_flavour.php">
<label class="add_products_field_label" for="flavour">Aroma:</label>
<input class="w-input" data-name="flavour" id="flavour" maxlength="256" name="flavour" required="required" type="text">
<input class="add_products_form_button w-button" type="submit" name="insert_post" value="Adauga">
<?php insertFlavour()?>
</form>
function insertFlavour(){
global $connection;
$pro_id=getProductID();
if (isset($_POST['insert_post'])) {
$flavour = $_POST['flavour'];
$insert_flavour_query = "insert into flavour (name) values ('$flavour')";
$run_insert_flavour = mysqli_query($connection, $insert_flavour_query);
if($run_insert_flavour){
echo"<script>alert('flavour added!')</script>";
echo "<script>window.open('add_products_flavour.php?pro_id=$pro_id','_self')</script>";
}
}
}
function getProductID(){
if(isset($_GET['pro_id'])) {
$pro_id = $_GET['pro_id'];
return $pro_id;
}
}

solved it by doing
<?php $pro_id = $_GET['pro_id'];
echo "<form name='flavour_form' method='post' action='add_products_flavour.php?pro_id=$pro_id'>";
?>

Related

How can I handover properly the value of more HTML text boxes to a MYSQL Query on the same page?

My task is to modify a MySQL record according to data from forms on a HTML page. I am browsing the Stackoverflow since yesterday morning, but I have found only solution where only one textbox is implemented. It is working. My problem is, when I want to implement more textboxes, that is not working.
This is my currently working version for a simple query with one textbox (to be easy, I do not write here the functions for SQL connection, etc):
HTML:
<input type="submit" name="button4"
class="button" value="Delete record" /><br>
<p>
<label><span>Which record do you wnat to delete?</span></label><br>
<form name="form" action="" method="POST">
<input type='number' name="delteid" id="deleteid" value="">
</form>
</p>
PHP:
//usage of buttons in php:
if(array_key_exists('button1', $_POST)) {
button1();
}
else if(array_key_exists('button2', $_POST)) {
button2();
}
else if(array_key_exists('button3', $_POST)) {
button3();
}
else if(array_key_exists('button4', $_POST)) {
button4();
}
//php function for delete:
function button4() {
$deleteid = $_POST['deleteid'];
$sql = "DELETE FROM list WHERE id = '$deleteid'";
}
I thought, I have to simple duplicate the textboxes and variables for more elements, but it is completely not working. The previously implemented delete function also not working after the modification of code with new function (maybe the new variables disturbing the old one).
The following is the not working new version:
HTML:
<input type="submit" name="button3"
class="button" value="Add record" /><br>
<br>
<p>
<label><span>Please type the elements of new record</span><br>
<br>
<!-- <form name="form" action="" method="POST">
<input type="text" name="addid" id="addid" value="id">
</form>
<form name="form" action="" method="POST">
<input type='text' name="addlocation" id="addlocation" value="Location">
</form>
<form name="form" action="" method="POST">
<input type='text' name="addtype" id="addtype" value="Type">
</form>
PHP:
//usage of buttons in php:
if(array_key_exists('button1', $_POST)) {
button1();
}
else if(array_key_exists('button2', $_POST)) {
button2();
}
else if(array_key_exists('button3', $_POST)) {
button3();
}
else if(array_key_exists('button4', $_POST)) {
button4();
}
//php function for add record:
function button3() {
$addid = $_POST['addid'];
$addlocation = $_POST['addlocation'];
$addtype = $_POST['addtype'];
$sql = "INSERT INTO list (id, location, type) VALUES ('$addid', '$addlocation','$addtype'); ";
}
Could you please tell me, how can I implement the different functions properly, where I would like to receive data from more textboxes? It can be later even 9 or 10 pieces textboxes on the same page.
Thank you!

passing two values (one from text, one hidden) using html and php

I really struggle here and I spent much time on a solution but it seems that I missing something here.
So I have one page that I have 2 values:
$semesterID = $_GET['semesterID'];
$semesterID_old = $semesterID;
I do this as I want to pass 2 values on my next page. I am trying to do that with this:
<form action=ed_semester_v2.php method="post">
<div id="name">
<h2 class="name">Semester ID</h2>
<input class="subjectname" type="text" name="sem_id" id="sem_id" value= "<?php echo $semesterID; ?> " /> <br>
</div>
<input type="hidden" name="semesterID_old" value= "<?php $semesterID_old; ?>"/>
<button type="submit" value="Submit" name="submit">SAVE CHANGES</button>
</form>
The one $semesterID pass fine to the next page but the semesterID_old no. Just to clarify that the semesterid might change by the user and that's ok.
In my next page, I use this code:
if(isset($_POST['submit'])) {
$first_name=$_POST['sem_id'];
$last_name=$_POST['sem_name'];
$semesterID_old= $_POST['semesterID_old'];
if (empty($last_name)) {
echo "Name is empty";
} else {
$query = "UPDATE semesters SET name = '$last_name' where semesterid='$first_name'";
}
if (!mysqli_query($dbconnect, $query)) {
die('An error occurred when submitting your review.');
} else {
// echo "Thanks for your review.";
}
}
echo $first_name;
echo $semesterID_old;
First_name is fine but semesterID_old says that's undefined.
Any help would be much appreciated.
Just change echo semesterID_old; to echo $semesterID_old;

how to set array data for the textbox in php

**I have home.php file and superLec.php file. user can enter studentUniversityId and click on the submit button. particular data i want to show studentName textbox and lecturerName textbox. I want to pass data as a array. How to set array data for textbox value.Please any one help me. **.
home.php
<?php
session_start();
include_once 'include/class.supervisor.php';
$supervisor = new Supervisor();
if (isset($_POST['submit'])) {
extract($_POST);
$autofill = $supervisor->check_auto_fill($studentUniversityId);
/* if ($autofill) {
// Registration Success
header("location:homeLecturer.php");
} else {
// Registration Failed
echo 'Wrong username or password';
}*/
}
?>
<form action="" method="post">
<label for="studentID">Student ID</label>
<input type="text" name="studentUniversityId" id="studentID" placeholder="studentID">
<!--input type="submit" id="autoFill" name="autoFill" value="Auto Fill"><br><br>
<input class="btn" type="submit" name="submit" value="Auto Fill"-->
<label for="studentName">Student Name</label>
<input type="text" id="studentName" name="studentName" value = "<?php echo print_r($autofill);?>" placeholder="Student Name"><br><br>
<label for="lecturerName">Supervisor Name</label>
<input type="text" id="lecturerName" name="lecturerName" value = "<?php echo ($lecturerName) ;?>" placeholder="Supervisor Name"><br><br>
<input type="submit" id="submit" name="submit" value="submit">
</form>
**This is my php file. I want pass array values for the home.php file. can any one help me pls. **
superLe.php
public function check_auto_fill($studentUniversityId){
$query = "SELECT supervisor.lecturerName, supervisee.studentName, supervisee.studentUniversityId FROM supervisee, supervisor WHERE supervisee.lecturerId = supervisor.lecturerId AND supervisee.studentUniversityId = '$studentUniversityId' ";
$result = $this->db->query($query) or die($this->db->error);
$supervisor_data = $result->fetch_array(MYSQLI_ASSOC);
$arr = array("lecturerName", "studentName", "studentName");
return $arr;
}
You have to use JQuery function to set value of form elements.
Post studentUniversityId via ajax get data in response and populate the data to form element.
To create a response with array, you need to extract the fetched data from query and put them into the output array.
I suggest you to use a key/value array.
public function check_auto_fill($studentUniversityId){
$query = "SELECT supervisor.lecturerName, supervisee.studentName, supervisee.studentUniversityId FROM supervisee, supervisor WHERE supervisee.lecturerId = supervisor.lecturerId AND supervisee.studentUniversityId = '$studentUniversityId' ";
$result = $this->db->query($query) or die($this->db->error);
$supervisor_data = $result->fetch_array(MYSQLI_ASSOC);
$arr = array(
'lecturerName' => $supervisor_data[0]['lecturerName'], //the "0" index depends of the query structure, but as I can see, this is a right way
'studentName' => $supervisor_data[0]['studentName'],
)
//$arr = array("lecturerName", "studentName", "studentName");
return $arr;
}
After that in your home.php you can simple extract the data from array and put the elements in the right fields as exampled:
<label for="studentName">Student Name</label>
<input type="text" id="studentName" name="studentName" value = "<?php echo $autofill['studentName']; ?>" placeholder="Student Name"><br><br>
<label for="lecturerName">Supervisor Name</label>
<input type="text" id="lecturerName" name="lecturerName" value = "<?php echo $autofill['lecturerName']; ?>" placeholder="Supervisor Name"><br><br>
Hope it help you.
Marco
From what I see and understand (please excuse if I don't), you want to populate the text fields studentName and lecturerName with their respective values. To me, you code looks correct except one thing. You need to drop the line
$arr = array("lecturerName", "studentName", "studentName");
from superLe.php. The line
$supervisor_data = $result->fetch_array(MYSQLI_ASSOC);
already returns an associative array, which you can use in the variable $autofill in file home.php as $autofill['studentName'] or $autofill['lecturerName'].

Bug in form working with PHP, MySQL and HTML

guys. I was going ok with my app until I got to this type of form, written below. I have 2 items in this table and the ID's are "1" and "2". Even though, if I press "Submit" on the "1" ID item, it prints me "2" every time. Does anyone have a clue what might be the problem? Thank you.
<form method="POST">
<?php
$query = "SELECT * FROM table";
while($row = $query->fetch_array())
{
$id = $row['id'];
?>
<input type="hidden" name="id" value="<?php echo $id; ?>" >
<input type="submit" name="submit">
<?php } ?>
</form>
<?php
if(isset($_POST['submit']))
{
echo $_POST['id'];
}
?>
You're outputting two items into the same form with the same name. When you click the Submit button, it gathers all of the fields in the form based on the name attribute and sends them on to wherever the form is being posted.
If you want to have a button that submits each ID with a separate button, you'd want to try having a new form for each ID. There are other ways of doing this too, but based on your current code, try something like this:
<?php
$query = "SELECT * FROM table";
while($row = $query->fetch_array())
{
$id = $row['id'];
?>
<form method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>" >
<input type="submit" name="submit">
</form>
<?php } ?>
<?php
if(isset($_POST['submit']))
{
echo $_POST['id'];
}
?>

Post form values where Field Names are dynamically created from a loop?

I have a page that is created dynamically from the information that a user has submitted on a previous page. For example on page 1, the user inputs some department names. They can enter as many as they want. On page 2, multiple sections are created for each department that was entered on page one. Inside each of these sections are going to be forms, I currently have it set up so that the names of the forms are created using a variable $a. I need to know how to post these items once the submit button in each section is clicked. I have tried several different ways and nothing is working. I want it so that ONLY the items with the same $a value as the submit button's $a get posted.
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a;?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a;?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
<?php
echo "</div>";
}
?>
*EDIT I need everything to be in one form because the point of the page is to add up all of the values that will be inserted into each of the individual sections later. *
**EDIT 2 :
I figured it out using #dirt 's jquery suggestion.
HTML:
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
<div id=$a>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a ?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a ?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<button type="submit" name="areaSub" value="<?php echo $a ?>" />Add</button>
<?php
echo "</div></div>";
} ?>
jQuery:
<script>
$(document).ready(function() {
$('#<?php echo $a ?>').submit(function() {
.post("include/action.php")
});
});
</script>
PHP:
if(isset($_POST['areaSub'])) {
$areaval = intval($_POST['areaSub']);
$area = mysql_real_escape_string($_POST["dep_area".$areaval.""]);
list($area_id, $area_name) = explode(',', $area, 2);
$bdep = mysql_real_escape_string($_POST["bdep".$areaval.""]);
echo $areaval;
echo $area_name;
echo $bdep;
}
EDIT: If its a single form with multiple sections and you don't want to trim out unwanted form data after the POST then I think you must use jQuery to trim the form values prior to the post to the server, so see my jQuery portion below for a crued example of how you would go about only posting the data for the selected button.
Short answer would be to use the Name/Value attributes of the submit button and evaluate that once posted. Multiple buttons with the same name can have different values and the values don't have to be the labels.
Example:
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<?php
# _POST:
if (isset($_POST['submit']) && $_POST['submit'] == '1') {
echo 'Department 1';
}
if (isset($_POST['submit']) && $_POST['submit'] == '2') {
echo 'Department 2';
}
?>
You could also use jQuery to get all elements contained in a certain Div ID. Something like (this is rough idea):
<div id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
</div>
jQuery:
$('#<?php echo $a; ?>').submit(function() {
do_something_with_form_$a
});
I'm not sure if I understand completely... but the issue you're having is that, when a user hits the 'submit' button for a given set of values (a given department), it submits ALL of the data on the page? And you only want the input fields for that specific area submitted?
Section off forms with the tag:
<form> </form>
So, you'll have multiple areas:
while(...) {
<form method="..." name="form $a">
<input name="... $a">
<input name="... $a">
...
<submit>
</form>
}
(Obviously this is pseudocode, adjust accordingly)

Categories