Search code examples
phppostsession-variables

how to store values in one session in a certain condition


**I want to store data from the $_POST to $_SESSION which fits on certain condition when ever user clicks submit button. and i need to send that certified $_Session array to another page. However, my code is not working right. Could somebody help me on this? :( **

'''

session_start();
$errorFound = 0;

function test_input($data)
{
  $data = trim($data); 
  $data = stripslashes($data);
  $data = htmlspecialChars($data);
  return $data;
}

$cleanData_CUST_name = $_POST['cust']['cust[name]'];


if (isset($_POST['submit_booking'])){
    if (empty($_POST["cust[name]"])) {
        $nameErr = "Name is required";
        $errorFound++;
      } else {
            $name = test_input($_POST["cust[name]"]);
            if (!preg_match("/^[a-zA-Z ]*$/", $name)){
                $nameErr = "Only letters and whitespace are allowed.";
                $errorFound++;
       }if($errorFound ==0){
        $_SESSION['CUST'] =  $_POST['cust[name]']; 
       }
}

'''


Solution

  • if (isset($_POST['submit_booking'])){
    if (empty($_POST["cust[name]"])) {
        $nameErr = "Name is required";
        $errorFound++;
      } else {
            $name = test_input($_POST["cust[name]"]);
            if (!preg_match("/^[a-zA-Z ]*$/", $name)){
                $nameErr = "Only letters and whitespace are allowed.";
                $errorFound++;
       }if($errorFound ==0){
        $_SESSION['CUST'] =  $_POST['cust[name]']; 
       }
    }
    }
    

    you're missing to close a semicolon for if (isset($_POST['submit_booking'])){