jmdostal Posted March 8, 2012 Share Posted March 8, 2012 Good morning, I am a beginner at PHP, but trying to write a simple script to use at work and currently stuck on one part, looking for some advice! I have a simple form here > http://jmdostal.com/route2.php (Feel free to use it to test), that returns data on the next page, in a format such as : Jose F - Jose A - 285 Brenda L [Delivery] [Dallas] - Items Job 2 [Pickup] [Arlington] - Items [] [] - [] [] - [] [] - [] [] - [] [] - Gustavo - +1 - 284/pickup Jody W [Delivery] [Keller] - Items [] [] - [] [] - [] [] - [] [] - [] [] - [] [] - My question is how can I get it not to show the fields that are empty, so I dont have a bunch of lines with [] [] - formatting Here is my coding you can view here > http://jmdostal.com/code.txt A friend had suggested to me trying to use isset , so I tried it on a few lines, such as : if(isset($_POST['items14'])){ $items14 = "- ". Trim(stripslashes($_POST['items14'])); } But it didnt seem to change anything. Im sure this is something simply, Any suggestions are appreciated! Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/ Share on other sites More sharing options...
smerny Posted March 8, 2012 Share Posted March 8, 2012 http://php.net/manual/en/function.empty.php this what you were looking for? Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325223 Share on other sites More sharing options...
jmdostal Posted March 8, 2012 Author Share Posted March 8, 2012 Yes, I saw that as well, but perhaps I am just not using it correctly. I just added : if (empty($jobsite13)) { echo ''; } if (empty($dp13)) { echo ''; } if (empty($city13)) { echo ''; } if (empty($item13)) { echo ''; } $jobsite13 = "<br><font color=#0000ff><font size=3>". Trim(stripslashes($_POST['jobsite13'])); $dp13 = "</b>[". Trim(stripslashes($_POST['dp13'])) . "]"; $city13 = "[". Trim(stripslashes($_POST['city13'])) . "]"; $items13 = "- ". Trim(stripslashes($_POST['items13'])); Then tested again and left the 3rd job field blank, but still returned the [][]- Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325227 Share on other sites More sharing options...
Muddy_Funster Posted March 8, 2012 Share Posted March 8, 2012 empty is the way to go, however you are not using the if statments to remove the [] from the output. you are running the if's and then producing the [] using code that is run after them... Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325236 Share on other sites More sharing options...
jmdostal Posted March 8, 2012 Author Share Posted March 8, 2012 Could you give me an example of what I would need to change my line of code to, to do this properly, Instead of having , if (empty($dp13)) { echo ''; } $dp13 = "[". Trim(stripslashes($_POST['dp13'])) . "]"; What should it say instead? Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325238 Share on other sites More sharing options...
batwimp Posted March 8, 2012 Share Posted March 8, 2012 if (!empty($dp13)) { $dp13 = "[". Trim(stripslashes($_POST['dp13'])) . "]"; } Note the ! in from of empty, this means "if NOT empty"; Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325244 Share on other sites More sharing options...
Muddy_Funster Posted March 8, 2012 Share Posted March 8, 2012 use your if()'s to dictate what is displayed: if(!empty($_POST['jobsite13']){ $jobsite13 = "<br><font color=#0000ff><font size=3>". Trim(stripslashes($_POST['jobsite13'])); if (empty($dp13)) { $dp13 = ''; } else {$dp13 = "</b>[". Trim(stripslashes($_POST['dp13'])) . "]"; } if (empty($city13)) { city13 = ''; } else {$city13 = "</b>[". Trim(stripslashes($_POST['dp13'])) . "]"; } if (empty($item13)) { item13 = ''; } else {$dp13 = "</b>[". Trim(stripslashes($_POST['dp13'])) . "]"; } } else{ $jobsite13 = ''; } if(($jobsite13 != '')&&($dp13 != '')&&($city13 != '')){ echo "<table><tr><td colspan=2>$jobsite13</td></tr><tr><td>DP</td><td>$dp</td></tr><tr><td>CITY</td><td>$city13</td></tr></table>; else{ echo '';} Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325255 Share on other sites More sharing options...
jmdostal Posted March 8, 2012 Author Share Posted March 8, 2012 That is perfect. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325261 Share on other sites More sharing options...
jmdostal Posted March 8, 2012 Author Share Posted March 8, 2012 Err sorry, getting closer I think , Ive put the code such as : if (!empty($driver1)) { $driver1 = "<font color=#0000FF><font size=5>". Trim(stripslashes($_POST['driver1'])); } if (!empty($h1)) { $h1 = "- ". Trim(stripslashes($_POST['h1'])); } if (!empty($t1)) { $t1 = "- ". Trim(stripslashes($_POST['t1'])) . "<br>"; } if (!empty($jobsite11)) { $dp12 = "<br><font color=#0000ff><font size=3>". Trim(stripslashes($_POST['jobsite11'])) . "]"; } if (!empty($dp11)) { $dp12 = "[". Trim(stripslashes($_POST['dp11'])) . "]"; } if (!empty($city11)) { $dp12 = "[". Trim(stripslashes($_POST['city11'])) . "]"; } if (!empty($items11)) { $dp12 = "[". Trim(stripslashes($_POST['items11'])) . "]"; } But now when I enter something into the field, nothing is showing up on the page at all, as if its counting everything as a blank . ^ tried the code above but gave error Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325267 Share on other sites More sharing options...
Muddy_Funster Posted March 8, 2012 Share Posted March 8, 2012 not enough code, nothing of what you posted actualy controls display. Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325270 Share on other sites More sharing options...
jmdostal Posted March 8, 2012 Author Share Posted March 8, 2012 The code I have for the display is : print " <table width=70% bgcolor=#ffffff> <tr><td> <font color=#0000FF> <B>$driver1 $h1 $t1</b> <B>$jobsite11</b> $dp11 $city11 $items11 <B>$jobsite12</b> $dp12 $city12 $items12 <B>$jobsite13</b> $dp13 $city13 $items13 <B>$jobsite14</b> $dp14 $city14 $items14 <B>$jobsite15</b> $dp15 $city15 $items15 <B>$jobsite16</b> $dp16 $city16 $items16 <B>$jobsite17</b> $dp17 $city17 $items17 that was displaying before, but now nothing will show. The full code is if (!empty($driver1)) { $driver1 = "<font color=#0000FF><font size=5>". Trim(stripslashes($_POST['driver1'])); } if (!empty($h1)) { $h1 = "- ". Trim(stripslashes($_POST['h1'])); } if (!empty($t1)) { $t1 = "- ". Trim(stripslashes($_POST['t1'])) . "<br>"; } if (!empty($jobsite11)) { $dp12 = "<br><font color=#0000ff><font size=3>". Trim(stripslashes($_POST['jobsite11'])) . "]"; } if (!empty($dp11)) { $dp12 = "[". Trim(stripslashes($_POST['dp11'])) . "]"; } if (!empty($city11)) { $dp12 = "[". Trim(stripslashes($_POST['city11'])) . "]"; } if (!empty($items11)) { $dp12 = "[". Trim(stripslashes($_POST['items11'])) . "]"; } if (!empty($jobsite12)) { $dp12 = "<br><font color=#0000ff><font size=3>". Trim(stripslashes($_POST['jobsite12'])) . "]"; } if (!empty($dp12)) { $dp12 = "[". Trim(stripslashes($_POST['dp12'])) . "]"; } if (!empty($city12)) { $dp12 = "[". Trim(stripslashes($_POST['city12'])) . "]"; } if (!empty($items12)) { $dp12 = "[". Trim(stripslashes($_POST['items12'])) . "]"; } print " <table width=70% bgcolor=#ffffff> <tr><td> <font color=#0000FF> <B>$driver1 $h1 $t1</b> <B>$jobsite11</b> $dp11 $city11 $items11 <B>$jobsite12</b> $dp12 $city12 $items12 <B>$jobsite13</b> $dp13 $city13 $items13 <B>$jobsite14</b> $dp14 $city14 $items14 <B>$jobsite15</b> $dp15 $city15 $items15 <B>$jobsite16</b> $dp16 $city16 $items16 <B>$jobsite17</b> $dp17 $city17 $items17 <b>$driver2 $h2 $t2</b> <b>$jobsite21</b> $dp21 $city21 $items21 <b>$jobsite22</b> $dp22 $city22 $items22 <b>$jobsite23</b> $dp23 $city23 $items23 <b>$jobsite24</b> $dp24 $city24 $items24 <b>$jobsite25</b> $dp25 $city25 $items25 <b>$jobsite26</b> $dp26 $city26 $items26 <b>$jobsite27</b> $dp27 $city27 $items27 Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325272 Share on other sites More sharing options...
smerny Posted March 8, 2012 Share Posted March 8, 2012 if (!empty($jobsite11)) { $dp12 = "<br><font color=#0000ff><font size=3>". Trim(stripslashes($_POST['jobsite11'])) . "]"; } if (!empty($dp11)) { $dp12 = "[". Trim(stripslashes($_POST['dp11'])) . "]"; } if (!empty($city11)) { $dp12 = "[". Trim(stripslashes($_POST['city11'])) . "]"; } if (!empty($items11)) { $dp12 = "[". Trim(stripslashes($_POST['items11'])) . "]"; } is $dp11, etc getting set prior to this? if not, it needs to be, if it is.. why are you then using $_POST['dp11'] instead? and do you want to keep replacing whatever is in $dp12? Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325286 Share on other sites More sharing options...
jmdostal Posted March 8, 2012 Author Share Posted March 8, 2012 The $dp11 , etc is being set via the form at http://jmdostal.com/route2.php Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325289 Share on other sites More sharing options...
smerny Posted March 8, 2012 Share Posted March 8, 2012 $_POST['dp11'] gets set when you submit the form, not $dp11 Quote Link to comment https://forums.phpfreaks.com/topic/258533-php-form-do-not-post-blank-fields/#findComment-1325290 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.