Execute more submit in sequence in same page

Viewed 60

I have that code and it work fine!

session_start();
if(isset($_POST['submit1'])){
    $_SESSION['submit1']=true;
}

if(@$_POST['user_token'] && @$_SESSION['submit1']) {
   if($_SESSION['user_token'] == $_POST['user_token']) {
       if (isset($_POST['submit3'])) {
                unset($_SESSION['user_token']);
                $_SESSION['submit1']=false;
        include_once('./my3page.php');
       }
       include_once('./searchname.php');
   } else {
      header("location: ./index.php");
   }
} else {
    include_once('./token.php');
    include_once('./searchform.php');
}

Now i would like to add other submit session (because i have 2 search type)

WHAT I TRIED

index.php

session_start();
 //---------------FIRST SEARCH TYPE--------------------------------
if(isset($_POST['submit1'])){
    $_SESSION['submit1']=true;
}

if(@$_POST['user_token'] && @$_SESSION['submit1']) {
   if($_SESSION['user_token'] == $_POST['user_token']) {
       if (isset($_POST['submit3'])) {
        unset($_SESSION['user_token']);
                $_SESSION['submit1']=false;
        include_once('./my3page.php');
       }
       include_once('./searchname.php');//PAGE WITH SUBMIT3
   } else {
      header("location: ./index.php");
   }
}   

//---------------SECOND SEARCH TYPE------------------------
    if(isset($_POST['submit2'])){
    $_SESSION['submit2']=true;
}

if(@$_POST['user_token'] && @$_SESSION['submit2']) {
   if($_SESSION['user_token'] == $_POST['user_token']) {
       if (isset($_POST['submit4'])) {
        unset($_SESSION['user_token']);
        $_SESSION['submit2']=false;
        include_once('./my3page.php');
       }
       include_once('./searchdate.php');//PAGE WITH SUBMIT4
   } else {
      header("location: ./index.php");
   }
} 

//------------------------
else {
    include_once('./token.php');
    include_once('./searchform.php'); //FIRST PAGE THAT CONTAINS 2 FORM AND 2 SUBMIT BUTTONS, AS I WROTE BELOW
}

searchform.php

   IF YOU SEE THIS IS SEARCHFORM
    <form method="post">
    <input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token'];?>" />
    <input name="submit1" type="submit" value="Search1"/></td>
    </form>
    <form method="post">
    <input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token'];?>" />
    <input name="submit2" type="submit" value="Search2"/>
    </form>

token.php

$form_token = uniqid();
$_SESSION['user_token'] = $form_token;

searchdate.php

IF YOU SEE THIS IS SEARCHDATE
<form method="post">
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token'];?>">
<input name="submit4" type="submit" value="OK2"/>

searchname.php

IF YOU SEE THIS IS SEARCHNAME
<form method="post">
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token'];?>"><br>
<input align="left" name="submit3" type="submit" value="OK1"/>

my3page.php

MY3PAGE reached

PROBLEMS

  • If i press submit1 it execute also include_once('./searchdate.php');
  • If i press submit2 it execute also include_once('./searchform.php');

Why?

1 Answers

Please try this code. You seach form will now included only if submit1 and and submit2 not pressed. Comments added in appropriate places.

<?php
error_reporting(E_ALL);
ini_set('display_errors', false);
?><!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Ricerca</title>
    </head>

    <body>
        <?php
        session_start();
        if (isset($_POST['submit1'])) {
            $_SESSION['submit1'] = true;
            $_SESSION['submit2'] = false; //you can remove if this is not needed.
        }
        if (isset($_POST['submit2'])) {
            $_SESSION['submit2'] = true;
            $_SESSION['submit1'] = false; //you can remove if this is not needed.
        }

        //---------------FIRST SEARCH TYPE--------------------------------
        if (@$_POST['user_token'] && @$_SESSION['submit1']) {
            if ($_SESSION['user_token'] == $_POST['user_token']) {
                if (isset($_POST['submit3'])) {
                    unset($_SESSION['user_token']);
                    $_SESSION['submit1'] = false;
                    include_once('./my3page.php');
                }
                else { //include only if not submit 3 not pressed.
                    include_once('./searchname.php'); //PAGE WITH SUBMIT3
                }
            } else {
                header("location: ./index.php");
            }
        }else if (@$_POST['user_token'] && @$_SESSION['submit2']) {
            if ($_SESSION['user_token'] == $_POST['user_token']) {
                if (isset($_POST['submit4'])) {
                    unset($_SESSION['user_token']);
                    $_SESSION['submit2'] = false;
                    include_once('./my3page.php');
                }
                else { //include only if not submit4 not pressed.
                    include_once('./searchdate.php'); //PAGE WITH SUBMIT4
                }
            } else {
                header("location: ./index.php");
            }
        }
        //Include only if both submit1 and submit2 not pressed.
        else {
            include_once('./token.php');
            include_once('./searchform.php'); //FIRST PAGE THAT CONTAINS 2 FORM AND 2 SUBMIT BUTTONS, AS I WROTE ABOVE, THAT I SEE WHEN I PRESS SUBMIT1 (WHY?)
        }
        ?>
    </body>
</html>
Related