how to call variable in next page - php

Am getting Notice: Undefined index: getdobcval in C:\xampp\htdocs\bgtest\buy_test.php on line 11
Am trying call variable, how to call variable from page1.php to page2.php
am using below form action
<form action="" method="POST">
<input type="text" name="getdobcval">
<input type="submit">
</form>
when user click submit value, it'l store the value in database and show the values in same page, i used page redirectory option after function finish (after 5 seconds). page redirectory working but after page am getting error
how to call <input type="text" name="getdobcval"> value ?
this is my database
if(isset($_POST['submit'])){
$getdob_cval = $_POST['getdobtcval'];
$conn = mysqli_connect("localhost", "root", "", "bgtest");
$sql = "INSERT INTO bgtest(getdobtcval) VALUES('$getdob_cval')";
if (mysqli_query($conn, $sql)){
echo "Please wait, We are redirecting another page!";
echo '<script> setTimeout(function(){
window.location.href="buy_test.php"
},3000);
</script>';
}else{
echo "error";
}
mysqli_close($conn);
}

This is usually done via a server-side script. Let's say script.php
<form action = "script.php"...
Or make sure that
<form action = ""...
Is specified in from on a .PHP page itself.
When the script refreshes, the values will be available via following array:
<?php
$a = $_POST["getdobcval"]; // Submitted as POST
$b = $_GET["getdobcval"]; // Submitted as GET
?>
Or alternatively you can receive it regardless of how it was submitted by:
<?php
$c = $_REQUEST["getdobcval"];
?>

Related

$_POST input not reaching handler page

