retrieve users input with PHP and echo it - php

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']; ?>

Related

get value by id link with the href

this is my input
<input type="text" name="quantity" id="quantity" value="1" />
this is my href link
<a href="UserOrderProduct.php?id=<?php echo $Pid; ?>
&price=<?php echo $Price; ?>&quantity=">add</a>
how to write it in the href and please ignore the
<?php echo $Pid; ?>&price=<?php echo $Price; ?>
this is because i get the id already from the database but only the quantity not really how to get it, can teach me?
If you have a form where you get the "quantity" value as it looks from your code:
<input type="text" name="quantity" id="quantity" value="1" />
if you use the "GET" method
<FORM method='get' action='next_script.php'>
all the values will be in the address, otherwise, if you POST the values you'll get them in the next script via $_POST array.
If you need to pass one or more couples variable=value between different scripts the best solution is to use PHP sessions.
You just need to add at the beginning of each script:
session_start();
and put your_variable and your_value as:
$_SESSION['your_variable'] = your_value;
or
$_SESSION['session_variable'] = $your_variable;
In such a way data are not visible on the link and you can access them anywhere you like provided that you keep the session_start() command at the beginning of each php page.
If you have html...
<form method="post" action="phpfile.php">
<input type="text" name="quantity" id="quantity" value="1" />
<input type="submit" value="send">
</form>
...in your phpfile.php you can retrieve the value of quantity like this:
<?php
$qty = $_POST['quantity'];
?>
<a href="UserOrderProduct.php?id=<?php echo $Pid; ?>
&price=<?php echo $Price; ?>&quantity=<?php echo $qty;?>">add</a>

Possible errors in session

I just want to ask what are the possible errors in SESSION... Because I've been suffering from my bugs! My codes are right but I don't know why it has happened that when I click the submit button it's supposed to pass the value that i declare but it always declare the last value I declared(it means I can't renew the value! once I declared my value that is permanent which is wrong because every time you click the submit it suppose to give new variable)
home.php
<form method="post" action="1home.php">
<label id="checkinD">
<h3>Day</h3>
<Input id="chiD" name="chiD" type="number" min="<?php echo $_SESSION["day_today"]; ?>" max="<?php echo $_SESSION["day_count"]; ?>" required />
</label>
</form>
$chiD = $_POST['chiD'];
$_SESSION["chiD"] = "$chiD";
1home.php
<form method="post" action="2home.php" onsubmit="return validate()">
<label id="checkinD">
<h3>Day</h3>
<Input id="chiD" name="chiD" type="text" value = " <?php echo $_SESSION["chiD"]; ?>" readonly />
</label>
</form>
BTW There is also a crazy one that occurs on my codes it's working very smoothly without logical errors but every 4 hours my codes will have logical errors without my fault!!! it's like automated bugs appeared every hour.
and sometimes to make it work I need to erase the name of my form then replace it again and typed the word that I erased. What kind of shit is this?
PHP, you need to session_start() before assigning values to your
session variables. The date_today variable holds the present datetime, and date_count variable holds a random number.
Though i can not see your full code but here's the working solution.
home.php
<?php
session_start();
$_SESSION["day_today"] = date("Y-m-d H:i:s");
$_SESSION["day_count"] = rand();
?>
<form method="post" action="1home.php">
<label id="checkinD"><h3>Day</h3></label>
<Input id="chiD" name="chiD" type="number" min="<?php echo $_SESSION["day_today"]; ?>" max="<?php echo $_SESSION["day_count"]; ?>" required />
<input type="submit" value="FORM 2" name="btn_form2" >
</form>
home1.php
<?php
session_start();
if(isset($_POST['chiD'])):
$chiD = $_POST['chiD'];
$_SESSION["chiD"] = $chiD;
?>
<form method="post" action="2home.php" onsubmit="return validate()">
<label id="checkinD"><h3>Day</h3></label>
<input id="chiD" name="chiD" type="text" value = "<?php echo $_SESSION["chiD"]; ?>" readonly />
</form>
<?php echo "Day: ". $_SESSION['day_today']; ?>
<br>
<?php echo "Day Count: ". $_SESSION['day_count']; ?>
<?php else: ?>
<h4> Sorry! Somethinh went wrong. </h4>
<?php endif; ?>
Here's a screenshot of the result
Hope this helps

Not able to post PHP variable to another page

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)

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 do I receive post from a form where I name an input as <?php echo $var ?>?

I tried $_POST['<?php echo $var ?>] but I should have known that it wouldn't be that easy.
The reason why I try to do is because I have several input boxes with values I take from a database and I'm trying to create an updation script where any of the input box values can be changed.
for example
<form action="process.php" method="post">
<?php
while($variable=mysql_fetch_array($sqlconnec))
{
?>
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />
<?php
}
?>
<input type="submit" />
</form>
Any help is appreciated.
I think that what you need is:
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
$_POST[$col] //this will have the input value defined above.
In process.php you have to do the same query as mentioned above. If you iterate through those results $_POST[$col] will contain the posted values.
You need to do like this:
<form action="process.php" method="post">
<?php
$variable = mysql_fetch_assoc($sqlconnec);
foreach($variable as $col => $val)
{
?>
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
<?php
}
?>
<input type="submit" />
</form>
Now, mysql_fetch_assoc gets you the database row in a associative array. Then, the code iterates each column in the row and displays the name/value pair for it. And yes, you were not closing the value tag correctly.
foreach($_POST as $k=>$v) {
//do something with $v or $_POST[$k]
}
I think that you want to change the name of the input to something that is constant.
For example:
<input type="text" name="testname" value="<?php echo $variable['val'] ? />
And then retrieve your variable like so:
$_POST['testname']
For example you could print the variable you sent in the input to test it like so:
echo $_POST['testname'];
You are not closing your input 'value' tag with ". Also your second php closing tag is incorrect.
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />

Categories