The Little Guy Posted November 6, 2007 Share Posted November 6, 2007 I am using AJAX to send POST data... it works... but it doesn't work.... It sends the variables to the PHP, but when I use php the php, my IF statement isn't working... Why? JavaScript/AJAX: function getVal(){ var val = ''; if(document.getElementById('fname').value != '') val += 'fname='+document.getElementById('fname').value if(document.getElementById('lname').value != '') val += '&lname='+document.getElementById('lname').value if(document.getElementById('email').value != '') val += '&email='+document.getElementById('email').value if(document.getElementById('userN').value != '') val += '&userN='+document.getElementById('userN').value if(document.getElementById('pass').value != '') val += '&pass='+document.getElementById('pass').value if(document.getElementById('repass').value != '') val += '&repass='+document.getElementById('repass').value if(document.getElementById('passhint').value != '') val += '&passhint='+document.getElementById('passhint').value //return val; document.write(val); } // AJAX FUNCTION function btnJoin(){ var contentType = "application/x-www-form-urlencoded; charset=UTF-8"; var ajaxRequest; try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("Your Browser Doesn't support AJAX."); return false; } } } ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState < 4){ document.getElementById('replaceJoin').innerHTML = '<div id="joinBox"><div class="joinboxtext">Joining Please Wait...</div></div>'; } if(ajaxRequest.readyState == 4){ document.getElementById('replaceJoin').innerHTML = ajaxRequest.responseText; } } va = getVal(); ajaxRequest.open("POST", '/process/join.php', true); ajaxRequest.setRequestHeader("Content-Type", contentType); ajaxRequest.send(va); } PHP: <?php include '../incl/includes.php'; $pass_len = strlen($_POST['pass']); $user_len = strlen($_POST['userN']); $sql = mysql_query("SELECT * FROM users WHERE user='{$_POST['userN']}' LIMIT 1")or die(mysql_error()); if(mysql_num_rows($sql)==0 || !empty($_POST['fname'])|| !empty($_POST['lname'])|| !empty($_POST['email'])|| !empty($_POST['userN'])|| !empty($_POST['pass'])|| !empty($_POST['repass'])|| $_POST['pass']==$_POST['repass']|| $pass_len > 5|| $pass_len < 11|| $user_len > 3|| $user_len < 51){ $string = "abcdefghijklmnopqrstuvwxyz0123456789"; $auth_code = ''; for($i=0;$i<25;$i++){ $pos = rand(0,36); $auth_code .= $string{$pos}; } $fname = addslashes($_POST['fname']); $lname = addslashes($_POST['lname']); $user = addslashes($_POST['userN']); $email = addslashes($_POST['email']); $pass = addslashes($_POST['pass']); $phint = addslashes($_POST['passhint']); $date_reg = date("Y-m-d H:i:s"); $ip = $_SERVER['REMOTE_ADDR']; /*mysql_query("INSERT INTO users(`fname`,`lname`,`email`,`user`,`pass`,`phint`,`date_reg`,`auth_code`,`ip`) VALUES('$fname','$lname','$email','$user','".base64_encode($pass)."','$phint','$date_reg','$auth_code','$ip')")or die(mysql_error()); mkdir("../users/$user"); mkdir("../users/$user/thumbs"); unset($_POST); $sql = mysql_query("SELECT MAX(id) as id FROM users WHERE ip = '$ip' AND auth_code = '$auth_code' ORDER BY id LIMIT 1"); $row = mysql_fetch_array($sql); include 'process/regEmailFormat.php'; mail($email,"TzFiles.com Welcomes You",$message,$headers); header("Location: success.php"); exit;*/ echo '<div id="joinBox"><div class="joinboxtext">User Created!</div> <p style="padding:18px;> Please Check your email, there will be a activation link for you to follow. This will confirm the account and your email address. </p> </div>'; }else{ echo '<div id="joinBox"><div class="joinboxtext">Invalid!</div><p style="padding:18px;>Oops, there was an error, please re-check</p></div>'; } ?> If statement rules: there can NOT be a user in the database with the same name all $_POST['*'] fields must be filled in ($_POST['passhint'] is optional) $pass_len must be between 6 and 10 characters $user_len must be between 4 and 50 characters is there something I am missing? it all ways echos this: echo '<div id="joinBox"><div class="joinboxtext">User Created!</div> <p style="padding:18px;> Please Check your email, there will be a activation link for you to follow. This will confirm the account and your email address. </p> </div>'; and only echo's this if the user name is already in the database: echo '<div id="joinBox"><div class="joinboxtext">Invalid!</div><p style="padding:18px;>Oops, there was an error, please re-check</p></div>'; Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/ Share on other sites More sharing options...
The Little Guy Posted November 6, 2007 Author Share Posted November 6, 2007 oh yeah... if I leave all the fields blank, it echo's out the successful one echo '<div id="joinBox"><div class="joinboxtext">User Created!</div> <p style="padding:18px;> Please Check your email, there will be a activation link for you to follow. This will confirm the account and your email address. </p> </div>'; Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386192 Share on other sites More sharing options...
MadTechie Posted November 6, 2007 Share Posted November 6, 2007 i just reviewed the if statement shouldn't it be && instead of ||? Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386200 Share on other sites More sharing options...
MadTechie Posted November 6, 2007 Share Posted November 6, 2007 try this.. long winded but will revele the problem with the if <?php $valid = false; if(mysql_num_rows($sql)==0) { if(!empty($_POST['fname'])) { if(!empty($_POST['lname'])) { if(!empty($_POST['email'])) { if(!empty($_POST['userN'])) { if(!empty($_POST['pass'])) { if(!empty($_POST['repass'])) { if($_POST['pass']==$_POST['repass']) { if($pass_len > 5) { if($pass_len < 11) { if($user_len > 3) { if($user_len <51) { $valid = true; }else{echo "user over 51";} }else{echo "user Under 3 chars";} }else{echo "pass over 11";} }else{echo "Pass Under 5 chars";} }else{echo "Passwords Don't match";} }else{echo "pass Not Set";} }else{echo "pass Not Set";} }else{echo "userN Not Set";} }else{echo "email Not Set";} }else{echo "lname Not Set";} }else{echo "fName Not Set";} }else{echo "Name Taken";} if(!$valid) { die("error"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386205 Share on other sites More sharing options...
The Little Guy Posted November 6, 2007 Author Share Posted November 6, 2007 when I run it with NO values I get this: fName Not Seterror and If I place 123123 in all the fields.... I get no errors Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386222 Share on other sites More sharing options...
MadTechie Posted November 7, 2007 Share Posted November 7, 2007 sounds right to me.. is that not what you are expecting! Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386234 Share on other sites More sharing options...
darkfreaks Posted November 7, 2007 Share Posted November 7, 2007 you need to check whether fname is set or not it works the same as not empty Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386235 Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Author Share Posted November 7, 2007 well If I run mine, it doesn't work give it a try: http://beta.tzfiles.com/ click join it doesn't matter if you leave it blank or add something to it, it will give the same user the ONLY way it will show a correct errror is if the user exists so... enter "ryan" for the user and press join. Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386236 Share on other sites More sharing options...
darkfreaks Posted November 7, 2007 Share Posted November 7, 2007 its not giving any errors at all Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386237 Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Author Share Posted November 7, 2007 OK... I changed them all to && and now it only displays "Invalid" when Join Now is clicked Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386239 Share on other sites More sharing options...
darkfreaks Posted November 7, 2007 Share Posted November 7, 2007 <?php if(mysql_num_rows($sql)=="0"|| !empty($_POST['fname'])&& !empty($_POST['lname'])&& !empty($_POST['email'])&& !empty($_POST['userN'])&& !empty($_POST['pass'])&& !empty($_POST['repass'])&& $_POST['pass']==$_POST['repass']&& $pass_len > 5&& $pass_len < 11&& $user_len > 3&& $user_len < 51) { $valid="true";} else { $valid="false";}?> Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386243 Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Author Share Posted November 7, 2007 Doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386244 Share on other sites More sharing options...
MadTechie Posted November 7, 2007 Share Posted November 7, 2007 what was the problem with the code i posted ? Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386245 Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Author Share Posted November 7, 2007 nothing.... it works perfect... I don't see why mine isn't working.... it is the same code I got from the one that I already use... and that does work Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386248 Share on other sites More sharing options...
MadTechie Posted November 7, 2007 Share Posted November 7, 2007 to fix your use if(mysql_num_rows($sql)!=0 && !empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['email']) && !empty($_POST['userN']) && !empty($_POST['pass']) && !empty($_POST['repass']) && $_POST['pass']==$_POST['repass'] && $pass_len > 5 && $pass_len < 11 && $user_len > 3 && $user_len < 51){ EDIT: note the !=0 (mysql_num_rows($sql)!=0 && Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386254 Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Author Share Posted November 7, 2007 I have tried that... doesn't work... SO... I just use yours and in my if I just do: <?php if($valid){ Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386256 Share on other sites More sharing options...
MadTechie Posted November 7, 2007 Share Posted November 7, 2007 yep.. i find it easier to manage/debug and update later (of course you don't need to tab it out like i have) Quote Link to comment https://forums.phpfreaks.com/topic/76292-solved-bad-if-condition/#findComment-386259 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.