ali_2kool2002 Posted February 19, 2007 Share Posted February 19, 2007 Hi can anyone tel me why the code below wont check if the array is empty im using the if statement as below in the main code else if( empty ( $_POST['txtbox'] ) ) { echo "nothings entered into any input text box"; } if i take the if statement it works but i need to somehow say if someone click submit button and non of the input boxes have any data then should say "nothins chosen" ...ne help guys ?? (another thing can some1 tel me how to check the current size of the array ?) <?php session_start(); $PHPSESSID = session_id(); require_once('mysql_connect.php'); if (isset($_POST['submitted'])) { //if ( ! empty ( $_POST['max']) ) //{ //echo "$_POST['max']"; //} if ( ! empty ( $_POST['txtbox'] ) ) { foreach ( $_POST['txtbox'] AS $key => $value ) { //echo $key . ' = ' . $value . '<br />'; if($value == ""){ //echo "non chosen <br />"; } else{ echo $key . ' = ' . $value . '<br /> added to cart'; } } } else if( empty ( $_POST['txtbox'] ) ) { echo "nothings chosen"; } } ?> Link to comment https://forums.phpfreaks.com/topic/39182-solved-how-to-check-if-empty-array/ Share on other sites More sharing options...
printf Posted February 19, 2007 Share Posted February 19, 2007 you have it following a foreach(), so you can't use elseif / else if on a foreach() <?php session_start(); $PHPSESSID = session_id(); require_once('mysql_connect.php'); if (isset($_POST['submitted'])) { //if ( ! empty ( $_POST['max']) ) //{ //echo "$_POST['max']"; //} if ( ! empty ( $_POST['txtbox'] ) ) { foreach ( $_POST['txtbox'] AS $key => $value ) { //echo $key . ' = ' . $value . '<br />'; if($value == ""){ //echo "non chosen <br />"; } else{ echo $key . ' = ' . $value . '<br /> added to cart'; } } } else if( empty ( $_POST['txtbox'] ) ) { echo "nothings chosen"; } } ?> Link to comment https://forums.phpfreaks.com/topic/39182-solved-how-to-check-if-empty-array/#findComment-188694 Share on other sites More sharing options...
flappy_warbucks Posted February 19, 2007 Share Posted February 19, 2007 Hi, what i would do is just do this: if (!isset($_POST) | sizeof($_POST) == 0) { //action depending on empty array } Link to comment https://forums.phpfreaks.com/topic/39182-solved-how-to-check-if-empty-array/#findComment-188696 Share on other sites More sharing options...
tom100 Posted February 19, 2007 Share Posted February 19, 2007 if (count($_POST['txtbox']) == 0) { echo "Nothing chosen"; } Also your else if statement should just be an else. Link to comment https://forums.phpfreaks.com/topic/39182-solved-how-to-check-if-empty-array/#findComment-188699 Share on other sites More sharing options...
printf Posted February 19, 2007 Share Posted February 19, 2007 Just to add.. But you really don't need the else if(?), because else would be good enough, because you already tested ! empty(), so if that doesn't return true, then it's empty, so no need for else if()! Also if the form is input='text', the array will still be filled, just the element_key will be empty. Link to comment https://forums.phpfreaks.com/topic/39182-solved-how-to-check-if-empty-array/#findComment-188701 Share on other sites More sharing options...
ali_2kool2002 Posted February 19, 2007 Author Share Posted February 19, 2007 THANKSS GUYS!!! :) :) Link to comment https://forums.phpfreaks.com/topic/39182-solved-how-to-check-if-empty-array/#findComment-188704 Share on other sites More sharing options...
ali_2kool2002 Posted February 19, 2007 Author Share Posted February 19, 2007 HI Iv tried that but nothings being displayed ...have i entered you if statement in the rite place? <?php session_start(); $PHPSESSID = session_id(); require_once('mysql_connect.php'); if (isset($_POST['submitted'])) { if ( ! empty ( $_POST['txtbox'] ) ) { foreach ( $_POST['txtbox'] AS $key => $value ) { if($value == ""){ } else{ echo $key . ' = ' . $value . '<br /> added to cart'; } } } } if (count($_POST['txtbox']) == 0) { echo "Nothing chosen"; } ?> Link to comment https://forums.phpfreaks.com/topic/39182-solved-how-to-check-if-empty-array/#findComment-188706 Share on other sites More sharing options...
ali_2kool2002 Posted February 19, 2007 Author Share Posted February 19, 2007 According to printf "Also if the form is input='text', the array will still be filled, just the element_key will be empty." my array when the submit button is pressed will always be full of elements which will be empty spaces ,so i think that if statement checking if the array is == 0 ? wont really work cuz its always going to be filled up even if it has no characters... so ne more suggestions?? Link to comment https://forums.phpfreaks.com/topic/39182-solved-how-to-check-if-empty-array/#findComment-188715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.