I have a page that post data to another page once a form has been submitted. I am guessing server. Please correct me if I am wrong.
<form action="http://192.168.1.118/collegeShuttle/test/welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
It all great that I can do this on my second page and expect get the data back
<html>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</html>
My biggest problem is that, when I navigate to that page from a different device or computer. I get error messages saying that those variables are undefined. My question is that if it claims to be posting to the server why cant I access it from different computers? If that is not the case, is there a way to send data to the server that I can access from any computer?
the $_POST data is being sent to http://192.168.1.118/collegeShuttle/test/welcome.php, so are you getting the data in that page?
Post data has a resquest scope, if you want to persist the data on server probably wanna use a database.
Before you learn php you need to study the at less some of "http"
because your second device does not have same value in cookie named phpsessid. If you want to work it that way submit the form from one device . Note the phpsessid cookie value , on second device create cookie with name phpsesid and put value you copied before . Now open the second page.
try this :
<html>
Welcome<?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.
</html>
The $_POST variable is defined whenever someone sends POST data to your page (usually via form, like you have listed above). The variable is only defined when POST data has been submitted to the page and will only be valid for that request (from which the data was sent). If you open a different window resending the POST data, then it is lost.
Related
I'm trying to create a cookie for a web page. The cookie value will vary based on the users name. Does PHP have an input type function? I just want to add an input field to the page an then the PHP will use that to define the users name for the page. I have the create cookie code, just can't figure out how to get the name from the screen and insert it to the cookie code. Appreciate any suggestions. This is on a WP website.
Not natively because php does not execute in browser, it executes on your server, but it can be used to write an HTML input.
The syntax would look something like this:
echo '<input type="text" name="myinput">';
or
?>
<input type="text" name="myinput">
<?php
You would then use a form post, CURL, or AJAX function to send the data back to the server where a second PHP script would process the input.
That said, it would help to post your create cookie code, since you may not even need to send it back to the server, but just handle it all in the browser using Javascript in which case your submit button only needs to pass the input to a Javascript function instead of posting it.
Is this something you are looking for?
Here it just takes the value user input from the browser and set it as a cookie
<?php
if(isset($_POST['name']) && !empty($_POST['name'])){
setcookie('setcookie_name',$_POST['name']); // setting cookie
}
?>
<form action="" method="post">
<input name="name" value="" placeholder="Enter your name" />
<input name="submit" type="submit" value="Submit"/>
</form>
How can I avoid the the browser form-resubmission alert?
This question seems to have been discussed a lot here on SO, for example:
Preventing form resubmission
Prevent Back button from showing POST confirmation alert
Never ever respond with a body to a POST-request
What I do not get from the previous discussion, is how I can use the posted data and incorporate it into the html. The previous links discuss how to use the php header function to send a get request to itself. But when sending this get request, the posted data will no longer be available to the new page, (since we cannot use the php post method..)
I would like to do this without using the php or javascript session storage technique (or saving the posted data to a temporary mySQL database).
For a simple example:
<html>
<body>
<form action="post.php" method="post">
User name: <input type="text" name="user"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
where post.php is:
<html>
<body>
<?php
echo "<p>".$_POST['user']."</p>";
?>
</body>
</html>
Pressing CTRL-R in google chrome on the second page brings up the alert.
Do a redirect from post.php. Save data in session or in database and retrieve from redirect page.
Example Scenario:
Submit the form
Save the user record to db, get the id of the new record e.g. in $id
redirect using header, something like:
header('Location: result.php?user_id='.$id);
get the user record from db, with the provided id and show it to the
user.
Use this:
<script>
if(window.history.replaceState)
{
window.history.replaceState(null,null,window.location.href);
}
</script>
you may rewrite the browser history object
history.replaceState("", "", "/the/result/page");
See this
I'm having a bit of trouble with this one, as basic as it may be. I have a simple form with name, email, comments, etc. that outputs itself to one php page, but I want to have a link that sends it to a second page, for example:
<label for="name">Name:</label><input type="text" name="name" size="20" />
Goes to a second page (second.php) with this code and prints it just fine:
print "<div>Thank you, $name.</p></div>";
But if I try to send $name to a third page (third.php) using similar code it shows up like this:
Thank you, $name.
With the actual variable and not what was stored in $name.
I feel like I'm missing one tiny little thing but I'm not sure what it is. I used this:
$name = $_POST['name'];
To bring it to second.php and this to bring it to third.php:
print 'Click here to proceed.';
Just to see if it would get the same information from second.php, but I don't think it works that way. Is there something else I should be doing on the third page? I have a feeling it's something incredibly insignificant but as I'm learning, I just can't quite get a grasp on it.
You can do it this way.
when you declare
$name = $_POST['name'];
you can use in a header to pass this variable
if(isset($_POST['btnname']))
{
header('Location: second.php?name='.$name);
}
The in your second php
you can get it by this way
Thank you, <php echo $_GET['name']; ?>
Or if you want it to be available to all pages use session
$_SESSION['name'] = $_POST['name'];
=D
you can try it using a hidden input
<label for="name">Name:</label>
<input type="hidden" name="name" value="<? echo $name; ?>" size="20" />
Websites are stateless, which means that the variables only last for a few seconds on the server, then the html is rendered, and sent to the clients browser. The server memory is then freed up to serve other clients.
You have a few options:
1) Using a hidden form field and printing (with php) their name to the hidden form field so when they post again it gets saved (if they post again).
2) Sessions
3) cookies
4) Print it out in the url (i.e. page.php?name=".$name; )
It all depends on how you get to your third page (From a link? A form? A php redirect?)
You can store the name in the second page and again set the $_GET variable before the third page is called. This is because the variables are only valid for that particular request and they need to be set again when another request is made.
I have a very simple app, it's only one php page (page A). I would like add one more php page (page B) that receives data from an html form of "page A". I haven't found any tutorials about it. Can you help me?
GET METHOD
Page A: (eg. index.html)
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Page B (welcome php)
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
When to use method="get"?
When using method="get" in HTML forms, all variable names and values are displayed in the URL.
Note: This method should not be used when sending passwords or other sensitive information!
However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
Note: The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.
POST METHOD
Page A: (eg. index.html)
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Page B (welcome php)
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
When to use method="post"?
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
The PHP $_REQUEST Function
The PHP built-in $_REQUEST function contains the contents of both $_GET, $_POST, and $_COOKIE.
The $_REQUEST function can be used to collect form data sent with both the GET and POST methods.
Example
Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.
If you are using Form on Page A then you can post the values from that form to another page B.
if you are not using post or get you can still pass values from one page to another by creating session.
Html form can have action set to Page B and $_POST or $_GET can give you data from page A to Page B
You are best off looking into a few tutorials this is PHP 101.
You can store the data in a session or cookie.
You can use a form to POST data to the next page.
you can use GET to send data in the uri
Here is a tutorial for POST and GET
Edit : It sounds like you wish to pass the variables with a redirect. You could set the variables in session and retrieve them later or pass the variables in the redirect and fetch them using $_GET. Rather than redirecting to example.php redirect to example.php?var=value.
I am sending login status = fail, back to my login page.Here is my code-
header("location:index.php?login=fail");
but that is sending through URL like-
http://localhost/303/index.php?login=fail
is there any way to pass value without showing in URL? And how to get this value on the second page?
You are passing that value via a GET request, which is why it appears in the URL. In order to pass a value without showing it in the URL, you want to pass it via a POST request.
In order to do this you aren't going to want to "return" the value to your login page. Instead, whatever php form is handling the process of logging in the user after they click the "login" button, will decide what to show the user.
In PHP post variables can be accessed by the global $_POST object -
$_POST['username'];
Would get the value with the name "username" that you passed via POST:
<form method="post" action="checkLogin.php">
Username:
<input type="text" name="username" maxlength="25" />
Password:
</td><td><input type="password" name="password" />
<input type="submit" name="submit" value="Login">
</form>
In order to dynamically save and show errors to the user, you can store them in the session, for example have a file called "errors.php"
<?php
if (isset($_SESSION['errors']))
{
echo $_SESSION['errors'];
}
unset($_SESSION['errors'])
?>
And in your php that checks the login, do:
session_start();
$_SESSION['errors'] = "Invalid username or password.";
Then redirect to your login page (don't pass any variables) and on your form always have this field:
<?php include("errors.php"); ?>
If you didn't have any errors, it won't show anything and the login page will look normal.
Note: In any php form that you use a session_start(), it HAS TO BE THE FIRST THING in the form.
Other ways are to use session or hidden fields but you what you are doing is fine for the purpose. You can later retrieve the value like this:
if ($_GET['login'] === 'fail')
{
// failed.......
}
there are several ways to accomplish your task
Modern AJAX way. Form being sent using AJAX. No page reload until password is correct. Errors shown in place. Requres javascript.
Post/Redirect/Get pattern. Form being sent using regular POST. No redirect on errors, shown in place.
sessions, when we store an error in the session