1

new.php に基本的な投稿フォームがあり、new1.php からの php スクリプト変数メッセージをどのようにエコー (または表示) できるか疑問に思っています。

new.php:

 <body>
<div id="contact_form">
  <form name="contact" method="post" action="new1.php" id="contact">
    <fieldset>
      <label for="firstName" id="firstName_label">First Name*:</label>
      <input type="text" name="firstName" id="firstName" size="36" value="" class="text-input" />
      <label class="error" for="firstName" id="firstName_error">This field is required.</label>
      <br />
            <label for="lastName" id="lastName_label">Last Name:</label>
      <input type="text" name="lastName" id="lastName" size="36" value="" class="text-input" />
      <label class="error" for="lastName" id="lastName_error">This field is required.</label>
      <br />
      <label for="email" id="email_label">Email*:</label>
      <input type="text" name="email" id="email" size="36" value="" class="text-input" />
      <label class="error" for="email" id="email_error">This field is required.</label>
      <label class="error" for="email" id="email_error2">This is not a valid email address.</label>
      <br />
      <label for="postcode" id="postcode_label">Postcode:</label>
      <input type="text" name="postcode" id="postcode" size="12" value="" class="text-input" />
      <label class="error" for="postcode" id="postcode_error">This field is required.</label>

        <br />
      <input type="submit" name="submit" id="submit_btn" value="Send" />
    </fieldset>
  </form>
</div>
</body>

new1.php - $msg_to_user にメッセージを返します。ご覧のとおり、このページにエコーしますが、フォームがある new.php にエコー バックするにはどうすればよいでしょうか?

<?php
// set variables to null
$firstName   = "";
$lastName    = "";
$email       = "";
$postcode    = "";
$msg_to_user = "";


include_once "connect_to_mysql.php";

// set variables from form
// be sure to filter this data to deter SQL injection, filter before querying database
$firstName = $_POST['firstName'];
$lastName  = $_POST['lastName'];
$email     = $_POST['email'];
$postcode  = $_POST['postcode'];

$sql     = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
$numRows = mysql_num_rows($sql);

// first name is mandatory
if (!$firstName) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Please enter your name.</font></h4>';

    // email is mandatory
} else if (!$email) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Please type an email address ' . $firstName . '.</font></h4>';

    // check email is valid
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">' . $email . ' is not a valid email address.               </font></h4>';

    // check postcode is a number
} else if (!is_numeric($postcode)) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Postcode must be a numeric value.</font></h4>';

    // check postcode is greater than 4 chars
} else if (strlen ($postcode) < 4) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Postcode must be at least 4 characters.</font></h4>';

    // check postcode is less than 12 chars
} else if (strlen ($postcode) > 12) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Postcode must be less than 12 characters.</font></h4>';

    // check email doesn't exist    
} else if ($numRows > 0) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">' . $email . ' is already in the system.</font></h4>';

    // if all test passed, insert details into database
} else {
    $sql_insert = mysql_query("INSERT INTO newsletter (firstName, lastName, email, postcode, dateTime) 
                                                    VALUES('$firstName','$lastName','$email','$postcode',now() )") or die(mysql_error());

    $msg_to_user = '<br /><br /><h4><font color="0066FF">Thanks ' . $firstName . $lastName . ' , you have been added successfully.</font></h4>';




    $firstName = "";
    $lastName  = "";
    $email     = "";
    $postcode  = "";
}
?>

<html>
<body>

<p>First Name: <?php
echo $firstName;
?></p>
<p>Last Name: <?php
echo $lastName;
?></p>
<p>Email: <?php
echo $email;
?></p>
<p>Postcode: <?php
echo $postcode;
?></p>
<p>Message: <?php
echo $msg_to_user;
?></p>
</body>
</html>
4

3 に答える 3

1

ajaxまたはを使用php sessionsしてこれを行います。

を使ったやり方ですphp sessions

new1.php で

session_start();
$_SESSION['name']=$variable;

new.php で

session_start();
echo $_SESSION['name'];
于 2013-04-01T05:55:02.643 に答える
1

これを行う簡単な方法は、ヘッダー関数を使用して new1.php から new.php にリダイレクトすることです。リダイレクト中に、URL を介して $msg_to_user を渡します。

これを行うには、$msg_to_user に適切な値を割り当てた直後に、new1.php に次のコードを追加します。

header("location: new.php?msg_to_user=".$msg_to_user);
exit;

new.php に次のコードを追加します。

echo $_GET['msg_to_user']; //this will display the message

これを行う別の方法は、セッションを使用してページ全体の値にアクセスすることです。

new1.php に次のコードを挿入して、セッション変数にメッセージを設定します。

session_start();
$_SESSION['msg_to_user'] = $msg_to_user;
header("location: new.php);
exit;

そして、new.php に次のコードを追加して、new1.php で設定されたメッセージを取得します。

session_start();
if (isset($_SESSION['msg_to_user'])) {
    echo $_SESSION['msg_to_user'];
    unset($_SESSION['msg_to_user']);//After displaying the message unset it, to make sure that the message is displayed only once.
}
于 2013-04-01T06:04:03.727 に答える
0

セッションで検証メッセージを渡します。そして、$_SESSION['name']=''; そのメッセージ表示でセッション値を1回だけ削除します。

于 2013-04-01T05:58:17.017 に答える