PHP to display text input content - php

Im wanting to display the content that a user types into a text box lower down on the page once they've clicked a submit button.
I'm new to PHP and so far i've got this:
<?php if(isset($_POST['submit'])) {echo 'You entered: ', ($_POST['name']);}?>
Any ideas as to what i'm doing wrong?

Try this:
<form action="" method="post">
<input type="text" name="name" />
<input type="submit" name="submit" />
</form>
<?php if(isset($_POST['submit'])) {echo 'You entered: '.$_POST['name'];}?>

Related

How to redirect to a specific URL baed on the data submitted?

I am trying to create a form which, when submitted, redirects the user to a specific URL which contains the content of the submission at the end of the URL.
So, for example, a simple form like this:
<form method="post">
<input type="text" name="tracking">
<input type="submit" name="submit" value="submit">
</form>
When the user types "abc" as the tracking number and clicks 'submit' they would be redirected to:
https://www.specificurl.com/abc
My question is, is this possible and if so, how can it be done?
This is what I have so far...
On the form page:
<form action="redirect_form.php" id="#form" method="post" name="#form">
<label>Enter your tracking code:</label>
<input id="tracking" name="tracking" placeholder='Enter your tracking code' type='text'>
<input id='btn' name="submit" type='submit' value='Submit'>
<?php include "include/redirect.php"; ?>
</form>
included in the redirect.php file:
<?php
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$name = $_POST['tracking'];
if($tracking)
{
// To redirect form on a particular page
header("Location:https://specificurl.com/$tracking");
}
else{
?><span><?php echo "Please enter tracking number.";?></span> <?php
}
}
?>
Probably JavaScript will be enough here:
<input type="text" id="tracking" name="tracking">
<input type="button" name="submit" value="submit" onclick="window.location.replace('https://www.specificurl.com/'+tracking.value);">
you should use the GET method for this.
With javascript you can then extract the text input and manipulate the url to which the user is redirected to.
Hope that helps you further.

Retrieve $_POST for two pages

I need to save my submit form data like:Name for two pages...
For some reason the $_POST only saves data for the "action" page, but cannot be retrived after the action page.
Here's my code:
HTML (form):
<html>
<body> <form name="input" action="staff.php" method="post">
Username: <input type="text" name="Name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the next page after submiting and it works... (staff.php)
<html>
<?php
session_start();
echo "You have choosen". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="submit" value="Submit">
</form>
</html>
Ok and after age submiting Name and Age stop working... (staff2.php)
Here's the code:
<?php
session_start();
echo "You have choosen".
$_POST['Name']; //it does't show Name.. Please help!
$_POST['Age']; // it doesnt't show this either..
?>
Obviously, there is nothing wrong on the first page. So don't change anything.
The second page. The post works. Then add a hidden input to preserve it and carry it on the next one:
<?php
echo "You have chosen: ". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="hidden" name="Name" value="<?php echo $_POST['Name']; ?>" /> <!-- this one -->
<input type="submit" value="Submit">
</form>
On the third and final page. Properly concatenate the variables:
echo 'You have chosen: <br/>';
echo $_POST['Name'] . '<br/>'; // this should carry the hidden input you set on the last page
echo $_POST['Age'];
//^^ you forgot the echo
as you have a session running pass them as session variables.
$_SESSION['name'] = $_POST['name'];

PHP - My form page freezes after submitting form

Here's the thing. When I submit the form, it works every time UNLESS my Title field is blank.
My HTML on page:
Title: <input type="text" name="title"><br />
Description:<br />
<textarea name="desc"></textarea><br /><br />
Location: <input type="text" name="where"><br />
<input type="submit" value="Submit">
My PHP having to do with the title input, on the other page:
if(empty($_POST["title"]) or !ctype_alpha(str_ireplace(" ","",$_POST["title"]))){
$ge=True;
$et=True;
}
The above if statement just checks to see if the title entered contains letters and spaces ONLY, and to make sure it isn't blank.
It's only when that one text field is blank, and nothing else.
I'm new to PHP so any help would be awesome.
Thanks for your time!
- Eric
EDIT
Sorry, should have been more descriptive. The page acts as if it's trying to submit the form (the spinning loading icon thing is there), but it never makes it to the process page. The form page just becomes unresponsive.
You can try this,
<html>
<body>
<form method="POST">
Title: <input type="text" name="title"><br />
Description:<br />
<textarea name="desc"></textarea><br /><br />
Location: <input type="text" name="where"><br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
if(empty($_POST["title"]) or !ctype_alpha(str_ireplace(" ","",$_POST["title"])))
{
$ge=True;
$et=True;
echo 'empty title';
exit();
}
echo 'form submitted';
}
?>

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

Is it possible to include a form in a wordpress page?

I have the following code in a php template called contact_us. I have created a new page which uses this template, but when you click submit it doesn't post back to the same page and display what the user entered in the form. Any ideas why this doesn't work?
Thanks,
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST["name"];
$comments = $_POST["comments"];
echo $name;
echo $comments;
}
?>
<form action="<?php echo $PHP_SELF;?>" method="post" >
Name : <br/>
<input type="text" name="name" /><br/>
Comment <br/>
<textarea name="comments" name="comments"></textarea>
<br/><br/>
<input name="submit" type="submit" id="submit" value="Send" />
</form>
Make sure that you don't use "name" as a variable name. I will assume that the same thing goes for comments.
More information here
http://wpquicktips.wordpress.com/2010/02/17/use-an-empty-action-attribute-in-forms/
Does it make any difference removing <?php echo $PHP_SELF;?> from the action and leaving it blank?
I managed to get this to work by adding an id attribute to the form. I think this is something wordpress requires.

Categories