charmquark Posted January 17, 2007 Share Posted January 17, 2007 I'm having a problem comparing blank variables (form inputs which should remain blank)[code]$coef1 = "";$coef2 = "";$coef3 = "2";function check($pqnum){ if ((!$_POST['coef3']==$coef1) || (!$_POST['coef2']==$coef2) || (!$_POST['coef3']==$coef3)){ return FALSE; } else { return TRUE; }}[/code]Any ideas how I can fix this so if the $coef is " " then the if statement works? Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/ Share on other sites More sharing options...
dgiberson Posted January 17, 2007 Share Posted January 17, 2007 if (isset($_POST['coef'])){echo "variable is set";}else{echo "variable is not set";} Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-162505 Share on other sites More sharing options...
charmquark Posted January 17, 2007 Author Share Posted January 17, 2007 Thanks, but I still don't get it and I'm getting parse errors.What if I was to change the definition of the $coef to a $_POST['coef'] =""; ? Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-162518 Share on other sites More sharing options...
dgiberson Posted January 17, 2007 Share Posted January 17, 2007 if (isset($_POST['coef']) { if ($_POST['coef'] == $coef) { //do something }} Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-162519 Share on other sites More sharing options...
emehrkay Posted January 17, 2007 Share Posted January 17, 2007 print_r($_POST) to see what vars you have available to you Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-162529 Share on other sites More sharing options...
charmquark Posted January 17, 2007 Author Share Posted January 17, 2007 [quote author=dgiberson link=topic=122737.msg506598#msg506598 date=1169002126]if (isset($_POST['coef']) { if ($_POST['coef'] == $coef) { //do something }}[/quote]It makes sense to use the isset(), but I'm still confused (still getting parse errors) when checking for all 4 variables as in my above example. For this question, 3 of the variables should remain blank. For other questions one or two or none of the variables may be blank.I tried print_r($_POST) ;This was the output:: Array ( [coef1] => [coef2] => [coef3] => [coef4] => 2 [submit] => submit [qnum] => 3 ) Makes sense, but I'm tripping over these blank defined variables when comparing them to the posted variables.Interesting note: Other chemistry exercises I wrote don't have any problem with blank coefficients, but I'm pulling the blank variables from a database table. In this exercise, I'm just using different pages for each question as the form construction is different, depending on the question.I think I might retry putting the answers and form type in a database table, and then use a case statement to call the type of form... although I'd still like to know how to compare a blank $_POST var with a blank defined variable. Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-162570 Share on other sites More sharing options...
kenrbnsn Posted January 17, 2007 Share Posted January 17, 2007 Going back to the code you originally posted, the "if" statement is incorrect and the variables will not be defined inside the function. Try this instead:[code]<?phpfunction check($pqnum){ $coef1 = ""; $coef2 = ""; $coef3 = "2"; if (($_POST['coef3'] != $coef1) || ($_POST['coef2'] != $coef2) || ($_POST['coef3'] != $coef3)){ return FALSE; } else { return TRUE; }}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-162572 Share on other sites More sharing options...
charmquark Posted January 17, 2007 Author Share Posted January 17, 2007 ;D kenrbnsn, the solution is all good ... it works great and reduces confusion.Of course, my variable scope was all wrong, I'm only a copy and paste php wannabe. Now it works exactly as I expect. hugs....More thinking about this script requires a change of the structure. There are 25 files. <egads> Last night I created the table in the db with a column called type. I'm only guessing, but the next step is to get my head around the switch() ??? Each $pqnum (question number) has a form type, either synthesis, decomp or other.Does the following make sense?[code]wannabe code:query($qnum=$pqnum);query($type);switch() {case if $type="synthesis":echo $show_form_synth;break;case if $type="decomp":echo $show_form_decomp;break;case if $type="other":echo show_form_other;break;}[/code]Thanks again for helping. Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-162897 Share on other sites More sharing options...
kenrbnsn Posted January 17, 2007 Share Posted January 17, 2007 You really should read up on the syntax of the [url=http://www.php.net/manual/en/control-structures.switch.php]switch statement[/url][code]<?phpswitch($type) { case "synthesis": echo $show_form_synth; break; case "decomp": echo $show_form_decomp; break; case if "other": echo show_form_other; break;}?>[/code]Also, learn to indent your code, it will make reading and debugging much easier.Ken Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-162904 Share on other sites More sharing options...
charmquark Posted January 18, 2007 Author Share Posted January 18, 2007 Again, you are absolutely right. For now, I'm the only person looking at this and it's all fresh in my head. If I ever hope to share this application with another chemistry teacher, I need to pull up my indentation socks. I've googled php indentation, but feel free to point me to your favorite reference. I've yet to create a script of my own with a switch (), if always worked. Just fitting the switch into the flow of the script is hard for me, so please be patient.I'll go away and think/read about it and play with it some more.Thanks again for your help and to everyones response.Have a happy php freaking day. Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-163306 Share on other sites More sharing options...
anatak Posted January 18, 2007 Share Posted January 18, 2007 [quote]Interesting note: Other chemistry exercises I wrote don't have any problem with blank coefficients, but I'm pulling the blank variables from a database table. In this exercise, I'm just using different pages for each question as the form construction is different, depending on the question.[/quote]there is a difference between "" and null out of a database.check if in your database you have the "" string or the null variable. Quote Link to comment https://forums.phpfreaks.com/topic/34501-cant-compare-a-blank-var-with-a-blank-_post-var/#findComment-163452 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.