Question

I have the following problem: I have a registration form. The response message will be shown by a javascript function (colorbox) that will be called via a php variable.

It looks like:

<?php if ($result):?>
  <script type="text/javascript">no need to post, it is working fine</script>
<?php endif; ?>

That variable is not working how it should do. If I set:

<?php if **(isset($_POST['submitted']))**: ?>
  <script type="text/javascript"> no need to post it is working</script>
<?php endif; ?>

it works fine.

The problem is that when I'm going to submit the form. The box starts even if the registration form is not filled out correctly. To avoid that problem I thought I have to find a variable that is like a boolean true or false, if that form is filled in the right way which answer comes from my server. My thoughts were like:

$sql = mysql_query("INSERT INTO tabelle (x1, x2...) VALUES('$x1','$x2')") 
       or die (mysql_error()); 

if ($result = mysql_affected_rows() = 2) {
  return TRUE;
} 

$result = mysql_affected_rows(); 
$result = mysql_query($sql);

In that way I would have changed the variable $result into a clause that should work. It doesn't. :(

What am I doing wrong?

Was it helpful?

Solution

In your code

$sql = mysql_query("INSERT INTO tabelle (x1, x2...) VALUES('$x1','$x2')") 
       or die (mysql_error()); 

if ($result = mysql_affected_rows() = 2) {
  return TRUE;
} 

$result = mysql_affected_rows(); 
$result = mysql_query($sql);

why are you returning TRUE? If that is done, it won't get to the lines that come after.

Also you are using $sql in mysql_query($sql) which is already a mysql result resource.

If you want to show that on success of query

<?php
  ..
   $sql = mysql_query("INSERT INTO tabelle (x1, x2...) VALUES('$x1','$x2')") 
       or die (mysql_error()); 

if (mysql_affected_rows() == 2){ ?>
  <script type="text/javascript">no need to post, it is working fine</script>

<?php } ?>

OTHER TIPS

You have alot wrong with this code.

Do it like this:

<?PHP
$success = false;
$result = mysql_query("INSERT INTO tabelle (x1, x2...) VALUES('$x1','$x2')") or die (mysql_error()); 
if ($affected = mysql_affected_rows() == 2) { $success = true; }
?>
<?PHP if ($success){ ?>
<script type="text/javascript">no need to post, it is working fine</script>
<?PHP } ?>

Sorry if that doesnt completely work, you kind of confused me with the isset($_POST['submitted']) part and your question wasn't entirely clear.

just to show the entire code. all of the following querys are working.

    <?php

$errorMsg = "";
$x1 = "";
$x2 = "";
$x3 = "";
$x4 = "";
$email = "";
$password = "";
$x7= "";

if (isset ($_POST['x1'])){

     $x1 = $_POST['x1'];
     $x2 = $_POST['x2'];
     $x3 = $_POST['x3'];
     $x4 = $_POST['x4'];
     $email = $_POST['email'];
     $password = $_POST['password'];
     $x7= $_POST['x7'];

         $x1 = stripslashes($x1);
     $x2 = stripslashes($x2);
         $x3 = stripslashes($x3);
     $x4 = stripslashes($x4);
     $email = stripslashes($email);
     $password = stripslashes($password);
     $x7= stripslashes($x7);

         $x1 = strip_tags($x1);
     $x2 = strip_tags($x2);
     $x3 = strip_tags($x3);
     $x4 = strip_tags($x4);
     $email = strip_tags($email);
     $password = strip_tags($password);
     $x7= strip_tags($x7);

     include_once "scripts/connect_to_mysql.php";
     $emailCHecker = mysql_real_escape_string($email);
     $emailCHecker = eregi_replace("`", "", $emailCHecker); 

     $sql_email_check = mysql_query("SELECT email FROM tablename WHERE email='$emailCHecker'"); 

     $email_check = mysql_num_rows($sql_email_check); 

     if ((!$x1) || (!$x2) || (!$x3) || (!$x4) || (!$email) || (!$password) || (!$x7)) { 

     $errorMsg = 'following issues are missing<br />';

    if(!$x1){ 
       $errorMsg .= ' *x1<br />'; 
     } 
     if(!$x2){ 
       $errorMsg .= ' *x2<br />';
     } 
     if(!$x3){ 
       $errorMsg .= ' *x3<br />';
     } 
     if(!$x4){ 
       $errorMsg .= ' *x4<br />'; 
     } 
     if(!$email){ 
       $errorMsg .= ' *email<br />'; 
     }
     if(!$x7){ 
       $errorMsg .= ' *x7<br />'; 
     }  

     } else if ($email_check > 0){ 
       $errorMsg = "Mail already exists"; 

     } else 

     { 

     $x1 = mysql_real_escape_string($x1);
     $x2 = mysql_real_escape_string($x2);
     $x3 = mysql_real_escape_string($x3);
         $x4 = mysql_real_escape_string($x4);
     $email = mysql_real_escape_string($email);
     $password = mysql_real_escape_string($password);

     $x1 = eregi_replace("`", "", $x1);
     $x2 = eregi_replace("`", "", $x2);
     $x3 = eregi_replace("`", "", $x3);
     $x4 = eregi_replace("`", "", $x4);
     $email = eregi_replace("`", "", $email);

     $db_password = md5($password); 

     $sql = mysql_query("INSERT INTO tablename (x1, x2, x3, x4, sign_up_date, email, password)    

     VALUES('$x1','$x2','$x3','$x4', now(), '$email','$db_password')")  

     or die (mysql_error()); 

     $id = mysql_insert_id();

     /*mkdir("members/$id", 0755); */

    $to = "$email"; 

    $from = "xxx@xxx.xx";       
    $subject = "activationlink";        

    $message = "Link for activation";

    $headers  = "From: $from\r\n";
    $headers .= "Content-type: text\r\n";

    mail($to, $subject, $message, $headers);

   $msgToUser = "text";


   /*include_once 'msgToUser.php';  */



   exit();

   } 

} else { 

      $errorMsg = "Errormessages";
          $x1 = "";
      $x2 = "";
      $x3 = "";
      $x4 = "";
      $email = "";
      $password = "";
      $x7 = "";


}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XXXX</title>

        <link rel="stylesheet" href="css/colorbox.css" />
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> 
        <script type="text/javascript" src="js/jquery.colorbox.js"></script>   

<?php if **a variable or clause will be true: ?>
        <script type="text/javascript">
        $(document).ready(function(){
        $.colorbox({iframe:true, scrolling:false, href:"#$msgToUser.php", innerWidth:"408px", innerHeight:"292px", opacity:0.75, overlayClose:false, escKey:false, });
        });
        $("#colorboxCloseBtn").click(function() {
        $.colorbox.close();
        });
        $(document).ready(function() {
        /* Automatically resize to content 
        var y = $(document.body).height();
        var x = $(document).width();
        parent.$.colorbox.resize({innerWidth:x, innerHeight:y});*/

        $("#button_cancel").click(function() {
        parent.$.colorbox.close();
        return false;
        })
        });
        </script> 
 <?php endif; ?> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top