Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default value for $_POST[];

Tags:

html

sql

php

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?

like image 646
Tim Levinsson Avatar asked Dec 01 '15 20:12

Tim Levinsson


People also ask

What is $_ POST [] in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post".

What is the default value of variable in PHP?

1 Answer. Show activity on this post. Variables that are declared without a value and undefined/undeclared variables are null by default.

How does $_ POST work?

The $_POST variable collects the data from the HTML form via the POST method. When a user fills the data in a PHP form and submits the data that is sent can be collected with the _POST Method in PHP. The Post Method transfers the information via the Headers.

Which variable will be used to pick the value using post method?

The $_REQUEST variable The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.


1 Answers

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.

like image 200
DFriend Avatar answered Sep 20 '22 11:09

DFriend