How do I get rid of this notification box in PHP? - php

I have this problem where when I refresh, a notification box will pop up in the browser(Chrome) just like this.
my code for the sign in is here:
<form class="uk-form">
<fieldset data-uk-margin>
<legend>Sign up here:</legend>
<input type="text" placeholder="username" required>
<input type="password" placeholder="password" required>
<a href="" class="uk-form-password-toggle" data-uk-form-password></a>
<select>
<option>PYP</option>
<option>MYP</option>
<option>DP</option>
<option>Adults/Teachers</option>
</select>
<div><input type="checkbox" name="remember" id="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?> />
<label for="remember-me">Remember me</label>
</div>
<button type="confirm" class="uk-button uk-button-primary">Sign Up</button>
</fieldset>
</form>
and the result is this:

This is occurring because you previously POSTed to that page, meaning that you've most likely come from a form. If you refresh the page it means the POST data will be posted again to that page which is probably not what you want to do.
My advice would be once you've done what you need to do with the form, is to redirect it to another page (could in theory be back to the form page) and repopulate the data if you need to keep it.

After processing the $_POST data on the previous request you can redirect the user, and clear the Post data by doing this:
header('Location: '.$url);
exit;
Where $url is set to the URL you want to send them to. There are 2 very important things if you do this.
You cannot output anything before calling header, not even 1 single space.
Coincidentally this is why you will see some PHP files that do not include a ?> closing tag. The closing tag is optional if the file only contains PHP, and even a space after the end tag can mess up redirect if it was included before calling them. So by omitting the end tags, there can be no output.
Conciser this:
<?php
...code..
?>
\n
Where \n is a new line. If this is included before doing a header or even a download it can have undesirable effects. Personally I never mix PHP and HTML and so I never use the end tags.
Likewise this can prevent the redirect from working
\n
<?php
... code ...
You call exit immediately following the redirect. After doing the redirect PHP will continue to execute the current script, which is probably not what you want and can have unpredictable results.

It's happend because you come from another form that send POST. When you refresh, it will confirm you that data have been posted before will posted again.
I advice you redirect to another page or same page. Read header in php to redirect

Related

Cab PHP create an Input field?

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>

Redirect after posting a HTML form

Is there anyway to redirect the user after posting a HTML form?
I have a form which looks like this
<form action="/URL/" method="post">
<input type="radio" name="answer" value="W" />W<br />
<input type="radio" name="answer" value="X" />X<br />
<input type="radio" name="answer" value="Y" />Y<br />
<input type="radio" name="answer" value="Z" />Z<br />
<input type="submit" />
</form>
Is there anyway I can redirect the user to a new page after they click the submit button? I found a bit of javascript however that seemed to stop the post from working :S
Cheers!
If you're using PHP, do it on the server:
if( isset( $_POST['answer'])) {
header( 'Location: http://www.example.com/newurl');
exit();
}
Note that you should be using a full URL in the header() call, and that you likely want to exit() after calling header() in this case.
You can use a simple php header('yourlocation.php'); to move them away from the form.
The PHP that your form action pointed to will still continue executing even if your header() is at the top of the php file.
The header redirect will take care of the user hitting refresh - it has already moved them past the point of sending form data, so even if they refresh, it will just open the page again sans any form submissions.
An example is as fololows:
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
// Process Form Data, insert to database, whatever you want... then
header('Location: http://www.example.com/');
?>
The user then ends up at wwww.example.com and they can spam refresh as much as they like, but all the processing has already taken place. It won't resend the information (or process their payment again or anything else).
Yes, use a redirect header:
header("Location: http://new.location.com/w00t");
This must be called before any output is sent.
Take note that everything after the header call would still be executed! So you'll need die() or exit() afterwards to prevent unexpected results.
You can just simply specify URL to which form will go after being submitted.
<form action="http://localhost/redirected.php" method="post">
You can check with PHP whether form is submitted and then redirect:
if (isset($_POST['answer']))
header('Location: redirect.php');
And You can achieve this with javascript/jquery.
$('#submit_button').click(function () {
// do everything with variables
window.location = 'redirect.php';
});
Yep, just post the data using ajax and then use window.location = 'http://xxxx.com' to redirect user to location of your choice.
That's assuming you want to redirect user without using server-side location headers (like the other ones posted).

