Tim Levinsson
Tim Levinsson

Reputation: 597

default value for $_POST[];

I have a problem with default value for $_POST[]; So i have a html form with textboxes and the informations is sent to a php script. The php script has a sql query that is being sent to my database. But if my textbox in html form is empty the query doesnt have a value. So i want to set my post to a default value 0 so it returns a value atleast.

So here is an example of html form (This is not my actuall script. Just an example.

<form action="testscript.php" method="POST">
    <input type="id" name="ID"/>
    <input type="text" name="test"/>
    <input type="submit" value="Send"/>
</form>

Ok so this script will send both id and test textboxes will always have a number value. And it sends the information to testscript.php

Here is testscript.php example

    $conn = mysqli_connect('host', 'dbuser', 'dbpass', 'dbname');
    $id = $_POST['id'];
    $test = $_POST['test'];
    $sql = "INSERT INTO test_table (id, test) VALUES ($id, $test)";

    if (mysqli_query($conn, $query)) {
        echo "Success";
    } else {
        echo "Failed" . mysqli_error($conn);
    }

Alright so now if i submit my html form to php script without inserting any text to the textboxes the query will look like this

INSERT INTO test_table (id, test) VALUES ( , )

But the query should be like this

INSERT INTO test_table (id, test) VALUES (0, 0)

So. I know i can use value attribute in the html tag but then the value will be visible in the textbox and i dont want that.

And i know i can do an if statment to make a default value like this

if (isset($_POST['test'])) {
    $test = $_POST['test'];
} else {
    $test = 0;
}

But now the problem is that i would have to do that if statment for every textbox and my html form have more than 100 textboxes. So i dont want to make an if statment for every textbox because then my script will be way to big and it will take hours.

So is there any way to set a default value for all the textboxes without using if statment in php or value attribute in html form?

Upvotes: 7

Views: 11496

Answers (3)

Confiance
Confiance

Reputation: 926

You can easily use null check and define your default value like this :

$name = $_POST['name'] ?? 'John';

in my case the default value is John if the name is not defined. It gives the same result like this :

 $name = isset($_POST["name"]) ? $_POST["name"] : 'John';

Upvotes: 0

DFriend
DFriend

Reputation: 8964

I know it seems like a pain but you MUST check that all inputs are valid. You can simplify the amount of code by using a ternary operator like this.

$id = isset($_POST['id']) ? $_POST['id'] : 0;
$test = isset($_POST['test']) ? $_POST['test'] : 0;

....

And no, it won't take hours even with hundreds of them.

To make this slightly less painful to code you can use the power of looping with PHP's variable variables

The most painful part will be creating an array with all your field names

$fields = array('id', 'test', 'extra', 'more', ..., 'one_hundred');

Then loop through that array creating variable names and at the same time escaping the strings - if they are there - otherwise set a value of 0 (zero). You might want/need to set this to "" (empty string)

foreach($fields as $field_name)
{
   ${$field_name} = isset($_POST[$field_name]) ? mysqli_real_escape_string($conn, $_POST[$field_name]) : 0;
}

You now have the variables $id, $test, $extra, $more, ...., $one_hundred available for your use.

Upvotes: 8

Dimitri
Dimitri

Reputation: 1966

If your checkboxes have unique names, then you'll need to check them on the server side to see if they actually have values in them one by one by using the ternary

isset($_POST["test"]) ? $_POST["test"] : 0

However, if your checkboxes are in array form:

<input type="checkbox" name="courses[]" value="1">
<input type="checkbox" name="courses[]" value="2 >

Then you could do the following:

foreach($_POST['courses'] as $course) {
    echo $course; // etc etc etc 
}

You can also set database defaults.

Another note, your code is prone to SQL injection. Although the question you have might simply be an example, you might just keep in mind there are better and safer ways of querying a database see PDO connections.

Upvotes: 0

Related Questions