php代码从多选列表框中获取计数并存储在数据库中

时间:2015-03-20 14:22:57

标签: javascript php mysql

下面是我的文本框值....我无法将屏幕截图放在这里..

我在这个网站上使用了多个输入标签jquery ..

http://loopj.com/jquery-tokeninput/demo.html

和此演示的第二个选项。 plz访问演示链接...

但我使用了多个输入文本框..我们可以在一个文本框中选择多个标签。

如700X 701X 702X

我需要获取此文本框值并存储在3行..

FOR EX - 在上面的截图中有3个值700,701,702确定...现在我需要问你...当我点击保存时我需要将这些值存储在3个不同的行中...

Rows No   -   used_receipt
 1                 700
 2                 701
 3                 702

我尝试如下,但不会工作......

文本框代码

<input  id="demo-input-local" type="text"  value="<?php echo $data['used_receipt'];?>" name="used_receipt" />

的javascript

 <script type="text/javascript">
        $(document).ready(function() {
            $("#demo-input-local").tokenInput([
<?php 
$receipt = $database->getRows("SELECT DISTINCT SM.receipt_no FROM scheme_master SM Inner join book_issue BI ON BI.book_no = SM.Book_no2  where SM.receipt_no not in (select used_receipt from book_return)");       
 foreach($receipt as $row){ ?>

                {name: "<?php echo $row['receipt_no']; ?>"},
               <?php } ?>


            ]);
        });
        </script>

php代码在数据库中插入多个值

$used_receipt = $_POST['used_receipt']; 
$arr = explode(",", $used_receipt);
$max = count($arr);

for ($i = 0; $i < $max; $i++)
{
$insertrow = $database->insertRow("INSERT INTO book_return (book,surveyor,used_receipt,city,return_date,created)    
                         VALUES  (:book,:surveyor,:used_receipt,:city,:return_date,:created)",  
                         array(':used_receipt'=>$arr[$i]);              
}

2 个答案:

答案 0 :(得分:0)

而不是

foreach($receipt as $row){ ?>

            {name: "<?php echo $row['receipt_no']; ?>"},
           <?php } ?>

试试这个

$r = array();$i=1;
foreach($receipt as $row){ 
$r[]['name'] = $row['receipt_no'];$r[]['id'] = $i++;
}
echo "JSON.parse(\"".json_encode($t)."\")";

答案 1 :(得分:0)

下面是我使用的脚本

<script type="text/javascript">
        $(document).ready(function() {
            $("#demo-input-local").tokenInput([<?php 
$receipt = $database->getRows("SELECT DISTINCT SM.receipt_no FROM scheme_master SM Inner join book_issue BI ON BI.book_no = SM.Book_no2");      
 foreach($receipt as $row){ ?>          
               {id:<?php echo $row['receipt_no']; ?>,name: "<?php echo $row['receipt_no']; ?>"},
               <?php } ?>
            ]);
        });
        </script>

并在

中输入此输入
$used_receipt = $_POST['used_receipt']; 
            $arr = explode(",", rtrim($used_receipt));