Submit form redirect variable - php

I'm trying get a form to redirect to a certain page depending on what option is chosen in a select div. When I press submit, it redirects to the home page. I realize it's because it doesn't change $industry_choice until after the I press the button... is there a method to change this the moment you select a value?
Let's say I choose banking, and $industry_choice = "banking".
if(empty($_POST['industry'])) {
$industry_choice = "";
} else {
$industry_choice = $_POST['industry'];
}
<form method="post" action="http://localhost:8888/wordpress/<?php echo $industry_choice ?>">
<select name="industry">
(lots of options here)
</select>
<input type="submit" class="select-industry" value="Select">
</form>

You are doing it wrong. I think you are not clear with the action attibute of the form.
<?php
if(empty($_POST['industry'])) {
$industry_choice = "";
} else {
$industry_choice = $_POST['industry'];
header('Location: http://localhost:8888/wordpress/'.$industry_choice);
}
?>
<form method="post" action="">
<select name="industry">
(options)
</select>
<input type="submit" class="select-industry" value="Select">
</form>
action attribute will redirect to the page you processed the input. After that you will redirect it to targeted location.

Related

How to repopulate multiple select boxes and checkboxes

I cannot find a way that works to re-populate multiple select boxes, Since it is stored as an array is there a way to cross-check and repopulate the selections that were made? I am trying to answer question 4 from the comments section of the code.
The section of the form that I am trying to repopulate is the like_bands multiple select box
my code:
<?php
session_start();
/*
This page should:
1) Use a cookie (NOT PHP SESSION) that expires a year from now to do the following:
Record the timestamp of the FIRST load of this page (but NOT any subsequent load of this page).
Note: You only need first 3 parameters in setcookie() for this HW.
2) If the suvey was already completed, transfer to third page (thank you page).
That page instructs you to set a cookie that will allow you to detect this.
Recall from the CRUD example how page transfers are done in PHP:
header("Location: URL");
3) The form in this page should submit to THIS PAGE, and then transfer to hw_page2.php
after saving stuff to PHPs built-in $_SESSION superglobal.
The data will then be available in all the other pages (remember no database is allowed for this HW).
4) If the button in the 2nd page is clicked to come back to this page, the
Session data should re-populate into the form.
*/
if (!isset($_COOKIE['load_date'])) {
setcookie('load_date', time(), time() + (86400 * 365));
}
$load_date = $_COOKIE['load_date'];
$is_cool = $_POST['is_cool'];
var_dump($is_cool);
$bands = $_POST['like_bands'];
$band_list = "";
foreach ($bands as $band) {
$band_list .= $band. ", ";
}
$other_band = $_POST['other_band'];
echo $other_band;
$_SESSION['is_cool'] = $is_cool;
$_SESSION['bands'] = $band_list;
$_SESSION['other_band'] = $other_band;
?>
<!DOCTYPE html>
<html>
<head>
<title>User Profile</title>
</head>
<body>
<h3><?=$message?></h3>
User Profile:
<br><br>
<form action="hw_page1.php" method="POST" name="form1" onsubmit="return validate_form()">
<input type="hidden" name="task" value="process_profile">
<?php if ($is_cool != null) {?>
<input type="checkbox" name="is_cool" value="yes" checked='checked'>
Are you cool?
<?php } else { ?>
<input type="checkbox" name="is_cool" value="yes">
Are you cool?
<?php }?>
<br><br>
What Bands do you like?
<br>
<select name="like_bands[]" multiple> <small>* Required Field</small>
<option value="Sabbath">Black Sabbath</option>
<option value="Mastodon">Mastodon</option>
<option value="Metallica">Metallica</option>
<option value="Swift">Taylor Swift</option>
</select>
<br><br>
Favorite band not in the above list.
<br>
<input type="text" name="other_band" value="<?=$other_band?>">
<br><br>
<button type="submit"> Continue/Confirm </button>
</form>
<script>
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Client-side form validation
// Don't change the names of stuff in the form, and you won't have to change anything below
// Used built-in JS instead of JQuery or other validation tool
//////////////////////////////////////////////////////////////////////////////////////////////////////////
function validate_form() {
var form_obj = document.form1;
var count_liked = 0;
for (var i=0 ; i < form_obj['like_bands[]'].options.length ; i++ ) {
if (form_obj['like_bands[]'].options[i].selected) {
count_liked++;
}
}
if (count_liked == 0) {
alert("You must choose a band from the menu.");
return false; // cancel form submission
}
return true; // trigger form submission
}
</script>
<?php
/*if (isset($_POST['like_bands'])) {
header('Location: hw_page2.php');
}*/
?>
</body>
</html>

