如何使用PHP将我的消息放在我想要的地方

时间:2011-12-20 22:11:01

标签: php html forms

这是我的代码,我需要将消息显示在表单的顶部,但是我收到了错误。 我需要在我输入代码的地方打印我的消息$ msg

对此有任何想法。

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
      <table width="300" border="0">
      <tr>
        <td colspan="2"><?php print($msg); ?></td>
        </tr>
      <tr>
          <td> *First name</td>
          <td><span id="sprytextfield3">
            <input type="text" value="" name="RFname" />
            <br>
            <span class="textfieldRequiredMsg">A value is required.</span></span></td>
        </tr>
        <tr>
          <td>*Last name</td>
          <td><span id="sprytextfield4">
            <input type="text" value="" name="RLname" />
            <br>
            <span class="textfieldRequiredMsg">A value is required.</span></span></td>
        </tr>
        <tr>
          <td>*User name</td>
          <td><span id="sprytextfield1">
            <input type="text" name="UserName" value="" />
            <br />
            <span class="textfieldRequiredMsg">A value is required.</span></span></td>
        </tr>
        <tr>
          <td>*Password</td>
          <td><span id="sprypassword1">
            <input type="password" name="UserPass" value="" />
            <span class="passwordRequiredMsg"><br />
            A value is required.</span></span></td>
        </tr>
        <tr>
          <td>*Email</td>
          <td><span id="sprytextfield2">
            <input type="text" name="UserEmail" value="" />
            <br />
            <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
        </tr>
        <tr>
          <td>Web site</td>
          <td><input type="text" name="WebSite" value="" /></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input name="submited" type="submit" onClick="MM_swapImage('Image1','','images/blog.png',1)" value="Register"/></td>
        </tr>
      </table>
    </form>
    </div>
    <div class="image_NewUserRegister"><iframe src="images/Untitled-2.jpg" name="now_regist" width="207" height="280" scrolling="no" frameborder="0"></iframe></div>
  </div>
  <?php
        if(isset($_POST['submited'])) {
            $RFname = $_POST['RFname'];
            $RLname = $_POST['RLname'];
            $USERname = $_POST['UserName'];
            $USERpassword = $_POST['UserPass'];
            $USERemail = $_POST['UserEmail'];
            $USERwebsite = $_POST['WebSite'];

            $check=$db->query("select 1 from loginaccess where Email ='".$USERemail."'");

            if(mysqli_num_rows($check) > 0 ) {
                $msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This email already taken</span>";
                }

这是我的代码,我需要将消息显示在表单的顶部,但是我收到了错误。我需要在我输入代码的地方打印我的消息$ msg

对此有任何想法。

2 个答案:

答案 0 :(得分:2)

您确实需要在之前设置消息,然后尝试输出它。

答案 1 :(得分:1)

详细说明David的答案:在表单标记之前移动PHP块,如下所示:

  <?php
        if(isset($_POST['submited'])) {
            $RFname = $_POST['RFname'];
            $RLname = $_POST['RLname'];
            $USERname = $_POST['UserName'];
            $USERpassword = $_POST['UserPass'];
            $USERemail = $_POST['UserEmail'];
            $USERwebsite = $_POST['WebSite'];

            $check=$db->query("select 1 from loginaccess where Email ='".$USERemail."'");

            if(mysqli_num_rows($check) > 0 ) {
                $msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This email already taken</span>";
            }
  ?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
      <table width="300" border="0">
      <tr>
        <td colspan="2"><?php print($msg); ?></td>
        </tr>
      <tr>
          <td> *First name</td>
          <td><span id="sprytextfield3">
            <input type="text" value="" name="RFname" />
            <br>
            <span class="textfieldRequiredMsg">A value is required.</span></span></td>
        </tr>
        <tr>
          <td>*Last name</td>
          <td><span id="sprytextfield4">
            <input type="text" value="" name="RLname" />
            <br>
            <span class="textfieldRequiredMsg">A value is required.</span></span></td>
        </tr>
        <tr>
          <td>*User name</td>
          <td><span id="sprytextfield1">
            <input type="text" name="UserName" value="" />
            <br />
            <span class="textfieldRequiredMsg">A value is required.</span></span></td>
        </tr>
        <tr>
          <td>*Password</td>
          <td><span id="sprypassword1">
            <input type="password" name="UserPass" value="" />
            <span class="passwordRequiredMsg"><br />
            A value is required.</span></span></td>
        </tr>
        <tr>
          <td>*Email</td>
          <td><span id="sprytextfield2">
            <input type="text" name="UserEmail" value="" />
            <br />
            <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
        </tr>
        <tr>
          <td>Web site</td>
          <td><input type="text" name="WebSite" value="" /></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input name="submited" type="submit" onClick="MM_swapImage('Image1','','images/blog.png',1)" value="Register"/></td>
        </tr>
      </table>
    </form>
    </div>
    <div class="image_NewUserRegister"><iframe src="images/Untitled-2.jpg" name="now_regist" width="207" height="280" scrolling="no" frameborder="0"></iframe></div>
  </div>

HTH。