Search code examples
phpvariablesbooleancolorboxclause

how to change variable into a clause


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?


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 } ?>