Home > Blockchain >  Getting blank page on submitting form
Getting blank page on submitting form

Time:11-24

I am pretty new to php and I am obtaining a blank page while submitting a form. Also no data is being added onto the database. Each time an expense record is added, the values for amount, date, id(user id) and cat_id should be added into the myexpense table.

Database my database structure

HTML

<form action="newexpense.php" method="post">

    <label for="amount"><i class="fas fa-money-bill"></i>&nbsp;<strong>Amount (Rs)</strong></label><br>
    <input type="number" id="amount" name="amount" placeholder="Input the amount spent"><br><br> 

    <p><i class="fas fa-clipboard-list"></i>&nbsp;<strong>Please select the category:</strong></p>
    <input type="radio" id="cat1" name="category" value="Car">
    <label for="cat1">Car</label><br>
    <input type="radio" id="cat2" name="category" value="Gifts">
    <label for="cat2">Gifts</label><br>  
    <input type="radio" id="cat3" name="category" value="Groceries">
    <label for="cat3">Groceries</label><br>
    <input type="radio" id="cat4" name="category" value="House">
    <label for="cat4">House</label><br>
    <input type="radio" id="cat5" name="category" value="Medicine">
    <label for="cat5">Medicine</label><br>
    <input type="radio" id="cat6" name="category" value="Travel">
    <label for="cat6">Travel</label><br>
    <input type="radio" id="cat7" name="category" value="Other">
    <label for="cat7">Other</label><br><br>  

    <label for="date"><i class="fas fa-calendar-alt"></i>&nbsp;<strong>Date</strong></label><br>
    <input type="date" id="date" name="date"><br><br><br> 
    
    <p class="b-option">
    <input type="submit" name="submit" value="Add Expense" class="loginbtn">
    </p>
</form>

PHP

<?php

session_start();

if(isset($_SESSION['id']) && isset($_SESSION['username'])) {

include "db.php";

if (isset($_POST["submit"])) {
    $amount = $_POST["amount"];
    $date = $_POST["date"];
    $category = $_POST["category"];      

    $cat_id = "";
    switch($category) {
      case "Car":
        return $cat_id = 1;
        break;
      case "Gifts":
        return $cat_id = 2;
        break;
      case "Groceries":
        return $cat_id = 3;
        break;
    }
  
  
    $sql = "INSERT INTO myexpense (amount, date, id, cat_id)
    VALUES ('".$amount."', '".$date."', '".$_SESSION['id']."', '".$cat_id."')";
  
    if ($conn->query($sql) === TRUE) {
      header("location: newexpensesuccess.php");
    }else {
      $message = "Error: " . $sql . "<br>" . $conn->error;
    }
      
  }
  
?>

CodePudding user response:

When you do a return in your switch statement, no code below it will be executed, so you query won't execute too. Remove the return statements before the variable assignments. Also the last break is redundant.

switch($category) {
    case "Car":
        $cat_id = 1;
        break;
    case "Gifts":
        $cat_id = 2;
        break;
    case "Groceries":
        $cat_id = 3;
}

CodePudding user response:

In your script you have a switch case statement. And the case will be true you assign the variable $cat_id = xxxx; Then you set return. The return statement aborts the script at this point. Use instead break; And that's the reason why you don't get any output. The correct use of the switch-case command is:

switch($var) {
  case '1': 
       $var = 'something';
       break;
  case '2': 
       $var = 'something';
       break;
  default: 
       // do something to avoid error or something else.
       $var = 'default Value';
       // or
       exit(); // because nothing matched

  •  Tags:  
  • php
  • Related