Not able to post PHP variable to another page - php

I have a date variable set in my page and I want to post it to another page.
Here's the PHP code in the first page:
<?php if(isset($date)) { echo $date; ?>
<form action="new.php" method="POST">
<input type="hidden" name="date" id="date" value="<?php echo htmlentities ($date); ?>">
</form>
<?php } ?>
$date is echoed when the date is set in the request so there is no issue with that.
In new.php
$mydate = $_GET['date'];
But when I dump the data in new.php all I get is NULL.
I want to do this specifically with hidden form inputs or any other solution that does not involve using sessions.
This seems so fundamental what am I doing wrong? I've checked my console and there are no errors over there.
UPDATE
<?php if(isset($date)) { echo $date; ?>
<form name="dateform" id="dateform" action="news_detail.php" method="POST">
<input type="hidden" name="date" id="date" value="<?php echo htmlentities ($date); ?>">
</form>
In my script:
document.dateform.submit();

You need to make use of POST , since your <form> tag has the action method POST
$mydate = $_POST['date'];
Simple Illustration
<?php
#extract($_POST);
if(isset($submit)) {
echo $_POST['date'];
}
?>
<form action="" method="POST">
<input type="hidden" name="date" id="date" value="23-4-2025">
<input type='submit' name='submit'>
</form>

$_GET is for GET data.
$_POST is for POST data.
$_COOKIE is for COOKIE data.
$_REQUEST is for ^ALL^ the data.

Must have some value in $date to enter into your form section
page1.php
$date ="some date";
<?php if(isset($date)) { echo $date; ?>
<form action="new.php" method="POST">
<input type="hidden" name="date" id="date" value="<?php echo htmlentities ($date); ?>">
</form>
<?php } ?>
Now new.php, On page1.php submit
echo $mydate = $_POST['date'];

you are passing varible using POST method from first page and second page you are retriving it ussing $_POST[]...
chage second page like this
$mydata=$_POST['data'] will work
and use submit button to send data to the new.php
<input type="submit" name="submit" value="Send"/>

since ur using post method.correct fetching of the data as the same method i.e. post.and if u r not sure which method u r using u can use $_REQUEST as well.but the later has some security issues.
<?php if(isset($date)) { echo $date; ?>
<form action="new.php" name="myform" method="POST">
<input type="hidden" name="date" id="date" value="<?php echo htmlentities ($date); ?>">
<input type="submit" value="submit">
</form>
<?php } ?>
than in new.php use the following code to fetch the data
$mydate = $_POST['date'];
with javascript(no need to use submit button):
Send data without submit button (Javascript / jQuery)

Related

retrieve users input with PHP and echo it

I was wondering if it was possible to take HTML user input using PHP (preferably the ID or something I can use numbers in) and to save confusion just echo it back.
So I have some example code here:
<input type="number" maxlength="3" name="test" id="1">
<input type="number" maxlength="3" name="test" id="2">
<input type="number" maxlength="3" name="test" id="2">
What I was looking for is a way where I could use their input and well.... echo it back for now.
if you already know how to submit a form you can use php on the other side like this to echo it out
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
<?php
if (isset($_POST['name'])){
$userinput = $_POST['name'];
echo $userinput;}
?>
in your example all three inputs are named "test" so youll need a different name for each one. My example above uses the "name" of the input to capture it. If your using "GET" change my $_POST['name'] to $_GET['name']
Which method did you use for these inputs? Name them differently, then retrieve the data with:
<?php echo $_GET['test1']; ?>
<?php echo $_GET['test2']; ?>
<?php echo $_GET['test3']; ?>
If you used POST type in the input method, then switch for:
<?php echo $_POST['test1']; ?>
<?php echo $_POST['test2']; ?>
<?php echo $_post['test3']; ?>

Why variable does not changed after assigning new value?

I have this code which changes the $date variable after submiting the form.
<?php
$date=date("m/d/Y");
?>
<form action="" method="post">
<input type='text' value=<?php echo $date ?> name='day1'/>
<button type='submit' name='btnFilter'>Filter</button>
</form>
<?php
if(isset($_POST['btnFilter'])){
$date=$_POST['day1'];
}
echo $date;
?>
What I wanted to happen is default value of #day1 to be today. And when user change it and submit, page must echo the new date and value in the #day1 must be new user input. But in my code when user submit the form, value of #day1 become today(default value). What do I have to change?
Try using this :
<?php
$date = date("m/d/Y");
if(isset($_POST['btnFilter'])){
$date = $_POST['day1'];
}
?>
<form action="" method="post">
<input type="text" value="<?php echo $date ?>" id="day1" name="day1"/>
<input type="submit" id='btnFilter' name="btnFilter">Filter</button>
</form>
You are using id.. that not carry a value with $_POST. Assign name to your input tags.
Add name to your submit button.
Enclose the value for text field with quotes.
Give name to your text field. then it will work.
<form action="" method="post">
<input type="text" name="day1" value="<?php echo $date ?>" id="day1"/>
<button type="submit" name="btnFilter" id='btnFilter'>Filter</button>
</form>
Note : What ever you access in $_POST[''] should be the name attribute not id attribute of form elements.
I don't know why you are checked isset of btnFilter. Try check for day1 element:
<?php
$date = date("m/d/Y");
?>
<form action="" method="post">
<input type='text' value=<?php echo $date ?> name='day1'/>
<button type='submit' name='btnFilter'>Filter</button>
</form>
<?php
if(isset($_POST['day1'])){
$date = $_POST['day1'];
}
echo $date;
?>

How to call php variable input text from another Php page

My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];

