【问题标题】:Output Cookie value into html form [closed]将 Cookie 值输出为 html 表单 [关闭]
【发布时间】:2017-11-16 06:02:57
【问题描述】:

我正在尝试在我的网站上实施一些 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>

新编辑我正在尝试在我的网站上实施一些 cookie。理想情况下,我想将用户 html 表单输入保存到 cookie 中,以便自动设置以供以后参考。然而,这种形式也会将值发送到数据库,如下所示:

【问题讨论】:

  • 这里没有足够的代码,你提到了一个数据库;问题不清楚。
  • @Fred-ii- 很抱歉,希望这次编辑能澄清一点
  • 我注意到了编辑:任何地方都没有 cookie 代码。我们知道的越少,花费的时间就越多,记住这一点。我现在将不得不传递这个,让其他人看看你的帖子。
  • @Fred-ii- 明白了。只是我对如何实现这一点没有足够的了解。希望这个伪代码有点帮助。
  • 我建议阅读 W3School's page 上关于 javascript 的 cookie。如果您仍然遇到问题,请使用不适合您的代码更新您的问题。您的问题目前没有显示您尝试设置 cookie 的代码或努力,因此它可能会出现“有人可以为我写我的 cookie 代码吗?”即使那不是你的意图。

标签: javascript php jquery html cookies


【解决方案1】:

您可以为此使用您的数据库。制作一个脚本,从用户那里获取最后输入的数据并将其放入表单中

例子:

//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>

【讨论】:

  • 非常感谢您的回答,这似乎正是我想要的!然而,我想知道如何在代码中准确地实现这一点。您是说将此代码放在表单开头的 代码中?
  • 将其放在 html 表单的输出之前并将变量打印/回显到表单字段
  • 我在主帖中编辑了我的代码,请您进一步协助我将这些值打印到它似乎不起作用的字段中,请原谅我缺乏知识。谢谢
  • @Jeroen 是的,我会回家的时候
  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-09
  • 1970-01-01
  • 2011-05-13
  • 2015-02-10
  • 1970-01-01
  • 2021-07-31
相关资源
最近更新 更多