2

我有以下问题:我有一个注册表单。响应消息将由将通过 php 变量调用的 javascript 函数(颜色框)显示。

看起来像:

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

该变量无法正常工作。如果我设置:

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

它工作正常。

问题是当我要提交表单时。即使注册表未正确填写,该框也会启动。为了避免这个问题,我认为我必须找到一个类似于布尔值 true 或 false 的变量,如果该表单以正确的方式填写,并且答案来自我的服务器。我的想法是:

$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);

这样,我会将变量更改$result为应该起作用的子句。它没有。:(

我究竟做错了什么?

4

3 回答 3

1

在您的代码中

$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);

你为什么返回TRUE?如果这样做了,它将不会到达后面的行。

此外,您正在使用 $sqlmysql_query($sql)其中已经是一个 mysql 结果资源。

如果您想在查询成功时显示

<?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 } ?>
于 2012-02-29T11:52:06.833 回答
0

您对这段代码有很多错误。

像这样做:

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

抱歉,如果这不完全有效,你有点让我对 isset($_POST['submitted']) 部分感到困惑,你的问题并不完全清楚。

于 2012-02-29T10:27:35.273 回答
0

只是为了显示整个代码。以下所有查询都有效。

    <?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; ?> 
于 2012-03-01T08:33:11.777 回答