0

I am currently creating an html form on a web page, and i was wondering if there was a way to add default values to the text boxes, but the user could also just delete the default text and enter in new text if he needed to???

What i currently have is this...

<html>
  <body>
    <form method="post">
      Suspend: <input type="text" name="sus"><br />
      Device Name: <input type="text" name="dev"><br />
      IP: <input type="text" name="ip"><br />
      Dependency: <input type="text" value="none" name="depend"><br />
      Email: <input type="email" value="abc@123.com" name="email">
      <input type="submit" value="submit" name="submit">
    </form>
    </body>
    </html>
 <?php
  if(isset($_POST['sumbit']))
 {
   $sus=$_POST['sus'];
   $dev=$_POST['dev'];
   $ip=$_POST['ip'];
   $depend=$_POST['depend'];
   $email=$_POST['email'];

    if(isset($sus) && isset($dev) && isset($ip) && isset($depend) && isset($email)
     {
       $update=mysqli_query($con, "UPDATE table SET Suspend=$sus, Device=$dev, IP=$ip,   Dependence=$depend, Email=$email WHERE id=$id");
      }
 }

With the email and dependency fields, they will be the default values 95% of the time, but not all the time, and my boss wants default values supplied for them, but also wants them to be able to change them... I then perform mysql queries with these $_POST results to update the database... right now, even if you change the text in the field in the form, it still enters in the default values in the database.. i need it to where if they do not change the default value, then the default value will be added to the database... any help would be greatly appreciated

4

4 回答 4

0

您可以像这样在 php 中获取值

$value = $_POST['email'];

如果您在输入框中输入了一些内容,您将获得默认值的值。

如果您使用html5,则需要使用占位符 attr,然后在此处查看:

http://davidwalsh.name/html5-placeholder

于 2013-04-22T01:41:38.973 回答
0

HTML 5 具有 INPUT 标记的新占位符属性,但是当您在字段中键入时,该值会消失。INPUT 标签也有 value 属性。

于 2013-04-22T01:44:57.777 回答
0

试着看看这个:占位符

占位符是显示在输入中的默认文本。但是,如果用户没有插入任何占位符值,则在提交表单时不会将其包含到帖子中。仍然会value=""被认可。

于 2013-04-22T01:46:00.407 回答
0

由于占位符 attr 仍然是相当新的并且不支持较旧的浏览器,因此这里有一个后备,可用于提供与占位符 attr 相同的功能。

if (!("placeholder" in document.createElement("input"))) {
    var inputs = document.querySelectorAll("input[placeholder]"),
        input,
        dateDefault;

    for (var i = 0, len = inputs.length; i < len; i++) {
         input = inputs[i];
         input.value = input.getAttribute("placeholder");

         input.addEventListener("focus", function() {
             dateDefault = this.getAttribute("placeholder");
             (this.value === dateDefault) && (this.value === "");
         }, false);
         input.addEventListener("blur", function() {
             (this.value === "") && (this.value === dateDefault);
         }, false);
    }
}

如果占位符 attr 不可用,它会使用您为占位符 attr 设置的值并将其分配给该值。然后使用一些js你可以检测输入字段是否为空或仍然具有原始值,或者用户是否输入。您可以轻松地将其调整为 jQuery 或使用 addEventListener、attachEvent 或任何您需要的方式来支持您需要的浏览器。希望这可以帮助!

于 2013-04-22T03:06:03.197 回答