如何在输入字段内使用选择选项的当前值 [英] how to use the current value of a select option inside input field

查看:101
本文介绍了如何在输入字段内使用选择选项的当前值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xampp中使用phpmyadmin,我在那里制作了两个表;
表1:类别,其属性为cat_id和cat_name(其中cat_id是主键)
表2:项目的属性为item_id,item_name,item_price ... cat_id(其中item_id是主要的Key和cat_id是外键)
我也在phpmyadmin中建立了正确的关系。
问题是在php中的select标签中使用所选cat_name的值,即cat_id。
ps。我知道是SQL注入的主题。

I'm using phpmyadmin in Xampp, where I have made two tables; Table 1: Category with its attributes as cat_id and cat_name (where cat_id is a Primary Key) Table 2: Item with its attributes as item_id, item_name, item_price ... cat_id (where item_id is a Primary Key and cat_id is a Foreign Key) I have also made the correct relationship in phpmyadmin. The problem is to use the value i.e. cat_id of the selected cat_name in a select tag inside php. ps. Im aware of being a subject to SQL Injection.


PHP

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

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>


推荐答案

感谢会员在这里我找到答案所以即时通讯将它发布在这里,万一有人来这里寻找同样的事情。

Thanks to a member here i found the answer so im posting it here, in case anyone comes here looking for the same thing.

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

这篇关于如何在输入字段内使用选择选项的当前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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