Send user to other page on input - php

I have a pege (index.php) with some text and a form with a single text input field. What i want is for the user to be sent to another page, upload.php if the input is "upload" or browse.php if input is "browse" and so on, how can this be achieved?
Thank you

<?php
$page=$_POST['txtpage'];
$redirect=$page.'.php';
if(file_exists($redirect))
{
header('Location:'.$redirect);
}
?>

You could use header() to send a location header depending on the value of the input.
switch ((string) $_POST ['mode'])
{
case 'browse':
case 'upload':
// Redirect
header('Location: http://www.example.com/' . $_POST ['mode'] . '.php');
die ();
break;
default:
// invalid input
break;
}
<form action=<?php echo ($_SERVER ['SCRIPT_NAME']); ?>
<select name="mode">
<option value="">select...</option>
<option value="upload">Upload something</option>
<option value="browse">Browse files</option>
<input type="submit" />
</select>
</form>

if you want redirect on another page when blur this textfield
<input onblur="window.location.href='YOUR_URL?value='+this.value" />
if you want redirect on another page when keyup on this textfield
<input onkeyup="window.location.href='YOUR_URL?value='+this.value" />
This will work for you

Related

check if post request has been recieved

i have created html form (in MyTestFile.php file) with several options, and what i want to do is to check if post request has been recievied. Or in other words when user chooses something from my combobox and pushes button "Go" then it goes to the same page and sends choosed size value and echoes the size, and then form is not visible any more when size has been set. So here is my form code which isn't really working:
if (isset($_POST('submit'))) {
?>
<html>
<h2>Select size:</h2>
<form action="MyTestFile.php" method="POST">
<div>
<select name="SIZE">
<option value="big">big</option>
<option value="medium">medium</option>
<option value="small">small</option>
</select>
<input type="submit" value="Go">
</form>
</html>
<?php
} else {
echo $_POST('submit');
//and do some stuff here
}
The main idea: check if post request with size has been recieved, if not - show the form, where user can choose size.
$_POST is an array, so you must use square brackets to access the elements, not parenthesies. So change this:
if (isset($_POST['submit'])) {
// ^ ^ square breackets not ()
Secondly your submit button doesn't have a name, so your isset() check will never be true. Give it a name:
<input type="submit" name="submit" value="Go">
Or use the SIZE select in the isset check:
if (isset($_POST['SIZE'])) {
Finally your isset check needs reversing, in order to show the form when the post value is not present:
if (!isset($_POST['SIZE'])) {
// ^ not operator to reverse the check
I can see you tried so here you go:
Just add an exclamation mark before isset, add a name to the submit button and replace those parentheses with square brackets.
if (!isset($_POST['submit']))
{
?>
<html>
<h2>Select size:</h2>
<form action="MyTestFile.php" method="POST"><div>
<select name="SIZE">
<option value="big">big</option>
<option value="medium">medium</option>
<option value="small">small</option>
</select>
<input type="submit" name="submit" value="Go">
</form>
</html>
<?php
}
else
{
echo $_POST['size'];
//and do some stuff here
}
Use [ don't use ( in $_POST method. Because $_POST is array so use square brackets. Use same submit as name in submit button.
$_POST['submit'] instead of $_POST('submit')
and add ! (Not Set) in if condition, use below code,
if(!isset($_POST['submit'])){
// Your Form Code
}
else{
print_r($_POST);
}

Null textbox then Redirect

So i have a form method post in index.php where in it will send the data from a textbox to another page which is print.php.
now what i want to do is if the textbox from index.php is null it wont redirect to print.php or if it redirect to print.php it will be redirected back to index.php.
index.php format
<form action="print.php" method="post" target="_blank">
<input type="text" name="faidf" id="faidf" size="25" value="" maxlength="25"/></td>
<input type ="submit" value="Print">
print.php
<?php
$faidf = $_POST['faidf'];
if(isset($_POST['faidf'])) {
echo "<td><font size=2>FAID:$faidf</td><td></font></td>";
}
else {
echo "FAID is missing";
}
?>
instead of FAID is missing could i redirect it home because i have about 10more php wherein it needs the variable of $faidf so the whole printd.php is utterly useless if the textbox is blank.thanks
You can do this by two ways:
Way 1:
Use JQuery/JavaScript for the form validation process onsubmit of the form. It will redirect only in case where there is data found in textbox.
Way 2:
Check the length of provided post data and if the length is less than 1 return it to the previous page.
I will advice you to use the JavaScript/JQuery as it will run on all cross browsers and easy to implementation and changed easyly
first add onchange function to your textbox
<input type="text" onchange="myFunction(this)" name="faidf" id="faidf" size="25" value="" maxlength="25"/>
and add an Id for the button
<input type ="submit" value="Print" id="btn" />
and add script tag in your page :
function myFunction(e)
{
var x=document.getElementById("btn");
if (e.value == ''){
x.setAttribute('disabled','disabled');
}else{
x.removeAttribute('disabled');
}
}
and instead of your echo at print.php add location header
header("Location: index.php");

how to select option to submit different php page

drop down select "sales" go to sales.php, other options selected go to addFund.php...
<form name='searform' method='post' action='<?php echo $home;?>addFund.php'>
Search : <input type='text' id='sear' name='sear'> by
<select id='psel' name='psel' onchange='change()'>
<option value='email'>Email</option>
<option value='name'>Username</option>
<option value='domain'>Domain name</option>
<option value='sales'>Sales</option>
</select>
<input type='submit' name='sub' id='sub' value='Go' onclick='gopage()'>
<input type='submit' name='dir' id='dir' value='Direct'>
</form>
How to submit different pages
You could use some Javascript nastiness to achieve this with the change() function, but the usual way to do this is to route all requests through a controller and include() the appropriate page. For example, point your form to action.php, and in that file, do this:
action.php
<?php
if (isset($_POST['psel']) && $_POST['psel'] == 'sales') {
include 'sales.php';
} else {
include 'addFund.php';
}
...or you could just put roughly that code into addFund.php, since you only seem to have one other script that you would want to send requests to.
You could do this with javascript:
function change(el) {
if(el.value === 'sales') {
el.form.action = 'sales.php';
} else {
el.form.action = 'addFund.php';
}
}
Change the onchange to onchange="change(this)". A better way would be to check the variable on serverside and include the right file.
Change your select to this:
<select name="vote" onChange="document.forms['searForm'].submit()">
And make your form action go to something like pageChange.php where it returns a different page depending on the $_POST value.

PHP - Form: save entries/selections on form submit

I have a PHP form that has some drop down selections and text field entries. If the user selects the wrong item from the dropdown, when they submit the form, I have it so that it will show an error message to the user and force the browser to go back to the previous page. The problem is that the user has to re-enter all of the information.
How do I make the form save the data until the form submit is successful?
EDIT:
Form submit method is $_POST and the form is being submitted to another page.
This would have to be done with strictly PHP as Javascript/Jquery solutions can be script blocked by more secure users.
Here you go. This will work, and is not dependent on Javascript:
form.php //the form page
<?php session_start(); ?>
<form method="post" action="action.php">
<input type="text" id="input1" value="<?php echo (isset($_SESSION['fields']) ? $_SESSION['fields']['input1'] : '') ?>" />
<input type="text" id="input2" value="<?php echo (isset($_SESSION['fields']) ? $_SESSION['fields']['input2'] : '') ?>" />
</form>
action.php //the action page
<?php
session_start();
//do your validation here. If validation fails:
$_SESSION['fields']['input1'] = $_POST['input1'];
$_SESSION['fields']['input2'] = $_POST['input2'];
//redirect back to form.php
?>
Is the form a POST or a GET? Either way, you have access to all the submitted fields in the PHP variables $_POST or $_GET. Within your HTML you can pass those values (if set), to the default value of each HTML input element. This way, if it is a first time, they will be blank, if there was an error, the values will repopulate.
If they're select values, you can do something like this:
<select name="my_select" id="my_select">
<option value="123"<?php if($_REQUEST['my_select'] == 123) echo ' selected="selected"; ?>>123</option>
</select>
If you have regular text inputs, you can simply apply the $_REQUEST variable to the value attribute:
<input type="text" name="my_text" value="<?php echo $_REQUEST['my_text'] ?>" />
I suggest a preventing the page from navigating away from the submission until the data is verified. Enter jQuery :)
<script type="text/javascript" src="jquery-library.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// Wait for the user to click on your button
$('#submit_button').click(function(){
// Check each form field for an appropriate value
if ($('#form_field1').val() != 'something I expect')
{
alert('Wrong submission!');
return false;
}
// Forward the user to some url location
window.location = 'url';
return false;
});
});
</script>

Setting PHP cookie from dropdown menu

I have a PHP script designed to allow users to decide which language a page is displayed in. The information is stored in a cookie and then read when needed to display the correct content.
Currently, I use an HTML dropdown box to allow the user to select the language and then they must press the form submit button to set the cookie. How can I make it so when they select the language in the dropdown menu it automatically selects that and submits the form? I hope you can understand my question.
My current PHP code is:
<?php
$user_lang = null;
if (isset($_POST["setc"])) {
$expire = time() + 60 * 60 * 24 * 30;
setcookie("mycookie", $_POST["sel"], $expire);
header("location: " . $_SERVER["PHP_SELF"]);
} else if (isset($_COOKIE["mycookie"])) {
$user_lang = $_COOKIE["mycookie"];
}
?>
<meta charset='utf-8'>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<select name="sel" size="1">
<option value="en"<?php echo (!is_null($user_lang) && $user_lang === "en" ? " selected" : ""); ?>>English</option>
<option value="es"<?php echo (!is_null($user_lang) && $user_lang === "es" ? " selected" : ""); ?>>Español</option>
<option value="fr"<?php echo (!is_null($user_lang) && $user_lang === "fr" ? " selected" : ""); ?>>Français</option>
<option value="de"<?php echo (!is_null($user_lang) && $user_lang === "de" ? " selected" : ""); ?>>Deutsch</option>
</select>
<input name="setc" value="save setting" type="submit">
</form>
Change your select tag to:
<select name="sel" size="1" onchange="this.form.submit();">
to make the form submit when users selects a language.
If you wish to do that without JavaScript you can use multiple submit buttons method instead:
<input name="set_language[en]" value="English" type="submit">
<input name="set_language[es]" value="Español" type="submit">
<input name="set_language[fr]" value="Français" type="submit">
<input name="set_language[de]" value="Deutsch" type="submit">
Processing this form is simple as it is:
if (isset($_POST["set_language"])) {
$language = key($_POST["set_language"]);
// $language contains user-selected language code now
}
This can't be done with a select field but without JavaScript or any other client-side scripting language.
You need to do it using JavaScript:
<select name="sel" size="1" onchange="this.form.submit();">
Or if you use jQuery:
$("select[name='sel']").change(function() {
$(this).parent().submit();
});
This will submit the form whenever a user changes value in the dropdown list.
You can add an onchange event to the select:
<select onChange="document.forms[0].submit();">
Remember that it's a good thing to leave the submit button for those users who don't have javascript enabled. You can hide the button using javascript, so it won't clutter the window of those that doe have a regular browser.
I think you have to use javascript like the others said, but you can also use the submit button as a backup if the user doesn't have javascript activated:
<noscript><input value="save setting" type="submit"></noscript>
And then, since the submit button will only be shown when the user has javascript disabled, you will need to add a hidden input to check if the form has been submitted on the server side
<input type="hidden" name="setc" value="true" />
The answer is in JavaScript not PHP. You could do it unobtrusively with JavaScript like this:
<script>
window.onload = function() {
document.getElementById("idOfDropDown").addEventListener("change", function() {
document.forms["formName"].submit();
});
}
</script>

Categories