• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bug when setting $_POST array field to null?

#1
I have some array fields in my html ie:
<input type="text" name="sold_for_price[]">
<input type="text" name="sold_for_price[]">
<input type="text" name="sold_for_price[]">
etc
which is not a mandatory fields, which means the first one can be 12.00, second one can be empty, third one can be $1.25 etc

so when submitting to post it becomes:

$_POST['sold_for_price'] = array('12.00', '', '1.25');

however in mysql, money type cannot be empty string, so I'll have to manually convert it into null. ie:

if (strlen($_POST['sold_for_price'][i]) == 0) $_POST['sold_for_price'][i] = null; //if field is not entered, I'll change the value to null

however, changes won't reflect in $this->input->post('sold_for_price')[1]

but if I do: $_POST['some_other_money_field'] = null, then $this->input->post('some_other_money_field') will be null.
Reply

#2
(10-26-2015, 02:16 PM)redjersey Wrote: I have some array fields in my html ie:
<input type="text" name="sold_for_price[]">
<input type="text" name="sold_for_price[]">
<input type="text" name="sold_for_price[]">
etc
which is not a mandatory fields, which  means the first one can be 12.00, second one can be empty, third one can be $1.25 etc

so when submitting to post it becomes:

$_POST['sold_for_price'] = array('12.00', '', '1.25');

however in mysql, money type cannot be empty string, so I'll have to manually convert it into null. ie:

if (strlen($_POST['sold_for_price'][i]) == 0) $_POST['sold_for_price'][i] = null; //if field is not entered, I'll change the value to null

however, changes won't reflect in $this->input->post('sold_for_price')[1]

but if I do: $_POST['some_other_money_field'] = null, then $this->input->post('some_other_money_field') will be null.