Display submitted form data after redirecting to the same page

How to redirect after the submit button to the same page? Note: in form i have two button, I want show user only one link, when the user click on it he must be redirected to another link and this link will redirect him to do same page form but this time showing the button for submit.
I'm not familiar with PHP (session) but at least I have an idea how it will work. This is the idea:
<form method="get" action="index.php">
<br>
<h2> ID : <input name="id" placeholder="your Id"/ size="45"/></h2><br>
</br>
<?php if (isset($_SESSION['link'])) {
?>
<input type="submit" name="submit" value="==>Submite<==" id="submit" />
<?php
} else {
?>
Click Here Before Submit
<?php }
?>
</form>
<?php
if(isset($_GET['link'])){
session_start();
{$_SESSION['link'] = $_GET['link'];}
header('location: redirection.php');
}
?>
Page redirection.php should test in index.php if the user clicked on the link if yes so user will be auto redirected to index.php with button submit header("Location: index.php").
exemple website : old-skys.net/cc
<h2> ID : <input name="id" placeholder="your Id"/ size="45"/></h2><br>
</br>
<?php if ($_GET['action']=='step2') {
?>
<input type="submit" name="submit" value="==>Submite<==" id="submit" />
<?php
} else {
?>
Click Here Before Submit
<?php }
?>
Please test it

PHP server redirect

I have been trying to use server side redirect for the drop down menu on my main page. I need the selected option to be remembered using cookies so that it will redirect to the last visited option itself.
This is what I have done so far, but couldn't able to figure it out where I m doing mistake.
<?php
if (isset($_POST['submitted'])){
$newcity=$_POST['city'];
#set cookies
setcookie("city",$newcity,time()+22896000);
}
if ((!isset($_COOKIE['city']) )){
$city = "";
}
else{
header("location:http://example.com/".$_COOKIE['city']."");
}
?>
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method ="POST">
<select name="city">
<option value ="toronto">Toronto</option>
<option value ="ottawa">Ottawa</option>
<option value ="kingston">Kingston</option>
</select>
<input type="submit" name="submitted" value="Submit">
</form>
<?php
if (isset($_POST['submitted'])){
$newcity = $_POST['city'];
setcookie("cookie_city", $newcity, time()+22896000);
}
if ((!isset($_COOKIE['cookie_city']))){
$city = "";
} else{
header("Location: http://example.com/".$_COOKIE['cookie_city']);
exit(0); // <<< Try this
}
?>
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method ="POST">
<select name="city">
<option value ="toronto">Toronto</option>
<option value ="ottawa">Ottawa</option>
<option value ="kingston">Kingston</option>
</select>
<input type="submit" name="submitted" value="Submit">
</form>
Two thoughts:
Try use exit(0) after the redirect.
Use a cookie name different from your POST and GET variables. Maybe your server is configured in some way that overrides POST, GET and cookies.
Your issue is present because you can't access a cookie value on the same page that you set it. Hence why it only works when you reload the page.
You'd be best to redirect them right after the cookie setting:
if (isset($_POST['submitted'])){
$newcity = $_POST['city'];
setcookie("cookie_city", $newcity, time()+22896000);
die(header("Location: http://example.com/".$new_city));
}
if ((!isset($_COOKIE['cookie_city']))){
$city = "";
} else{
header("Location: http://example.com/".$_COOKIE['cookie_city']);
exit(0); // <<< Try this
}
That way, you'll have the cookie set and then user is redirected either way.
Do you still need that end block though? Are they always going to redirect to the city they selected?

Multiple forms on one page, display one at a time with PHP if statements

