-3

I have an HTML form that calls a PHP function as:

<form action="test.php" method "get">
user data..
user data
</form>

Now I want to change the functionality as:

 User enters 'yes' : Goto test1.php
 User enters 'no' : Goto test2.php

As this I could not find a way to directly achieve this via HTML. I called javascript from HTML as:

<\input type ="button" onclick="myfunction()" value="submit"/>

In the myfunction() call, I take inputs of the form using

var = document.getElementById('data').value

Now Once user input is parsed. I want to take take all the form data and pass it to a PHP file like:

if var==yes:
    action="test1.php" method="get"
if var==no:
    action="test2.php" method="get"

I have browsed through other answers which suggest Ajax calls. Can it be done without Ajax calls ?

13
  • 1
    It seems far more logical that your test.php handles those choices rather than two separate scripts. Commented Jun 16, 2017 at 0:23
  • Possible duplicate of Send JSON file from jQuery to PHP without AJAX Commented Jun 16, 2017 at 0:23
  • Why does your input have a backslash? Commented Jun 16, 2017 at 0:25
  • @NewToJS Because the '<' of the input and '>' for back quote mix and does not highlight the text Commented Jun 16, 2017 at 0:27
  • 1
    method "get" you realize that is failing, unless what you posted isn't your real code. And the fact about the <\input slash in there, that doesn't make much sense. Commented Jun 16, 2017 at 0:30

4 Answers 4

0

It can easily be done in php.

Html:

<form action="test.php" method="get">
user data..
user data
</form>

test.php:

<?php
if (isset($_GET["somefield"]) && $_GET["somefield"] == "yes")){
header("Location:test1.php");
exit;
} else {
header("Location:test2.php");
exit;
}
?>

This assumes that you have a field in your html called somefield that if it has the value yes than it should navigate to test1.php. The header function used above just redirects to another page.

Sign up to request clarification or add additional context in comments.

2 Comments

method "get" you need to fix that. You did the same mistake as the OP did. And add exit's after each header while you're at it.
@Fred-ii- thanks for pointing out, it was copy of his html that i didn't even notice
0

You have two ways to do that:

  1. Change the value of action in Javascript
  2. You can create an <input type="hidden" name="var"/> with the value of your var

With the second way you just need one php file (test.php) and you can do action 1 if $_GET['var'] == 'yes' else action 2

Comments

0

Well, sorry to ask this but why not use an AJAX call? All you have to do is write the following piece of code and include the jQuery library:

var req = $.ajax({url:"******.php"});

However, if you do not want to use jQuery, you can merge the two PHP classes into one and use an if statement for each case.

$a = $_GET['yourVar'];

if ($a = 'yes') {
...
} else { //i suppose that the variable is a yes/no boolean
...
}

Note that passing a boolean to a PHP class will convert it to string.

Comments

0

The easiest way to do this is using on change event on your element, in this example is a drop down menu.

So, when the user select Yes, then the action attribute will be test1.php, and if they select No, then the form action attribute will be test2.php. Therefore, when the user click on submit button, it will redirect to your specified URL.

Hope this helps.

$(function() {
  $('#myvalue').change(function() {
    var url = 'test1.php';
    if ($(this).val() === 'No') {
      url = 'test2.php';
    }
    $('#myform').attr('action', url);
  });
});
<form id="myform" action="test1.php" method="get">
  <select id="myvalue">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
   </select>
  <input type="submit" value="Submit" />
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.