PHP Code:
$_POST['sold_for_price'] = array('12.00''0.00''1.25'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
Hi,

Just a suggestion. Maybe its not necessary but you can also use HTML5 input type number and set value to 0.

View Page
Code:
<form method="post" action="<?php echo base_url('yourController/methodName'); ?>">
   <input type="number" min="0" name="sold_for_price[]" value="0" />
   <input type="number" min="0" name="sold_for_price[]" value="0" />
   <input type="number" min="0" name="sold_for_price[]" value="0" />
   <button type="submit">Go</button>
</form>
This will help you on the validation to be quick and a little convenient. (e.g. to prevent unnecessary characters)


Regarding what you have mentioned.

Quote:however, changes won't reflect in $this->input->post('sold_for_price')[1]


but if I do: $_POST['some_other_money_field'] = null, then $this->input->post('some_other_money_field') will be null.

You can store it to a variable and manipulate that variable to tailor your needs. For my side, I do it this way. But it depends on your side also.

PHP Code:
public function index() {
 
   if($_POST) {
 
       $input $this->input->post();
 
       $soldForPrice $input['sold_for_price'];
 
       $index 0;
 
       
        foreach
($soldForPrice as $price) {
 
           if($price == null) {
 
               $soldForPrice[$index] = null;
 
           }
 
           $index++;
 
       }
 
       $this->prePrintR($soldForPrice);
 
 
       echo $soldForPrice[0] + $soldForPrice[1];
 
       //call your model method and pass your $soldForPrice here
 
   }
 
   $this->load->view('samples/test');
}

/**
* Customized Helper to pre print an array (expression)
* @param $expression array || mix
*/
private function prePrintR($expression) {

 
  echo '<pre>';
 
  print_r($expression);
 
  echo '</pre>';



Please see the attachment for clarification;
       

Thank you. I hope I have helped you a little bit.
Big Grin
Joseph K. Dicdican
Softshore Global Solutions Inc. | Junior Web Developer Intern
Passerelles numériques Philippines | Scholar
[email protected] 
Reply

#4
It can't set the sold_for $ field as $0. $0 means the item is sold for $0 (which it happens), not entering means the item is not yet sold.

Of course I can get away with this by creating a checkbox named "sold?" and only display the sold for money field if checkbox is checked. My point is that there is a bug in codeigniter.

Anyways, to temporary fix this I just used $_POST and xss_clean()
Reply

#5
You're not showing the actual code, but the lines that you did show have "i" as the index that you're setting to NULL and 1 as the one you're trying to fetch afterwards ...
Reply

#6
(10-27-2015, 06:40 AM)Narf Wrote: You're not showing the actual code, but the lines that you did show have "i" as the index that you're setting to NULL and 1 as the one you're trying to fetch afterwards ...

sorry for not showing the actual code as it's quite long.

so basically line:
if (strlen($_POST['sold_for_price'][i]) == 0) $_POST['sold_for_price'][i] = null;


is within a for loop, it will check for each sold_for_price array field, if it is not entered (ie. strlen = 0), it will be set to null


so in my example, $_POST['sold_for_price'][1] will be null (the second field is not entered), what I'm saying is, if $_POST['sold_for_price'][1] is manually set to null, $this->input->post('sold_for_price')[1] won't return null


Again this only happens in array field, ie. if I do $_POST['total_price'] = null, $this->input->post('total_price') is also null.
Reply

#7
(10-27-2015, 01:50 PM)redjersey Wrote:
(10-27-2015, 06:40 AM)Narf Wrote: You're not showing the actual code, but the lines that you did show have "i" as the index that you're setting to NULL and 1 as the one you're trying to fetch afterwards ...

sorry for not showing the actual code as it's quite long.

so basically line:
if (strlen($_POST['sold_for_price'][i]) == 0) $_POST['sold_for_price'][i] = null;


is within a for loop, it will check for each sold_for_price array field, if it is not entered (ie. strlen = 0), it will be set to null


so in my example, $_POST['sold_for_price'][1] will be null (the second field is not entered), what I'm saying is, if $_POST['sold_for_price'][1] is manually set to null, $this->input->post('sold_for_price')[1] won't return null


Again this only happens in array field, ie. if I do $_POST['total_price'] = null, $this->input->post('total_price') is also null.

Did you actually try manually setting $_POST['sold_for_price'][1] to null or are you just concluding that it has no effect because it doesn't work within your loop? "i" is a literal string index ...
Reply

#8
what I meant is, when manually setting $_POST['sold_for_price'][1] to null, it won't reflect changes on $this->input->post('sold_for_price')[1] (empty string) (forget about the loop if it confuses you).
Reply

#9
The loop doesn't confuse me, you just consistently wrote "i" instead of "1" in your posts.

And I can't really reproduce this behavior:

Code:
$ cat application/controllers/Test.php
<?php
class Test extends CI_Controller {

    public function index()
    {
        var_dump($this->input->post('foo')[1]);
        $_POST['foo'][1] = 'bar';
        var_dump($this->input->post('foo')[1]);
        $_POST['foo'][1] = NULL;
        var_dump($this->input->post('foo')[1]);
    }

}
$ php index.php test/index
NULL
string(3) "bar"
NULL
$

My bet is on a simple typo somewhere in your code, but you're still not really showing it, so nobody could possibly spot it.
Reply

#10
(10-28-2015, 10:53 AM)redjersey Wrote: what I meant is, when manually setting $_POST['sold_for_price'][1] to null, it won't reflect changes on $this->input->post('sold_for_price')[1] (empty string) (forget about the loop if it confuses you).

He wasn't confused, the code you posted was wrong. This is why people like to see code which demonstrates the issue before they investigate further. If you are actually using the code you posted, you should be getting errors beyond what you are reporting. The difference between i and $i is a minor typo, but it's significant.

It should also be noted that structuring your loop in certain ways might cause a problem like this.

Unless you're using PHP 5.3 or less, the difference between $this->input->post('sold_for_price')[1] and $_POST['sold_for_price'][1] when reading the value is essentially an isset() check, unless you have global XSS filtering enabled (in which case you may or may not receive other errors).

Finally, while I'm interested in determining what is going on here, I do feel the need to say that you would be better off not modifying the $_POST data in the first place. it would be better to create a variable (or set of variables) to hold your filtered $_POST data and pass the filtered data instead of the $_POST data. I do this to make sure I'm only passing the fields I actually use in my model, make sure each field is set to reasonable defaults for the data type if nothing was submitted (and the field isn't required), etc.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.