Redirect to another page after if condition - php

After submitting data from a form, I want to be redirected to a page, here's my code :
<form action="#result" method="POST">
<input name="zipcode" type="text" placeholder="Your ZipCode" />
<input name="zipcode_submit" type="submit" value="Send" />
</form>
<div id="result">
<?php
if(isset($_POST['zipcode_submit'])) {
header("Location: http://twitter.com");
}
?>
</div>
It does not work for me and I don't know why
Thanks for your help

try to shift the php code, above the form tag,i.e
<?php
if(isset($_POST['zipcode_submit'])) {
header("Location: http://twitter.com");
}
?>
above
<form action="#result" method="POST">

Have a look at php.net docs regarding the header function. It has to be the first output to the website. Put your form at the end of the file.
<html>
<?php
/* this will produce an error, header must be the first output on the website */
header('Location: http://www.example.com/');
exit;
?>
You should place a exit; after the header statement, to fully prevent the following code from being executed.

Related

How to exclude/remove an input after the php code is executed.?

So i have an post input where i submit data something simple.
<form method="post" action="result.php">
<input type="url" name="url" class="form-control" placeholder="http://example.com/">
<input type="submit" name="submit" />
</form>
After the html code is going to be executed a php code which echo success or something like this that doesn't matter.
But i have a problem when i include('submit.php') it's going to show also the input and i don't want this.
How i can do that to don't show the input on result.php?
If you want it to be user-specific, you can try to use cookies or sessions like this:
index.php
<?php
session_start();
?>
<?php if(!isset($_SESSION['show_button']) && !$_SESSION['show_button'] ){ ?>
<!-- Button logic here... -->
<?php } ?>
result.php
// If the url has been entered, it returns a false from empty()
$_SESSION['show_button'] = empty($_POST['url']);

How to send the user to different pages when the checkbox is checked?

<form entype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
Hi hello I want to ask a simple question. The code above is search.php,
I want to send the form to a different page based on if the checkbox is checked or not. If the checkbox is checked it will be directed to resqueryexp.php, but if not it will be directed to result.php
I have been trying to adding this code but it doesn't work.
<?php
if (isset($_GET['queryexp'])){
header("Location: resqueryexp.php");
}else{
header("Location: result.php");
}?>
Sorry for my bad English and Thanks in advance.
<?php
if ( isset( $_GET['submit'] )) {
if ($_GET['queryexp'] == 1 ){
header("Location: resqueryexp.php");
exit;
}
else
{
header("Location: result.php");
exit;
}
}
?>
<html>
<head><title>test</title>
</head>
<body>
<form enctype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
This code won't run here at SO, but this is how it may work on your webserver. The important part is to test if the form was submitted. So, in this case, I gave the submit button the name of "submit" and then tested with PHP to see if the form was even submitted. If the form is submitted and if the checkbox is checked, the redirect via header() occurs. Otherwise, if the checkbox is unchecked, then the redirect occurs via header to result.php. You may avoid header issues by making an adjustment to you PHP.ini settings and adding this line "output_buffering = On".
Note: usually a form with the enctype attribute having a value of "multipart/form-data" involves submitting a file and under such circumstances the method attribute should be a POST request instead of a GET; see MDN.
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
http://php.net/manual/en/function.header.php

PHP sessions problems

