如何在输入字段中使用选择选项的当前值

Posted

技术标签:

【中文标题】如何在输入字段中使用选择选项的当前值【英文标题】:how to use the current value of a select option inside input field 【发布时间】:2015-10-21 18:26:03 【问题描述】:

我在 Xampp 中使用 phpmyadmin,我在其中创建了两个表; 表 1:具有 cat_id 和 cat_name 属性的类别(其中 cat_id 是主键) 表 2:具有 item_id、item_name、item_price ... cat_id 属性的项目(其中 item_id 是主键,cat_id 是外键) 我也在phpmyadmin中建立了正确的关系。 问题是在 php.ini 中的选择标记中使用值,即所选 cat_name 的 cat_id。 附言。我知道自己是 SQL 注入的对象。

PHP

<?php  
require ('config.php'); 
if(isset($_POST['check']))

if(isset($_POST['button'])) 

$catname = $_POST['cat'];

$que1 = "SELECT * FROM category WHERE cat_name = '$catname'";
$res1 = mysql_query($que1);
$row = mysql_fetch_array($res1);
$cat_db = $row['cat_name'];

if($catname == $cat_db || $catname == "")
   
    echo "Catergory: $catname already exits. Failed to be inserted.";

else

    $que = "INSERT INTO category (cat_name) VALUES('$catname')";
    $res = mysql_query($que);
    echo "Catergory: $catname inserted successfully.";


die();



  if(isset($_POST['item_name']))
  
      $i_id = $_POST['item_id'];
      $i_name = $_POST['item_name'];
      $i_quan = $_POST['item_quantity'];
      $i_size = $_POST['item_size'];
      $i_price = $_POST['item_price'];
      $cat_id = $_POST['cat_id'];

$que = "INSERT INTO item(item_name, item_quantity, item_size, item_price, cat_id) VALUES('$i_name','$i_quan','$i_size','$i_price', '$cat_id')";
$run = mysql_query($que);
if(!$run)
   echo "Item Details failed to Update.";       
  

?>

html

<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" href = "login.css">
</head>
<body>
<form action = "" method = "POST">

<p><label class = "field">Add Category:</label></p>
<input type = "text" name = "cat" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"  title = "Please enter your Category Name">
<button type=  "submit" onclick = "location.href = '';" id = "savebutton" name = "button">Add Now</button>
<p><label class = "field">Add Item:</label></p>


<select name="cate">
<?php 
$que1 = "SELECT * FROM category";
$res1 = mysql_query($que1);
while($row = mysql_fetch_array($res1))
    
$cat_id_db = $row['cat_id'];                 //use array over here
$cat_db = $row['cat_name'];                  //use array over here
?>

<option value="<?php echo $cat_id_db; ?>" ><?php echo $cat_db;?></option>

<?php  ?>
</select>


<?php 
$que1 = "SELECT * FROM category WHERE cat_name = '$cat_db'";      //yahan masla hai bhai, how do i set '$cat_db' into a static variable?
$res1 = mysql_query($que1);
$row = mysql_fetch_array($res1);
$cat_db_id = $row['cat_id'];
?>

<p><label class = "field">Category ID:</label></p>
<input type = "text" name = "cat_id" value = "<?php echo $cat_id_db; ?>" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"> <!-- this is the PROBLEM how do i use the value of a selected option ONLY? -->
 <p><label class = "field">Item ID:</label></p>
 <input type = "text" name = "item_id" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"title = "Please enter your Item ID">
 <p><label class = "field">Item Name:</label></p>
 <input type = "text" name = "item_name" class = "textbox-300" pattern = "[a-zA-Z ]+"title = "Please enter your Item Name">
 <p><label class = "field">Item Quantity:</label></p>
 <input type = "text" name = "item_quantity" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"title = "Please enter your Item Quantity">
 <p><label class = "field">Item Size:</label></p>
 <input type = "text" name = "item_size" class = "textbox-300" pattern = "[a-zA-Z0-9\.\, ]+"title = "Please enter your Item Size">
 <p><label class = "field">Item Price:</label></p>
 <input type = "text" name = "item_price" class = "textbox-300" pattern = "[a-zA-Z0-9\.\, ]+"title = "Please enter your Item Price">


 <input type = "hidden" name = "check">
 <input type = "submit" class = "button" name = "sub"  value = "Save">

 </form> 
 </body>
 </html>

【问题讨论】:

我很难理解这个问题,起初我以为是因为您的select 的名称为cate,但您在php 中寻找cat 但后来意识到您有一个名为 cattext 字段。如果您在问题中缩小代码以便仅存在相关代码而不是整个 html 页面和 php 脚本,则更容易回答。 @martincarlin87,对不起。我已经评论了我遇到问题的行。请查看

标签下方。
【参考方案1】:

您将拥有所选子猫的 ID。所以写一个查询来获取 cat_id。

select * from sub_category where item_id = "posted id" 你会得到主分类id。

【讨论】:

您的意思是在查询中使用选定的选项吗?就像是; SELECT * FROM 类别 WHERE cat_name = ' ? ';我写什么代替'? -.-【参考方案2】:

感谢这里的一位成员,我找到了答案,所以我把它贴在这里,以防有人来这里寻找同样的东西。

<select name="cate" onchange="changeInput(this.value);">
    <?php //your php code here ?>
</select>

<p><label class = "field">Category ID:</label></p>
<input type = "text" name = "cat_id" id="cat_id" value = "change select to see me change" class = "textbox-300" pattern = "[a-zA-Z0-9\. ]+"  title = "Please enter your Item Price">
</form>

<script>
var changeInput = function (val)
    var input = document.getElementById("cat_id");
    input.value = val;

</script> 

【讨论】:

以上是关于如何在输入字段中使用选择选项的当前值的主要内容,如果未能解决你的问题,请参考以下文章

如何在日期选择器中显示当前日期

如何使用 php 验证选择字段?

在“选择”/下拉 HTML 列表中手动输入值?

如何验证下拉列表中的“其他”选项?

如何使用 jQuery 交换选择字段中的所有选项

在 php mysql 中选择选项时用值填充输入字段