-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 ?

john
  • 335
  • 3
  • 12
  • 1
    It seems far more logical that your `test.php` handles those choices rather than two separate scripts. – Marty Jun 16 '17 at 00:23
  • Possible duplicate of [Send JSON file from jQuery to PHP without AJAX](https://stackoverflow.com/questions/43366068/send-json-file-from-jquery-to-php-without-ajax) – Noah Cristino Jun 16 '17 at 00:23
  • Why does your input have a backslash? – NewToJS Jun 16 '17 at 00:25
  • @NewToJS Because the '<' of the input and '>' for back quote mix and does not highlight the text – john Jun 16 '17 at 00: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. – Funk Forty Niner Jun 16 '17 at 00:30
  • Possible duplicate of [How to redirect to another webpage in JavaScript/jQuery?](https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage-in-javascript-jquery) – chris85 Jun 16 '17 at 00:31
  • I'm not convinced any of this works, can you maybe create a jsfiddle for people to look at and maybe run on a localhost? – NewToJS Jun 16 '17 at 00:32
  • This all looks like pseudo code to me. The answer to `can it be done without ajax` is yes. See the above thread and use an `if` in JS to check the selected value. – chris85 Jun 16 '17 at 00:33
  • @chris85 not sure how that duplicate post relates to this.... This is obviously about posting form data to another page hosted on the same host/server/domain. Your duplicate post is about redirecting.... not posting data and dealing with that data. – NewToJS Jun 16 '17 at 00:33
  • @NewToJS That's how he/she can change the location of a submission. If AJAX isn't wanted than the request must be sent. – chris85 Jun 16 '17 at 00:35
  • @chris85 Ah right! I'm with you now. I see, sorry I read the OP's source again and it's the choice of approach/method wanted that has confused me but I understand what the OP is wanting to do now. Not sure why the choice of this method, very strange but yes redirection combined with the method above would no doubt work. – NewToJS Jun 16 '17 at 00:38
  • well, I'm not going to dupe hammer this, I'll just place my own vtc. – Funk Forty Niner Jun 16 '17 at 00:39
  • Possible duplicate of [How to make a redirect in PHP?](https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – Kyll Jun 18 '17 at 15:34

4 Answers4

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

Kaenn
  • 76
  • 3
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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Isac
  • 1,834
  • 3
  • 17
  • 24
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.

treecon
  • 2,415
  • 2
  • 14
  • 28
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>