Jump to content

Php Header() - Javascript


mvsveena

Recommended Posts

Hi All,Kindly need help on the following. I have created a registration form and once the user is registered successfully, am throwing a confirm box (using javascript). If the user clicks the 'ok' button on the confirm box it has to throw the login page. To attain the above functionality, am using the below logic. But it is not working properly, there is some mistake in the php header(). It could able to throw the confirm box, if I comment the code /*<?php header("Location:http://localhost//loginformV1.1.php") ?> */ and also it throws me the login page if I add some javascript statement inbetween the if cond (ex: if(pressed==true) { document.write("hi"); <?php header("location:http://localhost//loginformV1.1.php")?> } ) but this time its not displaying the confirm box.

<div class="main"><?php	 if($user != "")  //the variable $user has the value 'registration successfull'	  { ?>		   <script language="javascript">		   {	  var pressed = confirm("<?php echo $user ?>");   	  if (pressed==true)	  { 			   <?php header("Location:http://localhost//loginformV1.1.php") ?>		 	  }		 		  else	  {		 document.write(pressed);	  }			}			</script>  		    <?php } ?>............

Can anyone give me some solution for this..........Thanks,Veen

Link to comment
Share on other sites

First of all (remember this is just to help you get better in programming php):You php code looks like a total chaos. Try make php your main language and write javascript in it. Since php is a serversided language it will only send the data to a client. Javascript is a clientside language. This means that php outputs the javascript. Try work like this:1- php output to client2- client modifies data (by using a form for example)3- let javascript take care of user functionality and effects to make things go easier and look nicer.4- send the data back to the server using php.Below is what your script looks like in my way of working, now you are able to see what goes wrong:

<?phpecho '<div class="main">';if($user != "")  //the variable $user has the value 'registration successfull'{   //All javascript is echoed because I am using php variables in my javascript.  echo '<script language="javascript">  echo '{  echo '  var pressed = confirm("'.$user.'");    echo '  if (pressed==true)  echo '  {	//This php line doesn't belong here in the first place, but it is also missing; at the end  header("Location:http://localhost//loginformV1.1.php")  echo '  }			   echo '  else  echo '  {  echo '	document.write(pressed);  echo '  }  echo '}  echo '</script>	   }?>

Now the good way to fix this. (With comments in the script for explanation:

//Check if form is sendif(isset($_POST['submit'])){  //Secure the POST variables against sql injection  $username = mysql_real_escape_string($_POST['username']);	$email	= mysql_real_escape_string($_POST['email']);	 	//Make mysql insertQuery (this is the form of query I use)  $sql = "	  INSERT INTO users 	(		username,		  email	) 	VALUES 		(		  '".$username."',			'".$email."'	  ) 	";	//Execute query	  $result = $db->sql_query($sql);	//Check if the query was succesfull	if($result)	{	//Query is a succes, trow a confirm  ?>  <script language="JavaScript" type="text/javascript">  <!--	var ask = confirm('Your registration was a succes! Resume to the loginpage?');	  if(ask)	  {		  //User says yes and will be redirected to login.php		window.location = "login.php";	  }  //-->  </script>  <?php	}  	//An error occured in the query	else	{	  echo 'Something went wrong. Plz contact the admin.';	}}//Normal Loginformecho '<html>';echo '<head>';echo '</head>';echo '<body>';echo '<form action="registration.php" method="POST">';echo 'Insert username: <input type="text" name="username"><br />';echo 'Insert email: <input type="text" name="email"><br />';echo '<input type="submit" name="submit" value="Register">';echo '</form>';echo '</body>';echo '</html>';

Link to comment
Share on other sites

Hi Redroest,Thanks for your valuable input. I use 'window.location' and now it throws the login page properly. Actually I am using two forms Registration and Login. The new user has to register first and once the registration successful, am throwing the login page.For Registration form, I'm using 2 files. One is the Html form file(.php) and the other one php file(.php) which talks to database. In the php file, am checking whether the form has been submitted and if so connecting it to MySQL, inserting the details into table etc. Once it inserted the form details successfully, it has to throw me the (Success/failure) msg in the same page (ie: in the reg form page). But since am echo'ing the success/failure msg from php it displays the msg in a new page. To avoid this, again I throw the same html form file with the (success/failure) msg using the function require().This is the point I need some msg box and I used javascript confirm box.Here is my rough draft of code of both html and php file. Please look into it and provide me your comments.Html Form file Form.php: (Note: even in the Html file, am using php)

<body>	  <div class="main">		  <center>		  <?php 	 		 if($user != "")  		 { ?>		<script language="javascript"> // I will echo my javascript code. Will make proper modifications.			{		  var pressed = confirm("<?php echo $user ?>");   		  if (pressed==true)		  { 					 window.location.assign("http://localhost//loginformV1.1.php");	  		 	  }		 			  else		  {			 window.location.assign("http://localhost//homeV1.1.html");		  }				}				</script>  		  		<?php } ?>		  </center></br>			 <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">		   <label> First Name : </label>      <input type="text" name="first_name" size=20 tabindex="1"> <br> <br>		   <label> Last Name : </label>      <input type="text" name="last_name" size=20 tabindex="2"> <br> <br>		   <label> Gender :        </label>      		 <input type="radio" name="gender" value="m" tabindex="3"> Male         			   <input type="radio" name="gender" value="f" tabindex="4"> Female         <br> <br>		   <input type="text" name="year" id="yyyy" value="" tabindex="7" size="5"> <br> <br>		 <label> Email id :          </label>		   <input type="text" name="emailid"  value="" tabindex="8"> <br> <br>		 <label> Password :      </label>		   <input type="password" name="pwd" value="" tabindex="9"> <br> <br>		 <label> Confirm Password :     </label>		   <input type="password" name="cnfmpwd" value="" tabindex="10"> <br> <br>		   <input type="submit" name="subbutton" value="Submit" size="32" tabindex="11" onclick="return verifyData(this.form)"> 	   </form>					  .....................</body></html>

PHP FILE

<?php   $user="";      if(isset($_POST['subbutton'])) //check whether the user has clicked the submit button   {	 $user = register(); //calling the function register()   }   else   {	 require("newuserfrmV1.1.php");   }      if($user!="")   {	  require("newuserfrmV1.1.php");   }    function register() // function register() begins   {		 /* store the form values into local variables */	 $firstname = $_POST[first_name];	........	.........	.........	 $dbconn   = mysql_connect  //connect to database	 $query  // insert query	.......	.......		  	 if (!$dbconn)	 { 	echo "connection failed ". mysql_errno(). "<br>";		echo "warning: ". mysql_error();	 }	 else	 { 		  if (!mysql_query($query,$dbconn))  //executing the query	  { 		  echo "error in inserting values ". mysql_error();	  }	  else	  {			   echo "1 row added";			   $user="registration successfull";		   return $user;	  } 	 	 }	   }   mysql_close($dbconn);   return $user;?>

I feel that the above code is not upto standards though it works well. Appreciate any help/comments from anybody.Thanks,Veen

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...