I'm working on a form that I'd like to change the form action based off of the value of an input on form submit. This needs to be accomplished using PHP.
Here's what I've tried so far:
<?php
$action = "";
$input = (isset($_POST["hp"]));
if($input == "") {
$action = "action1";
} else {
$action = "action2";
}
?>
<form name="contactForm" id="contactForm" method="post" action="<?php echo $action; ?>">
<!-- form stuff here -->
<input id="hp" name="hp" type="text" class="hp"/>
<input type="submit" name="submit" id="submit" value="Submit Query" class="button" />
</form>
This doesn't work because the hp
field for (isset($_POST["hp"]))
doesn't have a value from the get-go, so it always goes to action1.
I've also tried:
<?php
if(isset($_POST['submit'])){
$input = ($_POST['hp']);
$action = "";
if($input == "") {
$action = "action1";
} else {
$action = "action2";
}
}
?>
<form name="contactForm" id="contactForm" method="post" action="<?php echo $action; ?>">
That didn't work because Perch (the CMS this is being built on) throws you an error that $action
isn't defined yet.
And when I tried:
<?php
$action = "";
if(isset($_POST['submit'])){
$input = ($_POST['hp']);
if($input == "") {
$action = "action1";
} else {
$action = "action2";
}
}
?>
It didn't do anything at all on submit because it set the action as "".
Any suggestions?