How to remember text input in PHP Forms?

i an new to php . please help me how to remember text input in PHP forms after go back to form ?
i have an rgister page and want to save text input in database , but if a text input existing in database go back to register page but remember text input .
please help me !
Save the data in your session.
Call
session_start();
in the form.php and your target.php. In target.php parse all your $_POST/$_GET Parameters and store them in the session.
$_SESSION['user_name'] = check_for_valid_name($_POST['user_name']);
in form.php just set input default to your session Variable:
<input name='user_name' value='<?= $_SESSION['user_name'] ?>' />
edit: In form.php you can also use:
value='<?php isset($_SESSION['user_name'])?(echo $_SESSION['user_name']):(echo "") ?>'
to get rid of warnings/notices of uknown index/variable.
Use PHP session
<?php
session_start();
$_SESSION['name'] = "YourSession";
// ...
?>
You can store the details in a session variable. You'll have to learn working with sessions though.
define your input field something like this.this will retain values after submission
<input id="user_firstname" type="text" name="user_firstname" value=<?= $_POST['user_firstmane'] ?> >
<form action="action.php" method="post">
<input type="text" name="inmputname" value="<?php echo $_POST['inmputname']?>">
<input type="submit">
</form>
Hope it will help you
Just try this one,
<?php
$name = NULL;
if(isset($_POST['submit'])){
$name=$_POST['inputname'];
}
?>
And in your form,
<form action="action.php" method="post">
<input type="text" name="inputname" value="<?php echo $name;?>">
<input type="submit">
</form>

How to not clear input fields in php form?

I have a webpage that uses php and has a bunch of input fields in which a user enters some data and I turn the input into an SQL statement to query some database. I successfully parse the input fields and put the SQL statement together but when they click the "submit" button, all the input fields get cleared. How can I make it so these input fields don't get erase every time the user clicks "submit"?
Store them in local variables like
<?php
$name = '';
$last = '';
if(isset($_POST['post_me'])) {
$name = $_POST['post_me'];
$last = $_POST['last'];
//Process further
}
?>
<form method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="last" value="<?php echo $last; ?>" />
<input type="submit" name="post_me" />
</form>
Something like this should work.
<?
if(!isset($_POST['stackoverflow'])){
$txt = "";
} else {
$txt = $_POST['stackoverflow'];
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="" method="post">
<input type="text" name="stackoverflow" value="<?= $txt ?>">
</form>
</body>
</html>
You need to check if the POST values (assuming you're using POST) are already set when the page loads (because they have already been submitted). If they are set, echo their contents into the value attributes of the form fields, or set checked=checked etc. depending on the field types.
This has always worked for me:
<input type="text" name="somename" value="<?php echo htmlspecialchars($_POST['somename']); ?>">
The key is to store the post values in session and display it using value tag.
Example:
HTML:
<form action="index.php" method="post">
<input type="text" name="name" placeholder="First Name"
value="<?php
if (isset($_SESSION['name'])) {
echo $_SESSION['name'];
}?>" >
<input type="submit" name="submit">
</form>
PHP:
session_start();
if (isset($_POST['submit'])) {
$name=$_POST['name']);
$_SESSION['name']=$name;
}
<?php
if(isset($_POST['submit'])) {
$decimal = $_POST['decimal'];
?>
<form action="" method="post">
<input type="number" name="decimal" required value="<?php echo (isset($decimal)) ? $decimal: ''?>">
</form>
Just use your $_POST['decimal'] into your input value
Have you used sessions and Cookies in PHP??
You can store the values in session on form submit before updating in Database for a user, then check if there is a session for user and value is set for field output the value else output null.
You just have to make sure the HTML input value has a php echo statement of the appropriate php variable for that field.
Simplest answer
`<form method="post" onsubmit="return false">`
coding your input with PHP is dangerous even if with htmlentities - hope this helps

Categories