将Cookie值输出为html格式

时间:2017-11-15 14:52:06

标签: javascript php jquery html cookies

我试图在我的网站上实现一些cookie。理想情况下,我想将用户html表单输入保存到cookie中,以便自动设置以供以后参考。但是,此表单还将值发送到数据库,如下所示:

<?php 
session_start();
?>
<?php

if($_POST['formSubmit'] == "Submit") 
{
    $errorMessage = false;

    if(empty($_POST['formName'])) 
    {
        $errorMessage = true;
    }
    if(empty($_POST['formEmail'])) 
    {
$errorMessage = true;       }
    if(empty($_POST['formAddress'])) 
    {
$errorMessage = true;       }
if(empty($_POST['formPrice'])) 
    {
$errorMessage = true;    }


$varName = $_POST['formName'];
    $varEmail = $_POST['formEmail'];
    $varAddress = $_POST['formAddress'];
$varPrice = $_POST['formPrice'];
    $varComments = $_POST['formComments'];

     if($errorMessage == false) 
    {

     $db = mysql_connect("","","");
  if(!$db) die("Error connecting to MySQL database.");
  mysql_select_db("" ,$db);

        $sql = "INSERT INTO formdata (name, email, address, price, comments) 
VALUES (".
                        PrepSQL($varName) . ", " .
                        PrepSQL($varEmail) . ", " .
                        PrepSQL($varAddress) . ", " .
          PrepSQL($varPrice) . ", " .
                        PrepSQL($varComments) . ")";
        mysql_query($sql);

        header("Location: thankyou.php");
        exit();
    }
}

//sql injection protection..
function PrepSQL($value)
{

    if(get_magic_quotes_gpc()) 
    {
        $value = stripslashes($value);
    }


    $value = "'" . mysql_real_escape_string($value) . "'";

    return($value);
}


?>
<!DOCTYPE html>
<html>        
<body>

<div class="Formm">
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <?php
    if ( $errorMessage == true ) {
        echo( "<h3>PLEASE FILL IN ALL QUESTIONS WITH A STAR.</h3>" );

    }


    $result = mysqli_query( $link, "SELECT * FROM `formdata` WHERE user=$user ORDER BY id ASC LIMIT 1" );

    if ( mysqli_num_rows( $result ) > 0 ) {
        // output data of each row
        while ( $row = mysqli_fetch_assoc( $result ) ) {
            $id = $row[ "id" ];
            $varname = $row[ "varname" ];
            $varemail = $row[ "varemail" ];
            $varaddress = $row[ "varaddress" ];
            $varprice = $row[ "varprice" ];
            $varcomments = $row[ "varcomments" ];
        }
    }

    ?>
    <p>

        <label for='formName'>Venue name*</label><br/>
        <input type="text" name="formName" maxlength="100" value="<? echo($varName); ?>"/>
    </p>

    <p>
        <label for='formEmail'>Email*</label><br/>
        <input type="text" name="formEmail" maxlength="100" value="<? echo($varEmail);?>"/>
    </p>
    <p>
        <label for='formAddress'>Address*</label><br/>
        <input type="text" name="formAddress" maxlength="100" value="<? echo($varAddress);?>"/>
    </p>
    <p>
        <label for='formPrice'>Estimated price*</label><br/>
        <input type="text" name="formPrice" maxlength="100" value="<?php echo($varPrice);?>"/>
    </p>
    <p>
        <label for='formComments'>Any comments associated with the Venue that the artist should be aware of?</label><br/>
        <input type="text" name="formComments" maxlength="250" value="<? echo($varComments);?>"/>
    </p>
    <input type="submit" name="formSubmit" value="Submit"/>
</form>




<?php
include 'footer.php';
?>


</body>
</html>

NEW EDITIm尝试在我的网站上实施一些Cookie。理想情况下,我想将用户html表单输入保存到cookie中,以便自动设置以供以后参考。但是,此表单还将值发送到数据库,如下所示:

1 个答案:

答案 0 :(得分:0)

您可以使用您的数据库。创建一个脚本,该脚本获取用户最后输入的数据并将其放在表单

示例:

//query
$result = mysqli_query( $link, "SELECT * FROM `formdata` WHERE user=$user ORDER BY id ASC LIMIT 1" );

if ( mysqli_num_rows( $result ) > 0 ) {
    // output data of each row
    while ( $row = mysqli_fetch_assoc( $result ) ) {
        $id = $row[ "id" ];
        $name = $row[ "name" ];
        $email = $row[ "email" ];
        $address = $row[ "address" ];
        $price = $row[ "price" ];
        $comments = $row[ "comments" ];
        }
}

修改

这符合您的需求:

<div class="Formm">
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <?php
    if ( $errorMessage == true ) {
        echo( "<h3>PLEASE FILL IN ALL QUESTIONS WITH A STAR.</h3>" );

    }


    $result = mysqli_query( $link, "SELECT * FROM `formdata` WHERE user=$user ORDER BY id ASC LIMIT 1" );

    if ( mysqli_num_rows( $result ) > 0 ) {
        // output data of each row
        while ( $row = mysqli_fetch_assoc( $result ) ) {
            $id = $row[ "id" ];
            $varname = $row[ "varname" ];
            $varemail = $row[ "varemail" ];
            $varaddress = $row[ "varaddress" ];
            $varprice = $row[ "varprice" ];
            $varcomments = $row[ "varcomments" ];
        }
    }

    ?>
    <p>

        <label for='formName'>Venue name*</label><br/>
        <input type="text" name="formName" maxlength="100" value="<? echo($varName); ?>"/>
    </p>

    <p>
        <label for='formEmail'>Email*</label><br/>
        <input type="text" name="formEmail" maxlength="100" value="<? echo($varEmail);?>"/>
    </p>
    <p>
        <label for='formAddress'>Address*</label><br/>
        <input type="text" name="formAddress" maxlength="100" value="<? echo($varAddress);?>"/>
    </p>
    <p>
        <label for='formPrice'>Estimated price*</label><br/>
        <input type="text" name="formPrice" maxlength="100" value="<? echo($varPrice);?>"/>
    </p>
    <p>
        <label for='formComments'>Any comments associated with the Venue that the artist should be aware of?</label><br/>
        <input type="text" name="formComments" maxlength="250" value="<? echo($varComments);?>"/>
    </p>
    <input type="submit" name="formSubmit" value="Submit"/>
</form>