-2

In my form there are multiple submit buttons and each submit button should redirect the page to same location but the content changes based on the session variable set. But writing isset(); for each submit button is difficult. So I am trying to write a loop where foreach input type submit the loop should write the isset(); function.

My form is like:

<form method="POST">
    <input type="submit" name="type1" value="type1" > <br/>
    <input type="submit" name="type2" value="type2" >
</form>

And I have tried to mix up JQUERY with PHP to achieve this, like:

<?php
  echo '<script>
  $("input:submit").each(function(){
  var inputName = $(this).attr("name");' ;
                    
  $inputName = print 'document.write(inputName);';
            
  if(isset($_POST[$inputName])){
    $_SESSION["value"] = $inputName;
    header('Location:somepage.php');
  } 
            
  echo '  });
        </script>';
        
?>

But this is not working.

Kindly help me to achieve the loop foreach input which has type as submit.

  • 1
    You can't write PHP in JavaScript. PHP runs on the server, JS runs on the client. – Barmar Jun 14 '21 at 17:50
  • Does this answer your question? [How do I pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) – Kinglish Jun 14 '21 at 17:51
  • In the submit button you can put a `formaction` atttribute. It will then submit to that URL instead of the action of the form. – Barmar Jun 14 '21 at 17:51
  • Your loop is redirecting to the same `somepage.php` for each submit button. I thought you said each button should redirect to a different location. – Barmar Jun 14 '21 at 17:53
  • @Barmar, I tried achieving my aim mixing jquery and php. Now I am looking on how to achieve this using php only. But I am stuck!! – user16225210 Jun 14 '21 at 17:56
  • @Barmar, yes like the page redirect's to same page but the content changes based on the session variable set. Sorry for that!! – user16225210 Jun 14 '21 at 17:57
  • Why do you give them different names? Why not use the same name but a different value? – Barmar Jun 14 '21 at 18:00
  • Only one submit button can be clicked and they all have a different value. Just use an array and get the value. – AbraCadaver Jun 14 '21 at 18:04
  • is that help you https://stackoverflow.com/a/67959051/13239969 – Adan Jun 14 '21 at 18:24

3 Answers3

-1

Use an associative array that lists all the possible submit button names. Check for it and then set the session variable.

$redirects = ['type1', 'type2'];

foreach ($redirects as $name) {
    if (isset($_POST[$name])) {
        $_SESSION['value'] = $name;
        break;
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Yes, this would also work. But again here I have to type each and every type in the array, right? – user16225210 Jun 14 '21 at 18:18
  • Yes. You have to provide the list somewhere. You could use the same array when creating the buttons in the HTML. – Barmar Jun 14 '21 at 18:20
-1

you can use php script only example :

   <?php
   if(isset($_POST['type'])){
      $a = $_POST['type'];
       foreach($a as $b){
       $c = preg_replace("/[^a-z0-9]+/i", "-", $b);
    }
    $type = rtrim($c, '-'); //output is type-num ex type-1
    $_SESSION["value"] = $type;
    if(isset($_SESSION['value'])){
      //  header('Location : page.php');
       }
    }
//cahnge 10 to how many input you need
echo'<form method="POST">';
for ($x = 0; $x <= 10; $x++) {
    $name = "type[$x]";
echo'<input type="submit" name="'.$name.'" value="'.$name.'" > <br/>';
}
echo'</form>';
il4mb
  • 105
  • 1
  • 5
-1

Since only one submit button can be submitted, just use the same name for them all and whatever distinct value you want:

<form method="POST">
    <input type="submit" name="type" value="type1" > <br/>
    <input type="submit" name="type" value="type2" >
</form>

<?php
        
  if(isset($_POST["type"])){
    $_SESSION["value"] = $_POST["type"];
    header('Location:somepage.php');
  } 

So $_SESSION["value"] will equal type1 or type2 based on the submit button value in this scenario.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87