HTML 输入值更改 [英] HTML input value change

查看:36
本文介绍了HTML 输入值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PHP 更新页面,我在其中显示了一个包含数据库值的文本字段.是这样的,而且效果很好,

I have a PHP update page in which I am showing a text field containing a value from the database. It is like this, and it is working,

<input type="text" name="title" id="title" class="text_box" value="<?php echo $row['title']?>"/>

现在我需要把这个更新的值放回数据库中!我已经使用了这样的代码,但它没有更新:

Now I need to put this updated value back in the database! I have used the code like this, but it's not updating:

$title=$_POST['title'];
$v_id = $_GET['v_id'];
$sql = mysql_query("update vehicles set title = '$title' where v_id = '$v_id'");

详细地...有一个输入字段.它显示了包含在 $title(从数据库中检索)中的值,并且该值将被编辑和更新.

In detail... an input field is there. It's showing a value contained in $title (retrieved from the database) and that value is to be edited and updated.

在我看来,我的代码运行良好,没有显示任何错误,但我给 $title 的值是相同的,没有任何更改.

From my side my code is working perfectly without showing any error, but the value that I give $title is giving the same one without any change.

有没有其他方法可以在不放入值"标签的情况下在输入字段中显示值?在单个输入字段中要发生两件事!

Is there any other way to show a value in an input field without putting in a "value" tag? Two things wants to happen in a single input field!

推荐答案

您还需要发布表单 HTML.

You'll need to post your form HTML as well.

除非您的表单如下所示,否则该代码将无法工作

Unless your form looks like the following, that code won't work

<form method='post' action='page.php?v_id=1'>
   <input type="text" name="title" id="title" class="text_box" value="<?php echo $row['title']?>"/>
</form>

这是因为您使用 $_GET 获取 id 字段并使用 $_POST 获取值字段

This is because you're using $_GET to get the id field and $_POST to get the value field

编辑你的编辑让水变得更浑了.我将假设您想要做的就是显示标题并让用户更新标题

EDIT Your edit has muddied the water a bit more. I'm going to assume all you want to do is show a title and let the user update the title

<?php
   if ($_POST) {
        $title = mysql_escape_string($_POST['title']);
        $id    = intval($_GET['v_id']);
        $sql   = "update vehicles set title = '$title' where v_id = '$id'";
        mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
   } 
   if (isset($_GET['v_id'])) {
       $id      = intval($_GET['v_id']);
       $sql     = 'SELECT title FROM vehicles WHERE v_id = ' . $id;
       $rs      = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
       $row     = mysql_fetch_assoc($rs);
       $title   = htmlspecialchars($row['title']);
   }
?>

<form method='post' action='?v_id=<?php echo $id?>'>
   <input type="text" name="title" id="title" class="text_box" value="<?php echo $title ?>"/>
   <input type='submit' value='update'>
</form>

这应该对你有用.我没有明显的错误测试,但这个想法是合理的.您还应该添加您认为必要的任何其他输入筛选.

This should work for you. I haven't error tested obviously, but the idea is sound. You should also add any other input screening you feel necessary.

这篇关于HTML 输入值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