表单在每个字段中显示“/”

时间:2011-09-03 18:56:58

标签: php html webforms

我发送邮件的php脚本如下:

<?php
    if (isset($_POST['submit'])) {
        $to='info@animalswecare.com';
        $fname=stripslashes($_POST['fname']);
        $email=$_POST['email'];
        if (get_magic_quotes_gpc())
            $email = stripslashes($email);
        //$email=trim($email, '/');
        $msg=$_POST['msg'];
        $msg=stripslashes($msg);
        $message="Name: $fname\n" ."Message: $msg\n";
        mail($to,$subject,$message,'From:'.$email)  ;
    }
   ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact us">
  <label>Name:</label>
  &nbsp;&nbsp;&nbsp;
  <input type="text" name="fname" value=<?php if(!empty($fname)) echo $fname; ?>  /><br />
  <label>Email:</label>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="email" value=<?php if (!empty($email)) echo $email; ?> /><br />
  <label>Message:</label>
  <textarea name="msg" rows="5"><?php if (!empty($msg)) echo $msg; ?></textarea>      <br />
  <input type="submit" name="submit" value="Post" />
</form> 

但是当表单出现时,每个字段都会添加/。 我尝试过使用trimrtim,获取魔法引号和stripslashes,但没有任何效果。

4 个答案:

答案 0 :(得分:3)

因为你的value=没有结束。

试试这个:<input type="text" name="fname" value="<?php if(!empty($fname)) echo $fname; ?>" />并对所有输入应用相同的学习。

我还重新格式化了你的代码并在CSS中添加了label。所以你没有使用那些丑陋的行空间。

<?php

if (isset($_POST['submit'])) {
    $to = 'info@animalswecare.com';
    $fname = stripslashes($_POST['fname']);
    $email = $_POST['email'];

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

    // $email = trim($email, '/');
    $msg = $_POST['msg'];
    $msg = stripslashes($msg);    
    $message = "Name: $fname\n" ."Message: $msg\n";
    mail($to, $subject, $message, 'From:' . $email);
}

?>

<style>
    label {width: 120px;}
</style>

<form action="" method="post">

    <label>Name:</label>
    <input type="text" name="fname" value="<?php if(!empty($fname)) echo $fname; ?>"  /><br />
    <label>Email:</label>
    <input type="text" name="email" value="<?php if (!empty($email)) echo $email; ?>" /><br />
    <label>Message:</label>
    <textarea name="msg" rows="5"><?php if (!empty($msg)) echo $msg; ?></textarea><br />
    <input type="submit" name="submit" value="Post" />

</form> 

答案 1 :(得分:1)

尝试在值属性

周围添加“”

<input type="text" name="email" value="<?php if (!empty($email)) echo $email; ?>" />

答案 2 :(得分:0)

这将教您遵循标准,始终将标记参数值括在引号中

答案 3 :(得分:0)

首先,不要使用stripslashes()。只需使用php.ini文件或.htaccess完全禁用magic_quotes_gpc即可。如果您不能,请查看此问题的接受答案:How to turn off magic quotes on shared hosting?

关于您的错误......

value=<?php if (!empty($email)) echo $email; ?> />

如果你看,你没有在“value”属性周围加上引号。

value="<?php if (!empty($email)) echo $email; ?>" />

会解决它