I have five forms on one page: selectCategorySubmit, selectSoloSubmit, editSoloSubmit, selectGroupSubmit and editGroupSubmit.
I am trying to display one at a time. When the page is opened the first form selectCategorySubmit is displayed and after it is submitted one of two thing should happen:
The selectSoloSubmit form is displayed and after it is submitted the editSoloSubmit form is displayed.
or
The selectGroupSubmit form is displayed and after it is submitted the editGroupSubmit form is displayed.
The problem I'm having is that when either the selectSoloSubmit or selectGroupSubmit form is submitted, it then goes back and displays the selectCategorySubmit form.
I have tried taking out the if(!isset($_POST['editSoloSubmit'])) { and if(!isset($_POST['editGroupSubmit'])) { if statements.
I have also tried multiple conditions for the different if statements like this:
if(!isset($_POST['editSoloLectureSubmit']) && !isset($_POST['selectSoloBiographySubmit']) && !isset($_POST['selectCategorySubmit'])) { but this either doesn't do anything or gives errors that selectedCategory or selectedBiography is not defined.
<?php
if(!isset($_POST['selectCategorySubmit'])) {
?>
<form action="editBiography.php" method="post">
<select name="selectedCategory" size="0">
<?php getCategoryList(); ?>
</select>
<input type="submit" name="selectCategorySubmit" value="Select Biography Category">
</form>
<?php
}
else {
$selectedCategory = $_POST['selectedCategory'];
if($selectedCategory == 'Solo Exhibition') {
if(!isset($_POST['selectSoloSubmit'])) {
?>
<form action="editBiography.php" method="post">
<select name="selectedBiography" size="0">
<?php getSoloList(); ?>
</select>
<input type="submit" name="selectSoloSubmit" value="Select Solo Exhibition">
</form>
<?php
}
else {
if(!isset($_POST['editSoloSubmit'])) {
$selectedBiography = $_POST['selectedBiography'];
$biography = getBiographyDetails($selectedBiography);
?>
<form action="editBiography.php" method="post">
<!-- Form fields go here -->
<input type="submit" name="editSoloSubmit" value="Edit Solo Exhibition">
</form>
<?php
}
}
}
else{
if(!isset($_POST['selectGroupSubmit'])) {
?>
<form action="editBiography.php" method="post">
<select name="selectedBiography" size="0">
<?php getGroupList(); ?>
</select>
<input type="submit" name="selectGroupSubmit" value="Select Group Exhibition">
</form>
<?php
}
else {
if(!isset($_POST['editGroupSubmit'])) {
$selectedBiography = $_POST['selectedBiography'];
$biography = getBiographyDetails($selectedBiography);
?>
<form action="editBiography.php" method="post">
<!-- Form fields go here -->
<input type="submit" name="editGroupSubmit" value="Edit Group Exhibition">
</form>
<?php
}
}
}
}
?>

Jquery form submit with PHP

I need help with what I thought would be a simple form submit problem with Jquery, but am running into issues.
I have a form, #listing_step_1, in which the user first selects a category from a dropdown. When a category has been selected, if the user clicks "next", the subcategory dropdown is revealed. When both a category and subcategory is selected, clicking next will take them to step 2 of the process using a redirect. On submission, the form is reloading the same page using $_SERVER['PHP_SELF'].
I want to autosubmit the form so clicking on next is not required to reveal the subcategories. I'm trying to do this on an onchange event using JQuery, but the following code does not work.
$('#item_category').change(function(){
$('#listing_step_1').submit();
});
I would appreciate any help with this as I am new to form submission using JQuery and AJAX. Manually, it's working fine - i.e., pressing the page button and with the PHP page refresh. I thought by simply submitting the form in the onchange instance it would reload and execute the required actions, but this does not appear to be the case.
Form code:
<form method="post" name="listing_step_1" id="listing_step_1" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" <?php if($errormessage != ""){ echo ' style="margin-top: 30px"';}?>>
<div class="formField fullwidth">
<label for="item_category">Category *</label>
<select id="item_category" name="item_category">
<option value="0">Select category...</option>
<?php $cd = new CategoryListing("<option_credits>", "</option>"); ?>
</select>
</div>
<div class="formField fullwidth">
<label for="item_subcategory">Subcategory*</label>
<?php if($dropdownlist != "") { ?>
<select id="item_subcategory" name="item_subcategory">
<?php echo $dropdownlist; ?>
</select>
<?php } else { ?>
<select id="item_subcategory" name="item_subcategory" disabled="true">
<option value="0">Select subcategory...</option>
</select>
<?php } ?>
</div>
<input type="submit" value="Next" id="submit" name="submit" style="width: 100px;" />
</form>
Form Submission:
if(isset($_POST['submit'])){
if($_POST['item_category'] != 0){
$dropdownlist = CategoryListing::getSubCategoryDropdown($_POST['item_category']);
}
else {
$errormessage = "Please select a category";
}
}
Jquery:
$(document).ready(function(){
$('#item_category').change(function(){
this.form.submit();
});
});
Try this instead:
$(function() {
$('#item_category').change(function(){
this.form.submit();
});
});
in your description, you say:
I have a form, #listing_stage_1, in
which the user first selects a c...
then in code you say:
$('#item_category').change(function(){
$('#listing_step_1').submit();
});
I imagine you are just using the wrong selector... change your code to this:
$('#item_category').change(function(){
$('#listing_stage_1').submit();
});

Categories