2015-07-14 6 views
13
<?php 

session_start(); 

include_once "connect.php"; 

$fullName = $_POST['fullname']; 
$userName = $_POST['username']; 
$emailAdd = $_POST['email']; 
$passWord = $_POST['password']; 
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' "); 
$result = mysql_fetch_array($query); 

    if($fullName == "") 
    { 

?> 
    <script> 
     alert("Full Name is required!"); 
     window.location.href = "registeruser.php"; 
    </script> 
<?php 
    } 

    else 
    { 
     if ($userName == "") 
     { 
?> 
     <script> 
      alert("Invalid username!"); 
      window.location.href = "registeruser.php"; 
     </script> 
<?php  
     } 

     else 
     { 
      if($userName == $result['USERNAME']) 
      { 
?> 
      <script> 
       alert("Username already exists!"); 
       window.location.href = "registeruser.php"; 
      </script> 
<?php 
      } 

      else 
      { 
       if ($emailAdd == $result['EMAIL']) 
       { 
?> 
       <script> 
        alert("Email address is required!"); 
        window.location.href = "registeruser.php"; 
       </script> 
<?php 
       } 

       else 
       { 
        if ($passWord == "") 
        { 
?> 
        <script> 
         alert("Username is required!"); 
         window.location.href = "registeruser.php"; 
        </script> 
<?php  
        } 

        else 
        { 
         if($_POST['password']==$_POST['confirmpass']) 
         { 
         mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) 
         VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')"); 
         mysql_close(); 
?>  
         <script> 
          alert("Registered Successfully!"); 
          window.location.href = "index.php"; 
         </script> 
<?php 
         } 

         else 
         { 
?> 
          <script> 
          alert("Password and Confirm Password do not match"); 
          window.location.href = "registeruser.php"; 
          </script> 
<?php 
         } 
        } 
       } 
      } 
     } 
    } 

?> 

Es funktioniert gut. Aber das Problem ist: das Alert-Fenster zeigt auf eine andere Webseite und nicht innerhalb der Webseite Es ist in Ordnung, aber es sieht schrecklich aus, den Benutzer zu einer anderen Webseite zu leiten, ohne Inhalt dort zu haben. Wenn Sie mir bitte helfen könnten, dies zu beheben und das JavaScript-Warnfenster nur auf derselben Webseite anzeigen zu lassen. Eigentlich habe ich einige Lösungen wie diese versucht ..JavaScript Alert Fenster Redirects zu einer anderen Webseite

if($fullName == "") 
{ 
    echo '<script>'; 
    echo 'alert("Full Name is required!")'; 
    echo 'window.location.href = "registeruser.php"'; 
    echo '</script>'; 
} 
+0

versuchen, eine '/', bevor Sie den 'registereduser.php' oder legen Sie den genauen Pfad mit Domain-Namen setzen. – roullie

+0

Vollständigen Pfad zu 'echo 'hinzufügen window.location.href =" registeruser.php "';' –

+0

Nein. Es funktioniert nicht. – monsteruniversity0908

Antwort

0

Verwenden Basis-URL und dann Benutzerdefinierte Pfad einfügen, um Ihre Seite wie:

<?php 
if($fullName == "") 
{ 
?> 
    <script> 
    var MyBaseURL = <?php echo BASE_URL_CONSTANT?> 
    var customURL = MyBaseURL."/registeruser.php"; 
    alert("Full Name is required!"); 
    window.location.href = customURL; 
    </script> 
<?php 
} 
?> 

Und Sie sollten Skript vermeiden Echoing, verwende regelmäßigen Skriptblock mit umschließenden PHP-Tags.

+0

Ich glaube, er spricht über die Seite, die während der Warnung angezeigt wird, nicht über die Seite, auf die sie nach der Warnung weitergeleitet wird. – Barmar

+0

es ist nicht php Problem überhaupt .. –

0

Sie sollten die Warnung auf die registeruser.php Seite setzen. Lassen Sie das PHP-Skript zu tun:

header("Location: registeruser.php?message=" . urlencode("Invalid username")); 

Dann registeruser.php tun können:

<?php 
if (isset($_GET['message'])) { 
    ?> 
    <script> 
    alert(<?php echo json_encode($_GET['message']); ?>); 
    </script> 
    <?php 
} 
+0

Haben Sie etwas verpasst? 'isset ($ _ GET ['Nachricht'' – TarangP

+0