HTML form action goes to wrong place, doesn't send variables to intended file

so I've been fighting with this for a few days, and I just can't seem to make it work. Whenever I press the submit button, the browser should send the post variables to write.php, but instead, it just redirects back to the website homepage, or the Document Root. This should be really, really simple, and I've done it before, but now it doesn't work for me. What I want to know is if this is a problem with my webserver setup, or PHP, or just a stupid mistake on my part. It's just a simple HTML form, not really special, so here's the form itself, in index.php:
<p style="font-size:13px">
<?php
$rp = fopen('mainlog.txt', 'r');
while(!feof($rp))
{
$read = fgets($rp);
echo($read).('<br/>');
}
fclose($rp);
?>
</p>
<form action="write.php" method="post">
Name: <input type="text" name="user" /><br/>
Changes:<br/>
<textarea cols="70" rows="8" name="change" style="background-color:#555;color:#ccc;font-family:verdana,arial,helvetica,sans-serif;font-size:13px"></textarea><br/>
<input type="submit" value="Add Entry"/>
</form>
And here's where it send to, in write.php:
<?php
$fp = fopen('mainlog.txt', 'a');
$wr1 = $_POST['change'];
#$my_t = getdate(date("g"));
date_default_timezone_set("America/New_York");
$date = date("n").('/').date("d").('/').date("Y").(', ').date("g").(':').date("i").(':').date("s");
$who = $_POST['user'];
$write = $date.(' by ').$who.('
').$wr1.('
');
fwrite($fp, $write);
fclose($fp);
header('Location: http://www.zennixstudios.com/first/chlog/');
?>
I have tried this both on my Apache 2.2 dedicated server with PHP 5 on FreeBSD8.2, and on XAMPP for Windows, with the same results. I have a suspicion that it may have something to do with PHP, specifically PHP include(), because I have several of those on this page, and when I put this on a friend's computer with XAMPP, but without the included files, the include()s just put errors on the screen, but the HTML form suddenly works fine. So, are there any known conflicts with HTML forms and certain PHP functions?
Other Notes:
The code shown above for index.php is within the main page div, but if you want the whole page source just ask.
I'm pretty sure the error isn't in write.php, because I KNOW that the browser never sends anything to it, because it would at least put the date in mainlog.txt.
If you want to see what this looks like in context, go to http://www.zennixstudios.com/first/chlog/
Thanks,
Chaos
Here is your problem:
<table align="right"><tr><td align="right"><form action="/" method="post">Username: <input action="login.php" type="text" name="uname"/><br/>Password: <input type="password" name="passwd"/><br/><input type="submit" value="Login" align="right"/></td></tr></table>
You never closed the form up in your header for the username and password, so your <form action="/" method="post"> is being used for pretty much the entire page and your write.php form action is being ignored because a form is already, technically, open. You'll need to close the form in your header for the rest of your page to work properly.
To reiterate: nothing is being redirected, you're actually posting all the data from both 'forms' to the location / as specified.

Get POST response from a url and print response to page?

I'm trying to get a POST response from a url and I can not get the response to print to my html page instead it just redirects me to the url in the action with the response.
Is there a way to grab the response with html? php?
Code of html page i'm using
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action="http://poster.decaptcher.com/"
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
</head><body></body></html>
Note: The url in the action will only show the response and nothing else is shown on the page.
Let's see if I can give this a try, because you seem to be a bit confused about how an HTML form works.
First and foremost, your website looks like so, correct?
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action="http://poster.decaptcher.com/"
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
</head><body></body></html>
One thing to point out before we explain an HTML form, is that you have your form in the <head> of the webpage. Any element which is supposed to be seen by the user (or anything that you want to appear within the browser's main viewing area) should be in the <body>. Failure to do this puts the browser into a "quirks mode", where it actually doesn't know what you're talking about and it makes its best guess to try and build the website that it thinks you wanted. Mind you that modern browsers are very good guessers, but you should still re-write it as:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form
method="post"
action="http://poster.decaptcher.com/"
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
</body>
</html>
As far as explaining the <form> tag... When you submit a form in HTML, it actually loads the other website. It doesn't secretly send data in the background, it will take you away from the page you're viewing and take you to the page that you are sending the data to. At first this may sound silly. Why should it take you away from the page you're viewing just to send the data to another website? If you wanted to be redirected after sending the data, you'd redirect them there after sending the data.
The reason it's done this way is to greatly simplify the HTTP protocol. Whenever you load any website, you send and HTTP request. This request contains butt-loads of information. Among this information is:
Your IP address
What browser you're using
The page you were last visiting
How you accessed this page (clicked a link or typed the URL into the address bar)
The page you want to view (is it index.html or mysite.html?)
Any cookies related to that server
Any POST information (extra information which the server may or may not have asked for)
Every time the server receives one of these requests, it looks at all of the information and decides what to do. Usually a server will just look at the page you want to view and send it to you. Sometimes the page you want to view will need some extra work before it's ready to show, though. For instance, if a page ends in .php then it will search through the page for <?php, and everything after that point will be executed as a script. Only the output of the script is sent to the person who requested the page, not the script itself.
If you were to send your POST information to a website, wait 10 minutes, THEN go to the website, it would have no way of remembering that it was you who sent the post information before or what information you sent. Web servers have a very short attention span. For that reason if you sent a form to log into a website, then waited 10 minutes, then tried to view a member's only page- it would forget that you were logged in. For this reason it sends you the page as you're submitting the form. It does it while it still remembers that you're logged in, before it has a chance to forget. There's a good chance that the page it sends you will include a cookie which you can use to remind the server you were logged in next time you request a page.
If this made sense, then you should understand what happens when you submit a form. It doesn't just take your information and give it to the server. It sends that information to the server as part of an entire request, then the server sends you back a webpage and your browser displays that webpage. There is really only one way to send data to a server without redirecting you to that server afterwards. There are multiple ways to do this trick, however. You have to send a "dummy request", requesting a webpage with certain POST data, but ignoring the webpage that's returned.
In your example, you wanted to send data to http://poster.decaptcher.com. To do this without redirecting the user to http://poster.decaptcher.com, your easiest solution would be to use javascript and AJAX. Javascript has certain functions that allow you to send an HTTP request without reloading the page, then you let the javascript determine what to do with the page that's returned.
This is generally used when you want to reload a part of a webpage without reloading the whole thing. For instance, if you have a chat program and you want to update the chat window without refreshing the entire page. The javascript would request a webpage which contains ONLY the new lines of chat, minus any <html>, <head>, or <body> tags. It then takes those lines and displays them in the chat window.
You can, however, use AJAX to request a page and then ignore what's returned instead of display it on the page. By doing this you will have sent the POST data but not redirect the user.
Another option is to send the request to a third website, which can then send its own dummy request. For instance, submit the form to a PHP page that you own. The PHP script can then tell your server to send a dummy request to http://poster.decaptcher.com and ignore the response, then you can send them a webpage containing whatever you want.
Now that I've described both of these processes in adequate detail, I'll leave it as an exercise to the reader to figure out exactly how to do these. =)
The page refresh on submitted form is the default behavior of HTML.
For people who need to display the response into the same page without refresh, they will want to use Ajax. Here is how it could be done with jQuery:
$('#the_form').submit(function (e) {
e.preventDefault();
the_form = $(this);
$('#response_container').load(
the_form.attr('action')
, the_form.serialize()
);
})
the action defines the redirect to that page. If you want to catch the response, make your own script and place it in between the two. This is a bad way of doing it though. We developers call it hack coding. lol.
Not quite sure what you want to do. If you want to show the POST content on the page, just do this:
print_r($_POST);
If you want to see what is getting POSTed to the action URL, and you don't have access to that URL, just use the HTTP Headers plugin for Firefox.
action should go to a PHP file belonging to you! ie - action="/ProcessMyForm.php"
On that file, simply use $_POST and those form elements are in there, indexed by name, in an associative array.
Also - it may have been accidental, but post parameters dont go up in the URL like get, they are "behind the scenes" (invisible to the user) and also capable of being far larger.
PS - if you want to go to that other site afterwards, use header("Redirect: other-website-here.com")
First of all, mention your question specifically. If you want to fetch data from a URL than you can't use the form method="post". If you want to fetch data from URL, you have to use method "get". Calling print_r($_GET) can be used to retrieve data from HTML page to controller page.

isset code not being executed PHP

I was wondering if my code below is even correct, I've been having numerous errors with this, but am not sure if the problem really exists here. The code is below:
The user will click 'Exit Group'.
<p class="logout"><a id="exit" name="logout" href="#">Exit Group</a></p>
The code that should be execute when 'Exit Group' is clicked is below:
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
However, the code I am trying to execute when the user clicks 'Exit Group' is not even being executed. There is nothing wrong with the code within the braces, as numerous people have checked it. But I was wondering if my problem may lie in the code above? Thank you.
If you click the link, nothing happens because the URL only contains the fragment identifier #. Not even a GET request will be issued.
You use this kind of link normally to jump to an element inside the page (e.g. Top to jump to an element with ID top). This is completely handled in the browser.
And if you only put the fragment identifier there, just nothing will happen. This is very often used if the link should execute some JavaScript and should actually not link to something else.
You are testing the $_POST array at the server side. But this array only contains elements, if you initiate a POST request by a form. That means you need to create a form with a submit button, e.g.:
<form action="" method="POST">
<input type="submit" name="logout" value="Exit Group" />
</form>
Here comes the name attribute into play, which will be the key in the $_POST array. But assigning this on a normal link will have no effect.
You could do it also with the link, but with a GET request this way:
<a id="exit" href="?logout=1">Exit Group</a>
<!-- ^-- parameter must be part of the URL, name has no effect -->
and
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
Note that you have to pass a parameter logout it here.
It seems you have mixed up GET and POST requests. If you have a form, the name s of the form elements will be transmitted as parameters to the server. That means given this form:
<form method="POST">
<input type="text" name="foo" value="" />
<input type="text" name="bar" value="" />
<input type="submit" name="send" value="Send" />
</form>
if the user clicks on the submit button, the $_POST array at the server side will have the keys:
$_POST['foo']
$_POST['bar']
$_POST['send']
This does not work with links though. A click on a link will create a normal GET request, and here, the parameters must be part of the URL, appended after a question mark ? and separated by an ampersand &:
Link
will result in
$_GET['foo']
$_GET['bar']
$_GET['andMore']
You probably should read about the HTTP protocol.
a isnt a form control. it needs to be an input or select if it's within a form.
For manual linking, do href="/page?logout"
You're using a regular hyperlink, no form will get posted. you need a submit button of some kind in a form with method="post" to do that. regular links just result in GET requests and nothing will ever be posted that way.
edit: added simple example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Form test</title>
</head>
<body>
<?if ($_SERVER['REQUEST_METHOD'] == 'POST'):?>
<pre><? print_r($_POST)?></pre>
<?endif;?>
<? // $_SERVER['REQUEST_URI'] holds the current URL, so we know that ?>
<? // we'll end up back in this file when the form is submitted. ?>
<form method="post" action="<?= $_SERVER['REQUEST_URI']; ?>">
<input type="text" name="textbox"
value="<?= isset($_POST['textbox'])?$_POST['textbox']:'Type something' ?>" />
<input type="submit" name="submitbutton" value="Submit" />
</form>
</body>
</html>
$_POST will only be filled if you use a form with method=post.
Yes. A POST and a GET are two different things ;)
if(isset($_GET['logout']))
This <a id="exit" name="logout" href="#"> should be <a id="exit" href="?logoff=true#">.
Then logoff will be in the $_GET array.

Categories