ecabrera Posted February 20, 2012 Share Posted February 20, 2012 help why does the IF not work if if($left && $right) <?php if(isset($_POST['savebtn'])){ $left = $_POST['left']; $right = $_POST['right']; if($left && $right){ echo "there is text"; }else{ echo "this is no text"; } } ?> <div class="fullpage"> <form action="edithome.php" method="post"> <div class='left'> <textarea name="left" style="width: 530px; height: 100%;"> </textarea> </div> <div class="right"> <textarea name="right" style="width: 420px; height: 100%;"> </textarea> </div> <input type="submit" name="savebtn" value"Save" /> </from> </div> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 It's certainly working, even if it isn't doing what you want it to do. Define "not working", and what you've done to debug the problem. Quote Link to comment Share on other sites More sharing options...
ecabrera Posted February 20, 2012 Author Share Posted February 20, 2012 <?php if(isset($_POST['savebtn'])){ $left = $_POST['left']; $right = $_POST['right']; if($left && $right){ echo "there is text"; }else{ echo "this is no text"; } } ?> well if i click submit and there is no text is says "there is text" Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 20, 2012 Share Posted February 20, 2012 Not working "how"? what is the input and the output you feel is wrong. That condition is very "loose" meaning that as long as the variable is set and not an empty string it will result in true. Your textareas have a line break in them so it will always result in true unless the user removes the value. You should probably trim the value and then check if it is empty() if(isset($_POST['savebtn'])) { $left = trim($_POST['left']); $right = trim($_POST['right']); if(!empty($left) && !empty($right)) { echo "there is text"; }else{ echo "this is no text"; } } Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 20, 2012 Share Posted February 20, 2012 use empty() instead. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 20, 2012 Share Posted February 20, 2012 is "savebtn" set? Quote Link to comment Share on other sites More sharing options...
ecabrera Posted February 20, 2012 Author Share Posted February 20, 2012 i dont think you need to set the savebtn Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 you aren't checking whether there is anything in the variables, you're checking whether they evaluate to TRUE. There must be something in them. var_dump both variables and see what they hold. Quote Link to comment Share on other sites More sharing options...
xyph Posted February 20, 2012 Share Posted February 20, 2012 use empty() instead. This will NOT WORK. Check the manual, and understand what the function does before offering it as a solution. Psycho has identified the cause of the error. Let the OP try to figure the rest out on their own first. Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 20, 2012 Share Posted February 20, 2012 use empty() instead. This will NOT WORK. Check the manual, and understand what the function does before offering it as a solution. Thanks, but I know what it does. Psycho posted at the same time I did. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 "Any text" could include a lone zero. empty() will return TRUE for a zero value, so empty() won't work for this. Quote Link to comment Share on other sites More sharing options...
xyph Posted February 20, 2012 Share Posted February 20, 2012 Thanks, but I know what it does. Psycho posted at the same time I did. Perhaps you need to turn on warning when others have replied while you were. If you know what it does, why did you suggest it as a solution? Adding (!)empty() will not change the results he gets. A variable containing a line break will result in empty() returning FALSE. Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 20, 2012 Share Posted February 20, 2012 so empty() won't work for this. Yes, I see that now. I didn't notice the scroll bar on his post earlier, so I didn't see the line break in the form. Thanks, but I know what it does. Psycho posted at the same time I did. If you know what it does, why did you suggest it as a solution? Adding (!)empty() will not change the results he gets. A variable containing a line break will result in empty() returning FALSE. See above. I'm tired, and had a long day. I don't put in a lot of effort to people that don't put any effort in on their part. My apologies. Stop busting my balls. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 20, 2012 Share Posted February 20, 2012 This is a possible way to test emptiness (this may not help in finding a solution to the current problem, but we were talking about the empty() function, so I thought I would post it.) Removes spaces/tabs/newlines/carriage returns (more might need to be removed): <?php function blank($str){ $newstr = preg_replace("/ |\r|\n|\t/", "", $str); if(empty($newstr)) return true; return false; } if(blank($_POST['something'])) echo "post value is blank."; else echo "post value is not blank."; ?> Quote Link to comment Share on other sites More sharing options...
ecabrera Posted February 20, 2012 Author Share Posted February 20, 2012 well im not to advance like you guys but heres what i did and its working i dont if its the best way but hey i aleast im trying <?php require "scripts/connect.php"; if(isset($_POST['savebtn'])){ $left = mysql_real_escape_string($_POST['left']); $right = mysql_real_escape_string($_POST['right']); if($left){ if($right){ $query = mysql_query("UPDATE `home` SET `left`='$left',`right`='$right' "); $msg = "SAVED"; }else $msg = "Fill in"; }else $msg = "Fill in"; } mysql_close(); ?> <?php require "scripts/connect.php"; $mysql = mysql_query("SELECT * FROM home"); $rows = mysql_fetch_assoc($mysql); $left = $rows['left']; $right = $rows['right']; mysql_close(); ?> <div class="fullpage"> <?php echo $msg; ?> <form action="edithome.php" method="post"> <div class='left'> <textarea name="left" style="width: 530px; height: 100%;"> <?php echo $left; ?> </textarea> </div> <div class="right"> <textarea name="right" style="width: 420px; height: 100%;"> <?php echo $right; ?> </textarea> </div> <input type="submit" name="savebtn" value"Save" /> </from> </div> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted February 21, 2012 Share Posted February 21, 2012 you need to do something like if(isset($left) && isset($right)) {// code here }else{ // error } Quote Link to comment 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.