@TarangP Ich habe das, was ist die Frage? – Barmar

+0

Ich denke, die richtige Syntax issetset ($ _ GET [' Nachricht ']) – TarangP

0

Vielleicht versuchen, einige html nach dem session_start Rendering(). z.B.

<html> 
<head> 
</head> 
<body> 
<h1>Login Message</h1> 
</body> 
</html> 

Das sollte eine Seite für die Nachricht bereitstellen, auf der angezeigt werden soll. Obwohl, noch besser, meiner Meinung nach: Nicht aus PHP überhaupt ausbrechen und die php header() -Funktion verwenden, um Sie auf die entsprechende Seite zu bringen, wo erforderlich.

0

Try es wie folgt verwenden:

if($fullName == "") 
{ 
    echo "<script language='javascript'>alert('Full Name is required!')</script>"; 
    echo "<script language='javascript'>window.location.replace('page.php?id=".$crmId."'); </script>"; 
} 
-1

Sie diesen Code

<?php 

session_start(); 

include_once "connect.php"; 

$fullName = $_POST['fullname']; 
$userName = $_POST['username']; 
$emailAdd = $_POST['email']; 
$passWord = $_POST['password']; 
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' "); 
$result = mysql_fetch_array($query); 

    if($fullName == "") 
    { 

?> 
    <script> 
     if(alert("Full Name is required!")) 
     { 
      window.location.href = "registeruser.php"; 
     } 
    </script> 
<?php 
    } 

    else 
    { 
     if ($userName == "") 
     { 
?> 
     <script> 
      if(alert("Invalid username!")) 
      { 
      window.location.href = "registeruser.php"; 
      } 
     </script> 
<?php  
     } 

     else 
     { 
      if($userName == $result['USERNAME']) 
      { 
?> 
      <script> 
       if(alert("Username already exists!")) 
       { 
        window.location.href = "registeruser.php"; 
       } 
      </script> 
<?php 
      } 

      else 
      { 
       if ($emailAdd == $result['EMAIL']) 
       { 
?> 
       <script> 
        if(alert("Email address is required!")) 
        { 
         window.location.href = "registeruser.php"; 
        } 
       </script> 
<?php 
       } 

       else 
       { 
        if ($passWord == "") 
        { 
?> 
        <script> 
         if(alert("Username is required!")) 
         { 
          window.location.href = "registeruser.php"; 
         } 
        </script> 
<?php  
        } 

        else 
        { 
         if($_POST['password']==$_POST['confirmpass']) 
         { 
         mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) 
         VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')"); 
         mysql_close(); 
?>  
         <script> 
          if(alert("Registered Successfully!")) 
          { 
           window.location.href = "index.php"; 
          } 
         </script> 
<?php 
         } 

         else 
         { 
?> 
          <script> 
          if(alert("Password and Confirm Password do not match")) 
          { 
            window.location.href = "registeruser.php"; 
          } 
          </script> 
<?php 
         } 
        } 
       } 
      } 
     } 
    } 

?> 

dieser Code Umleitungsseite, wenn Benutzer klicken Sie OK-Taste verwenden können. Alarm-Massage-Show vor der Seite Redirect

+0

Bitte fügen Sie weitere Informationen zu Ihrer Antwort. –

0

Sie müssen, wie andere sagten, Ihre Logik an das Ende von HTML zu bewegen. Zweitens müssten Sie diese Logik auf der Seite implementieren, die Sie laden möchten, und nicht auf der Seite, die umgeleitet wird. Es ist eine zweistufige Logik. Zum Beispiel:

Sie müssten window.location in Ihrem aktuellen Skript implementieren.

Dann, kurz vor Tag der Seite möchten Sie den Benutzer umgeleitet werden:

<?php 
if($fullName == "") 
{ 
    echo '<script>'; 
    echo 'alert("Full Name is required!")'; 
    echo '</script>'; 
} 
?> 
</body> 

Also, es zu tun, nachdem die Seite geladen ist, wird es richtig alarmieren.

0

Ihre Standort-HREF-Syntax ist falsch.Es sollte so aussehen:

if($fullName == "") 
{ 
    echo '<script>'; 
    echo 'alert("Full Name is required!")'; 
    echo 'window.location.href = SITE_URL."registeruser.php"'; 
    echo '</script>'; 
} 

Hier SITE_URL ist Ihre Projekte Wurzelpfad wie http://localhost/yourproject/

0