I'm using sessions to save what ever the user types in the form and what ever they do type will be displayed on the other pages.
It was working perfectly fine but after all the server uploads and such my code has completely done one on me and i'm lost.
Can somebody see if they can spot a mistake? I need fresh eyes.
HTML.
<div id="form"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start(); // declaring the variable
if(isset($_POST['fullName'])){ //setting the variable
if(empty($_POST['fullName'])){ //if empty dont to nothing but my wep page will reload
}else{ //if they have do this
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php"); //then will direct the user to the home page (will also display name on each page)
}}
?>
Session on other pages
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName']
?>
</div> <!--div ECHO end -->
$_SESSION['fullName'] = $_POST['fullName'];
session_register(fullName);
replace with this code try it
You'll need to add session_start() on whatever page you are redirecting to that is supposed to display the data.
Also, (I'm assuming you realize) what you posted doesn't have anything that would output the data, like:
<input type="text" name="fullName" value="<?php echo $_SESSION['fullName']; ?>"/>
You need to start session on other page as well and stop the script from setting that session. After header location you need to use exit here.
<?php session_start();?>
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName'];
?>
you need use exit after header location :-
header('location: home.php');
exit;
Just change the div id form to other because it has a default and remove the empty function because you add isset functon.
Use this.
<div id="myform"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start();
if(isset($_POST['fullName']))
{
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php");
exit();
}
?>
Session on other pages.
<div id="echo"> <!-- div ECHO start -->
<?php
session_start();
print_r($_SESSION);
echo $_SESSION['fullName'];
?>
</div> <!--div ECHO end -->
May be it helpful to you.If any problem then let me know.
You are "posting" the values to home.php, doing that you can't set $_SESSION['fullName'] = $_POST['fullName'] in the origin.
Change
<form action="home.php" method="post">
to
<form action="name_of_the_first_script.php" method="post">
$_POST['fullName'] does not exist before the redirect.
Here is how everything should look like (lest call the page index.php):
<div id="form"><!--Form Start-->
<form action="index.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
now after you hit submit the index.php will be reactioned and at this time with the $_POST request meaning that that the condition
if(isset($_POST['fullName'])){
will be true and the PHP code can be executed, setting the $_SESSION variable and redirecting you to home.php where you ca now read the $_SESSION previously set in index.php
Hope this can me more clear now! :)

I'm not able to redirect the URL to a different webpage after submiting form values

Hello Webmasters iam trying to redirect the webpage to a different URL/webpage after submitting the form elements ...i tried many ways but could not fix it... please check the codes below that i have tried so far...
<?php
if(isset($_REQUEST['down'])){
header("header("location: domainpath/kothi.html");
}
?>
<html>
<body>
<form action="glitter.php" method="post">
<input type="radio" name="font" value="fonts/darkcrystaloutline.ttf"/>
<input type="radio" name="font" value="fonts/darkcrystalout.ttf"/>
</form>
</body>
</html>
i have also tried
<?php
if(isset($_REQUEST['font'])){
header("location: domainpath/kothi.html");
};
?>
i also tried
<?php
header("location: domainpath/kothi.html");
?>
please help me to fix the problem....
Is that file glitter.php? The redirection script should be kept in glitter.php as that is the page that will be loaded when you submit the form.
There are several ways to do a redirect.
via meta tag: http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm
via header: http://php.net/manual/en/function.header.php (you have to put exit after the header statement)
via Javascript: http://www.tizag.com/javascriptT/javascriptredirect.php
Firstly, the below:
<?php
if(isset($_REQUEST['down'])){
header("header("location: domainpath/kothi.html");
}
?>
This is incorrect, the header is incorrectly declared, replace header with the header below:
<?php
if(isset($_REQUEST['down'])){
header("location: domainpath/kothi.html");
}
?>
Secondly, why $_REQUEST? You're making a POST, and thirdly, where is down coming from? You form is submitting font so the following is what you need:
<?php
if(isset($_POST['font'])){
header("Location: domainpath/kothi.html");
exit();
}
?>
exit() is added to stop the rest of the page loading too, by the way...
Update
It may also be best if you submitted the data too, so include the below inbetween the <form> tags
<input type="submit" name="submit" value="Submit this form">
And like others point out, I hope this PHP page is called glitter.php so it can submit to itself...
Update 2
Based on your comment, then you'd want the following:
<?php
if(isset($_POST['down'])){
header("location: domainpath/kothi.html");
exit();
}
?>
<html>
<body>
<form action="glitter.php" method="post">
<input type="radio" name="font" value="fonts/darkcrystaloutline.ttf"/>
<input type="radio" name="font" value="fonts/darkcrystalout.ttf"/>
<input type="submit" name="down" value="down">
</form>
</body>
</html>
Though the above form will go to glitter.php and the header will not redirect anywhere - one has to assume that another form/page submits to this one...
You have action="glitter.php", which means after submitting the form the result inputs will be accessible into glitter.php file. So in that file you have to redirect to whatever url you wanna go.
You already asked this question once, but here add this :
error_reporting(E_ALL);
But i have a assumption: You have to make an exit() after header() cos the rest is not allowed to be give an output. If it gives an output the header will be set to a HTML-Document and can therefore not be reset.
So try this:
<?php
if(isset($_REQUEST['down']))
{
header("location: /kothi.html");
exit();
}
?>
<html>
<body>
<form action="glitter.php" method="post">
<input type="radio" name="font" value="fonts/darkcrystaloutline.ttf"/>
<input type="radio" name="font" value="fonts/darkcrystalout.ttf"/>
</form>
</body>
</html>
BTW: I hope this form is in glitter.php and where is the input for down?
If this file is not glitter, add this in glitter:
<?php
if(isset($_POST['font'])){
header("Location: domainpath/kothi.html");
exit();
}
?>
And kill the php in the form-page.

POST without redirect with PHP

I have a simple form for a mailing list that I found at http://www.notonebit.com/projects/mailing-list/
The problem is when I click submit all I want it to do is display a message under the current form saying "Thanks for subscribing" without any redirect. Instead, it directs me to a completely new page.
<form method="POST" action="mlml/process.php">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="submit" value="" id="submit"name="submit" >
</form>
You will need AJAX to post the data to your server. The best solution is to implement the regular posting, so that will at least work. Then, you can hook into that using Javascript. That way, posting will work (with a refresh) when someone doesn't have Javascript.
If found a good article on posting forms with AJAX using JQuery .
In addition, you can choose to post the data to the same url. The JQuery library will add the HTTP_X_REQUESTED_WITH header, of which you can check the value in your server side script. That will allow you to post to the same url but return a different value (entire page, or just a specific response, depending on being an AJAX request or not).
So you can actually get the url from your form and won't need to code it in your Javascript too. That allows you to write a more maintanable script, and may even lead to a generic form handling method that you can reuse for all forms you want to post using Ajax.
Quite simple with jQuery:
<form id="mail_subscribe">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="hidden" name="action" value="subscribe" />
<input type="submit" value="" id="submit"name="submit" >
</form>
<p style="display: none;" id="notification">Thank You!</p>
<script>
$('#mail_subscribe').submit(function() {
var post_data = $('#mail_subscribe').serialize();
$.post('mlml/process.php', post_data, function(data) {
$('#notification').show();
});
});
</script>
and in your process.php:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'subscribe' :
$email_address = $_POST['address'];
//do some db stuff...
//if you echo out something, it will be available in the data-argument of the
//ajax-post-callback-function and can be displayed on the html-site
break;
}
}
?>
It redirects to a different page because of your action attribute.
Try:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="address" id="email" maxlength="30" size="23" />
<input type="submit" value="" id="submit" name="submit" />
</form>
<?php if (isset($_POST['submit'])) : ?>
<p>Thank you for subscribing!</p>
<?php endif; ?>
The page will show your "Thank You" message after the user clicks your submit button.
Also, since I don't know the name of the page your code is on, I inserted a superglobal variable that will insert the the filename of the currently executing script, relative to the document root. So, this page will submit to itself.
You have to use AJAX. But that requires JavaScript to be active at the users Brwoser.
In my opinion it's the only way to do without redirect.
to send a form request without redirecting is impossible in php but there is a way you can work around it.
<form method="post" action="http://yoururl.com/recv.php" target="_self">
<input type="text" name="somedata" id="somedata" />
<input type="submit" name="submit" value="Submit!" />
</form>
then for the php page its sending to have it do something but DO NOT echo back a result, instead simply redirect using
header( 'Location: http://yourotherurl.com/formpage' );
if you want it to send back a success message simply do
$success = "true";
header( 'Location: http://yourotherurl.com/formpage?success='.$success);
and on the formpage add
$success = $_GET['success'];
if($success == "true"){ echo 'Your success message'; } else { echo
'Your failure message';
Return and print the contents of another page on the current page.
index.php
<html>
<body>
<p>index.php</p>
<form name="form1" method="post" action="">
Name: <input type="text" name="search">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$_POST['search'];
include 'test.php';
}
?>
</body>
</html>
test.php
<?php
echo 'test.php <br/>';
echo 'data posted is: ' . $_POST['search'];
?>
Result:
Just an idea that might work for you assuming you have no control over the page you are posting to:
Create your own "proxy php target" for action and then reply with the message you want. The data that was posted to your php file can then be forwarded with http_post_data (Perform POST request with pre-encoded data). You might need to parse it a bit.
ENGLISH Version
It seems that no one has solved this problem without javascript or ajax
You can also do the following.
Save a php file with the functions and then send them to the index of your page
Example
INDEX.PHP
<div>
<?php include 'tools/edit.php';?>
<form method="post">
<input type="submit" name="disable" value="Disable" />
<input type="submit" name="enable" value="Enable" />
</form>
</div>
Tools.php (It can be any name, note that it is kept in a folder lame tools)
<?php
if(isset($_POST['enable'])) {
echo "Enable";
} else {
}
if(isset($_POST['disable'])) {
echo "Disable";
} else {
}
?>
Use
form onsubmit="takeActions();return false;"
function takeAction(){
var value1 = document.getElementById('name').innerHTML;
// make an AJAX call and send all the values to it
// Once , you are done with AJAX, time to say Thanks :)
document.getElementById('reqDiv').innerHTML = "Thank You for subscribing";
}

Categories