1

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?

4 Answers4

4

To write in short way

$action = isset($_POST['hp'])?'action2':'action1';

That's all.

Оzgur
  • 432
  • 2
  • 10
1

Differents possibilities:

Same action and redirect

The most easy way probably is send form to the same PHP file, and in this file, get the content of this input via POST and redirect to the correct file.

Prevent default submit and add submit event via JavaScript

The second option, may be add an event to form on submit via JavaScript, prevent default action to prevent the submit, then check value of input, set action and submit form:

<form name="contactForm" id="contactForm" method="post" action="">
    ...
</form>

<script>
    document.querySelector('#contactForm').addEventListener('submit', function(e) {
        //Prevent default submit
        e.preventDefault();

        //Check input value
        if (...) {
            this.action = "page1.php";
        } else if (...) {
            this.action = "page1.php";
        }

        //perform submit form.
        this.submit();
    });
</script>

Use data binding library

This is probably the best form to do it, but the most complicated to understad, this form is based of use a data binding library like Vue.js, KnockoutJS or RactiveJS to set in model object the action string depending of input value.

Then, in HTML form tag, set in action the value of model data using the binding syntax of the chosen library:

//Vue.js syntax
<form name="contactForm" id="contactForm" method="post" :action="action">

//Ractive.js syntax
<form name="contactForm" id="contactForm" method="post" action="{{action}}">

What do I recommend?

If you're novel with PHP and don't know JavaScript, the first option is probably the best for you, if you If you know JavaScript and you know how to work with events, but never used a binding library, probably the second option is more recommended for you.

If you worked with some data binding library (or framework that implements data binding like Angular), the third options is probably the best for you.

Ivan Montilla
  • 392
  • 4
  • 19
0

If the 2nd and 3rd versions don't work, you must be missing an input like:

<input type="submit" name="submit">

You can either add that button to the form, or you can change your code to use if isset($_POST['hp'])

<?php   
  $action = "";
  if(isset($_POST['hp'])){ 
    $input = ($_POST['hp']); 
    if($input == "") {
      $action = "action1";
    } else {
      $action = "action2";
    }
  }    
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This didn't work for me - for the same reason my third example didn't work - it only pulls in the `$action = "";` value on page load and doesn't pull in the value of `$_POST['hp'])` on form submit, so it always goes to action1. – Brittany Layne Rapheal Apr 17 '17 at 18:44
0

None of the answers above worked for me, so here is what I ended up going with.

Use Case:

  1. I have a "Search" form with two fields. When a user clicks "Search", these values should be added to the URL, so that previous searches may be bookmarked. The search should then be performed.
  2. When the page is first loaded or no search criteria are used, all possible results should be shown.

Code description:

  • At the top of the file, check if the user submitted the form. If so:
    • get the values from the fields you want and save them to local variables.
    • navigate to the same PHP file, this time passing the variables you are interested in
  • If the user did not submit the form:
    • check for URL parameters
    • If there are URL parameters present, save them to local variables, and use these local variables to run the search

Code Snippet:

  if(isset($_POST['submit'])){
    if(!empty($_POST['projectNameSearch'])){
      $projectNameSearch = mysqli_real_escape_string($conn, $_POST['projectNameSearch']);
    }
    if(!empty($_POST['projectDescriptionSearch'])){
      $projectDescriptionSearch = mysqli_real_escape_string($conn, $_POST['projectDescriptionSearch']);
    }
    if($projectNameSearch != '' || $projectDescriptionSearch != ''){
      header("Location: projects.php?projectnamesearch=$projectNameSearch&projectdescriptionsearch=$projectDescriptionSearch");
    }
  } else {
    if(isset($_GET['projectnamesearch'])){
      $projectNameSearch = mysqli_real_escape_string($conn, $_GET['projectnamesearch']);
    }
    if(isset($_GET['projectdescriptionsearch'])){
      $projectDescriptionSearch = mysqli_real_escape_string($conn, $_GET['projectdescriptionsearch']);
    }
  }
Fall Bay
  • 1
  • 1