dieses Skript auf der gleichen Seite ausgeführt wird, aber Sie haben Formular zu verwenden, wie diese

<form method=POST action=""> 
<!--input goes here--> 
</form> 



<?php 

session_start(); 

include_once "connect.php"; 
if(isset($_POST[submit])){ 
$fullName = $_POST['fullname']; 
$userName = $_POST['username']; 
$emailAdd = $_POST['email']; 
$passWord = $_POST['password']; 
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' "); 
$result = mysql_fetch_array($query); 

    if($fullName == "") 
    { 


    echo ' <script> 
     alert("Full Name is required!"); 
     window.location.href = "registeruser.php"; 
    </script>'; 

    } 

    else 
    { 
     if ($userName == "") 
     { 

     echo ' <script> 
      alert("Invalid username!"); 
      window.location.href = "registeruser.php"; 
     </script>'; 

     } 

     else 
     { 
      if($userName == $result['USERNAME']) 
      { 

      echo ' <script> 
       alert("Username already exists!"); 
       window.location.href = "registeruser.php"; 
      </script>'; 

      } 

      else 
      { 
       if ($emailAdd == $result['EMAIL']) 
       { 

       echo '<script> 
        alert("Email address is required!"); 
        window.location.href = "registeruser.php"; 
       </script>'; 

       } 

       else 
       { 
        if ($passWord == "") 
        { 

        echo ' <script> 
         alert("Username is required!"); 
         window.location.href = "registeruser.php"; 
        </script>'; 

        } 

        else 
        { 
         if($_POST['password']==$_POST['confirmpass']) 
         { 
         mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) 
         VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')"); 
         mysql_close(); 

         echo ' <script> 
          alert("Registered Successfully!"); 
          window.location.href = "index.php"; 
         </script>'; 

         } 

         else 
         { 

         echo ' <script> 
          alert("Password and Confirm Password do not match"); 
          window.location.href = "registeruser.php"; 
          </script>'; 

         } 
        } 
       } 
      } 
     } 
    } 
} 
?> 

oder sonst können Sie dieses registration.php verwenden

<?php 
if(isset($_GET[fname]) && $_GET[fname]==1) 
    echo ' <script> 
      alert("Full Name is required!"); 
     </script>'; 
    if(isset($_GET[uname]) && $_GET[uname]==1) 
     echo ' <script> 
      alert("Invalid username!"); 
     </script>'; 
    if(isset($_GET[uname]) && $_GET[uname]==2) 
    echo ' <script> 
       alert("Username already exists!"); 
      </script>'; 
    if(isset($_GET[pwrd]) && $_GET[pwrd]==2) 
    echo ' <script>alert("Password and Confirm Password do not match"); 
          </script>'; 
    if(isset($_GET[pwrd]) && $_GET[pwrd]==1) 
    echo ' <script>alert("Password is invalid"); 
          </script>'; 
    if(isset($_GET[mail]) && $_GET[mail]==1) 
     echo ' <script>alert("invalid mailid"); 
          </script>'; 

?> 
<form method=POST action="reg.php"> 
<!--input goes here--> 
</form> 

reg.php

<?php 

session_start(); 

include_once "connect.php"; 
if(isset($_POST[submit])){ 
$fullName = $_POST['fullname']; 
$userName = $_POST['username']; 
$emailAdd = $_POST['email']; 
$passWord = $_POST['password']; 
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' "); 
$result = mysql_fetch_array($query); 

    if($fullName == "") 
    { 
       header('Location:registration.php?fname=1'); 


    } 

    else 
    { 
     if ($userName == "") 
     { 
       header('Location:registration.php?uname=1'); 

     } 

     else 
     { 
      if($userName == $result['USERNAME']) 
      { 

      header('Location:registration.php?uname=2'); 
      } 

      else 
      { 
       if ($emailAdd == $result['EMAIL']) 
       { 
       header('Location:registration.php?mail=1'); 

       } 

       else 
       { 
        if ($passWord == "") 
        { 

           header('Location:registration.php?pwrd=1'); 
        } 

        else 
        { 
         if($_POST['password']==$_POST['confirmpass']) 
         { 
         mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) 
         VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')"); 
         mysql_close(); 

         echo ' <script> 
          alert("Registered Successfully!"); 
          window.location.href = "index.php"; 
         </script>'; 

         } 

         else 
         { 

      header('Location:registration.php?pwrd=2'); 
         } 
        } 
       } 
      } 
     } 
    } 
} 
?>