switch function to change page based on user input not working - php

I am trying to change page based on user input in form. The user enters their tag and it changes to that users page with details. Currently, it just keeps attempting to change page then eventually times out. Here is my code:
SWITCH STATEMENT
if(isset($_POST['submit'])) {
$name = $_GET['clan_tag'];
switch($name) {
case "player1":
header("Location: commander.php");
break;
case "player2":
header("Location: officer.php");
break;
...//
default:
header("Location: index.php");
}
}
FORM
<form action="" method="get">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
So if user enters player 1 in form then submits, it should change to commander.php page, but it isn't.
Could anyone point me in correct direction thanks.

First of all, change first line to
if(isset($_GET['submit'])){
2nd give a 'name' attribute to submit button
<input type="submit" class="submit" value="SUBMIT" name="submit"/>

I just looked at your code and realized your form was sending data using the HTTP GET method but your PHP script was checking for HTTP POST method in the if(isset($_POST['submit'])). So I modified the PHP as shown below.
if(isset($_GET['clan_tag'])){
$name = $_GET['clan_tag'];
switch($name) {
case "player1":
header("Location: commander.php");
break;
case "player2":
header("Location: officer.php");
break;
default:
header("Location: index.php");
}
}
?>
I also modified the HTML as shown below.
<form action="" method="get">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>

Try in this way:
In the FORM:
attribute method write "post" in action= write "path where is your php file"
i.e.
<form action="path/yourfile.php" method="post">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
in PHP file
if(isset($_POST['clan_tag'])) {
$name = $_POST['clan_tag'];
}
else{
$name="";//your code
}
....

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.

How to stop submit being shown in the address bar?

How do i stop the submit being shown in the search bar? am i doing something wrong? if i use post method it works fine, but recently i've tried using the get method and it prints both submit and the textbox text in the bar, i tried removing the name="submit" and it didn't do anything when i clicked it. SO how do i stop submit being shown in the address bar?
at the moment it give me $submit=submit, :(
ty all.
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
<?php
if(isset($_GET['submit']))
{
if(!empty($_GET['website']))
{
//do stuffs here
}
else
{
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
Just use:
!empty($_GET)
In your first if statement
So your code should look something like this:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(!empty($_GET)) {
if(!empty($_GET['website'])) {
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
To remove the variables from the URL bar, you need to use the POST method instead of the GET method.
So, this form code should work for you:
<form action="" method="POST">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
You'll also need to alter your PHP code to use $_POST instead of $_GET.
The downside to this is that it won't show the submitted website in the URL bar, meaning users can't bookmark a submitted form. However, this may also be intended.
Alternative Solution
You can remove the name from the submit, and use isset($_GET) to test whether the form was submitted instead.
Here is an example of how this could be done:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(isset($_GET) && count($_GET) > 0) {
if(!empty($_GET['website']))
{
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>

How to set two buttons on one form html&php

This form should calculate numbers and save
Now there are two buttons One is call Calculator and two call Save
If I press Calculator
I get the form action is going to file name save.php And I do not want it that way
How can I set it up that button do something else
Example
Calculator = Calculator
Save = save.php
Is it possible to set it
Because it is one form
Thanks to anyone who can help
<?php
error_reporting (0);
$NUM = $_POST["NUM"];
$NUM2 = $_POST["NUM2"];
$NUM = "$NUM";
$NUM2 = "$NUM2";
$subtotal= $NUM+$NUM2;
?>
<form action="save.php" method="POST" name="Calculator">
<p>
<input name="NUM" type="text" value="<?php echo $_POST["NUM"]; ?>" />
</p>
<p>+</p>
<p>
<input name="NUM2" type="text" value="<?php echo $_POST["NUM2"]; ?>" />
</p>
<p>
<input name="subtotal" type="text" value="<?php echo "$subtotal";?>" />
</p>
<p>
<input name="submit" type="submit" value="Calculator" />
<p>
<input name="submit" type="submit" value="Save" />
</p>
</form>
You can have all the logic in a single PHP script (no need to direct to a different script depending on the button). If the logic is complicated, use include statements in order to separate the code.
Name the buttons differently:
<input name="calculator_submit" type="submit" value="Calculator" />
<input name="save_submit" type="submit" value="Save" />
Then in PHP:
if (isset($_GET['calculator_submit'])) {
// ...
} else if (isset($_GET['save_submit'])) {
// ...
} else {
// ...
}
If you really need different PHP script, then you'll have to go with Javascript (function will change the form action when a submit is clicked).
Since you are now using two submit buttons, both will submit the form and go to save.php.
Make your "calculator" button an input type=button instead of submit, and handle it via JavaScript.
Just FYI:
HTML5 allows to define a different form target URL by specifying the formaction attribut on a submit button – but browser support is lousy as of now.
Form and Buttons
<input name="submit" type="button" onclick="submitForm('Calculator')" value="Calculator" />
<input name="submit" type="button" onclick="submitForm('Save.php')" value="Save" />
Some jquery:
function submitForm(path) {
$('#Calculator').attr('action', path);
$('#Calculator').submit();
}

2 forms with one PHP file

I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}

$_POST variable

I'm encountering a problem. I'm using Wordpress, but this ain't a Wordpress question.
I'm using two forms, on one form I have all the input fields and one hidden input field which I use for checking which form the user has submitted. I have saved its value as 'save'. There is another form which is just for resetting all the options and its value is 'reset'. In PHP, I check the value of the hidden field and take respective actions. But the problem is that the reset thingy isn't working.
Here is my HTML for the forms:
<fieldset>
<form method="post">
<!-- Some input fields here-->
<p class="submit">
<input name="save" type="submit" value="Save changes" />
<input type="hidden" name="action" value="save" />
</p>
</form>
</fieldset>
<fieldset>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</fieldset>
In PHP, I verify them like this:
// if I change the 'save' literal to something else like 'savea', $_POST variable will not be empty
// but if I dont, then $_POST variable is NULL
if ('save' == $_POST['action']) {
foreach ($this->cp_options as $option) {
if (isset($_POST[$option['id']])) {
update_option($option['id'], $_POST[$option['id']]);
}
else {
delete_option($option['id']);
}
}
header("Location: themes.php?page=functions.php&saved=true");
die;
}
// if I change the 'reset' literal to something else like 'reseta', $_POST variable will not be empty
// but if I dont, then $_POST variable is NULL
elseif ('reset' == $_POST['action']) {
foreach($this->cp_options as $option) {
delete_option($option);
}
header("Location: themes.php?page=functions.php&reset=true");
die;
}
The problem is if I change the 'reset' or 'save' literal to anything else like 'reseta' or 'saveasdfasd', $_POST variable won't be empty, but if I dont, then $_POST variable is NULL.
Any ideas on why this is happening?
[Old Answer Redacted]
EDIT
Try to isolate your testing environment first. This gave me results I expected.
<?php
if ( isset( $_POST['action'] ) )
{
switch( $_POST['action'] )
{
case 'save':
echo 'Save Action Requested';
break;
case 'reset':
echo 'Reset Action Requested';
break;
default:
echo 'Unknown action requested:';
var_dump( $_POST['action'] );
}
} else {
echo 'No action parameter received';
}
?>
<fieldset>
<form method="post">
<!-- Some input fields here-->
<p class="submit">
<input name="save" type="submit" value="Save changes" />
<input type="hidden" name="action" value="save" />
</p>
</form>
</fieldset>
<fieldset>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</fieldset>
I know you said that $_POST is null but are you assuming that or did you actually check $_POST == null? Have you tried doing a var_dump($_POST) to print out exactly what is getting sent across? Just comment out your re-direction and see what is in $_POST. Maybe that will give you a better clue as to what is happening.
Simple... remove the hidden inputs and change both of your "submit" buttons to have the same name, but different values:
<input type="submit" name="action" value="Reset" />
<input type="submit" name="action" value="Save" />
Then you can test it like this:
if ($_POST['action'] === 'Reset') {
// Do a reset here
} else {
// Do a save here
}
And you probably want to wrap the whole thing in:
if (isset($_POST['action'])) {
// Put your form handling here
}
If you have multiple forms on one page I'd recommend you send each form to a different URL. This is by far the simplest and most reliable way to detect where the form is going, just have two different scripts to deal with processing that form. You can then include or redirect to the final page you want the user to see.
This might because there are duplicate elements with the same name.
Can you try putting an id or name to your form?

Categories