i have a form it has a hidden input called id, when it submits it goes to a handler page and that filters other forms and decides what to do through a switch statement, this form is supposed to call a class function through the handler and send the id to the parameter of the function, but the id input is not reaching the handler, i checked by echo $id and nothing shows up so i'm sure the id is not sent to the function parameter, where is the error i can't find it i spent two hours looking for it.
the form
function showPassengerRides($db)
{
//fetching the data from database
$row=$stmt->fetchAll();
if($row)
{
foreach ($row as $stmt):?>
<form action="rideHandler.php" method="POST" class="rides" >
//echoing data from database
<input type = "hidden" name = "id" value = "<?=$stmt['id'] ?>" />
//i echoed here and it worked, so i'm sure the problem is not from the SQL statement
<button class="delete-button" name="formSubmit" value="delete">Delete</button>
</form>
<?php }
the handler page
switch ($_POST['formSubmit']) {
case 'delete':
$id = $_POST['id'];
//i echoed here but nothing showed up
$obj = new databaseUpdate();
$obj->deleteReservation($id, $db);
break;
}

How to store user input from php form in php session

I wanted to store list of users from php form in a php session
I defined an empty array at the beginning of the session and tried to
collect the user names at every submit.
session_start();
$persons=array();
if (isset($_POST)) {
$_SESSION['val']=$_POST['val'];
$persons=$_SESSION['val'];
}
foreach($persons as $d){
echo $d;
echo '</br>';
}
<form action="exam.php" method="post">
Enter a new Person: <input type="text" name = "val">
<input type="submit" name = "send">
</form>
I expected to have the list of persons I submitted returned from the
array
but every time I submit, the last submit replaces the first one.
You are overwriting the array each time:
$persons = $_SESSION['val'];
In order to push data to an array in php you must do it this way:
$persons[] = $_SESSION['val'];
If what you want to is store all persons on session, without overwriting them each time you first need to check if session exist, if not, create it.
if(!isset($_SESSION['persons'])){
$_SESSION['persons'] = array()
}
And then change how you store the info in the session, example:
$_SESSION['persons'][] = $_POST['val'];
Then you can do:
foreach($_SESSION['persons'] as $d){
echo $d;
echo '</br>';
}
So the code will look like:
session_start();
$persons=array();
if(!isset($_SESSION['persons'])){
$_SESSION['persons'] = array();
}
if (isset($_POST)) {
$_SESSION['persons'][] = $_POST['val'];
}
foreach($_SESSION['persons'] as $d){
echo $d;
echo '</br>';
}
<form action="exam.php" method="post">
Enter a new Person: <input type="text" name = "val">
<input type="submit" name = "send">
</form>
I did not compile the code, check for syntax errors, but the procedure is right.

Get submit button value from multiple buttons php

I have a dynamic menu inside a form element code is here
<form method="GET" action="index.php">
<?php
display_menu(); // this function generates menu items
?>
</form>
after the menu is generated every menu item is a submit button of the above form I want to get input of a single element by name or id attribute of submit button, and load a post from database.
<form method="GET" action="index.php">
<input type="submit" name="page-1" id="page-1" value="page-1">
<input type="submit" name="page-2" id="page-2" value="page-2">
<input type="submit" name="page-3" id="page-3" value="page-3">
<input type="submit" name="page-4" id="page-4" value="page-4">
</form>
so when any input button is pressed the function display_post() is called. Code of the function is as follows:-
function display_post(){
$conn = mysqli_connect('localhost', 'root', '', 'posts') or die('cannot connect');
if($_SERVER['REQUEST_METHOD'] == 'GET' ){
$blog_post_id = $_GET["id"];
$sql = "SELECT * FROM blog_posts where id='blog_post_id' ";
$result = mysqli_query($conn, $sql) or die('cannot load');
while ($row = mysqli_fetch_assoc($result)){
if($row > 0){
echo '<div>'.$row['content'].'</div>';
}else echo 'no posts';
}
}
}
However, the display_post() method is called inside a content tag whereas the display_menu() is called inside another div.
So the problem is I'm unable to get the id of the to submit button any help will be appreciated thanks in advance.
When you click on the submit button you will get all inputs values of the form.
Do one thing put this statement print_r($_GET);exit; in display_post() as a first line and see what you get after clicking on submit button.
Note: 1. you want to get the value of clicked button then you should use javascript or jQuery and Ajax.
2. You can not get input fields value by fields ID.
ex. if we have field <input type="submit" name="page-1" id="page-1" value="page-1">
then we can get its value as:
echo $_GET['page-1'];
page-1 is the input field name.
You can make 5 different forms for each button and change the action to
action="index.php?id=1"
Then use $_GET['id']
Also change id='blog_post_id' " to id='$blog_post_id' "
If I am guessing right what is your difficulty then
$blog_post_id = $_GET['id'];
$sql = "SELECT * FROM blog_posts where id='".$blog_post_id."' ";//I modified this line
$result = mysqli_query($conn, $sql) or die('cannot load');

Run two completely different sqli queries inside one script

I'm new to php.
I have this page:
<?php
function renderForm($id, $StaffFullName, $StaffJobPosition, $error)
{
?>
<!doctype html>
<html>
<head><title></title></head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div>'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p>ID: <?php echo $id; ?></p>
Name: * <input type="text" name="StaffFullName" value="<?php echo $StaffFullName; ?>"/><br/>
Job Position: * <select name="JobPosition">
<?php
$query = "SELECT * FROM LUT_JOBPOS";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)){
if ($StaffJobPosition == $row['JobposID'])
{
echo "<option value='{$row['JobposID']}' selected='selected'>{$row['JobposTitle']}</option>";
}
else {
echo "<option value='{$row['JobposID']}'>{$row['JobposTitle']}</option>";
}
}
$result->close();
?>
</select><br/>
<input type="submit" name="submit" value="Update">
<input type="button" onClick="parent.location='view.php'" value="Back">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
require_once('../../authenticate.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// do some funky stuff
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checking that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$query = "SELECT * FROM STAFF WHERE StaffID=$id";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$result->close();
// check that the 'id' matches up with a row in the database
if($row)
{
// get data
$StaffFullName = $row['StaffFullName'];
$StaffJobPosition = $row['StaffJobPosition'];
// show form
renderForm($id, $StaffFullName, $StaffJobPosition, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
So, what happens here is this:
When you open the page like edit.php?id=1, it fetches the data of the associated record from STAFF table and shows them on page for the user to edit them.
This part of the code works fine.
I also want the user to be able to select "Job Position" possible values from a drop down box. The drop down box should get its data from another table in database, LUT_JOBPOS.
This is the part of the code that doesn't work.
I was using mysql_query commands before on this page and it worked perfectly. However I was told to switch on mysqli_query instead.
Since I did the conversion I can't find how to run these two queries on the same script.
I messed a little bit with the require_once command and depending on where I call it I can run one query or another, but never both of them.
Looking at the logs of my web host the only thing I can see that may be relevant to my issue is:
"mod_fcgid: stderr: PHP Notice: Undefined variable: connection in /var/www/vhosts/myhostdomain.com/httpdocs/prod15/admin/staff/edit.php on line 24"
The connection variable comes from authenticate.php and it holds the connection parameters to the database. I'm sure it's set otherwise the first query (that gets the user data) wouldn't work.
I read somewhere that you can't run two sqli queries on the same script.
Then how I'm supposed to use a LUT table (lookup table)?
PS: I know that for showing the data I can use a UNION and that's what I do.
But when I edit the data I want the user to be able to select only from the possible values that exist on the LUT table (drop down select box)
Any help?
You have a lot of issues in your code. You really need to review it before use it in some real application, but for your specific problem, here is my guess.
You are calling the line $result = mysqli_query($connection, $query); in the line 24 and only after taht you call require_once('../../authenticate.php');.
As you said, the $connection var is defined in the authenticate.php, so in the line 24 is undefined.
Try to use require in the first line of your php script.

$_POST not working in PHP

<html>
<head><title>HEllo</title></head>
<body>
<input type="text" name="id">
<input type="text" name="name">
<input type="text" name="address">
<input type ="submit" name = "s" value = "Employee">
<?php
$link =mysql_connect('localhost','root') or die("Failed");
mysql_select_db("gagan",$link) or die("database not exists");
if($_POST['s']=="Employee")
{
print "g";
$id = mysql_real_escape_string($_POST['id']);
$name = $_POST['name'];
$address = $_POST['address'];
print "hi";
$update = "update emp set name = $name, address=$address where id = $id";
$result = mysql_query($update,$link);
print "Hello";
if($result)
{
print "Updated";
}
else{
print "$update";
}
}
?>
</body>
</html>
When i run this code it produce an notice and the above code is not working.
Notice: Undefined index: s in C:\wamp\www\1.php on line 12
What's the problem in my code can anybody tell me?
You forgot the form tag.
<form action="yourform.php" method="POST">
You need to ensure that array member is set first. Try using the result of isset($_POST['s']) to ensure it is set before trying to access it.
You need the form tage with the method set to post.
ie
The main problem (in addition to the missing form tag) is that the program flow continues to the part that tries to save the data even when the form hasn't been submitted yet. You must check that the form has been submitted before trying to save the data, or even easier would be if you moved the data saving part to its own script.
You also have an invalid SQL query but that's another matter :)

Categories