简体   繁体   中英

How to place my message where I want it using php

this is the code I have, I need my message to appear in the top of my form but I got an error. I need to print my message right where I put the code print $msg

any idea about this please.

<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>";
                }

this is the code I have, I need my message to appear in the top of my form but I got an error. I need to print my message right where I put the code print $msg

any idea about this please.

您确实需要设置消息, 然后再尝试将其输出。

To elaborate on David's answer: move the PHP block before the form markup, like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM