Welcome Guest, Not a member yet? Register   Sign In
Adding data to $_POST
#1

[eluser]Majd Taby[/eluser]
Hey, I have a checkbox, I want the checkbox to pass 0 in the POST array if it's not checked, and 1 if it is.

The problem is, checkbox's don't pass anything if they're not selected, as if they're not even in the form. So what i need to do is add it. AFAIK, accessing POST directly is bad practice, so i tried this:

$this->input->post('confidential') = intval($this->input->post('confidential'));

But that gives me an error:

Fatal error: Can't use method return value in write context in /Users/majd/Sites/cap1_wc/trunk/system/application/backend/controllers/BusinessListings.php


What do you guys think?
#2

[eluser]Michael Wales[/eluser]
Why exactly do you feel the need to add data to the post array? The only reason I could think of, is you are posting data directly to the database via the POST array (which is a bad idea).

Regardless, if you want the field in the table to house either 1 or 0 (checked or not), simply assign a default value to that field. Therefore, if you submit data to the database, and checkbox is not defined, it will receive the default value.

1. THE GOOD SOLUTION. Process all of the data you will be inserting into the database, something like this:
Code:
$insert->username = $this->input->post('username');
$insert->password = $this->input->post('password');
if ($this->input->post('checkbox') {
  $insert->checkbox = $this->input->post('checkbox');
}

2. THE BAD SOLUTION. Continue to process the post array openly.
#3

[eluser]Majd Taby[/eluser]
heh, you beat me to it, I was about to edit my post and say that i found a better solution. I modified my library to take extra fields that I specify in my controller, that way I don't have to touch the POST array.

Thanks though




Theme © iAndrew 2016 - Forum software by © MyBB