doubinei1457 2015-03-16 22:28
浏览 9
已采纳

使用PHP和搜索选项保存特定行

I have the following program, it searchs for the text placed in a previous php file, and it displays the results, by adding a radiobox to check the item that will be purchased. I am not able to make the page save the item that was checked from the items found into a new table, I don't know how to do that, because the items found are placed as fetched items, therefore I don't know how to select one to save the entire row selected. Please help!.

<!doctype html>
<html>
<head>

<meta charset="utf-8">
<title>Search option</title>

</head>

<body>
<?php
echo "<form action='slips.php' method='post'>";
if(isset($_POST['name_prod2'])){ 
$word=$_POST['name_prod2']; 

$conn = oci_pconnect('dbname', 'password', 'localhost/XE');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['Error'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, "SELECT * FROM product WHERE LOWER(name) LIKE '%" . $word . "%'");
oci_execute($stid);

echo "<table width='950' table border='1' align='center'>
";
    echo "<tr>
";
    echo "<th width='50'> <div align='center'>buy</div></th>";
    echo "<th width='110'> <div align='center'>Product ID</div></th>";
    echo "<th width='190'> <div align='center'>Product name</div></th>";
    echo "<th width='250'> <div align='center'>Description</div></th>";
    echo "<th width='100'> <div align='center'>in Store</div></th>";
    echo "<th width='100'> <div align='center'>price</div></th>";
    echo "<th width='190'> <div align='center'>Quantity to purchase</div></th>";
    echo "</tr>
";

while ($product = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {

    echo "<tr>
";  
    //echo "<td><div style='text-align:center'><label><input type='radio' name='radio1' value='valor'></label></td></div>";
    echo sprintf('<td><div style="text-align:center"><label><input type="radio" name="product" value="%s"></label></td></div>', $product['product_id']);

    foreach ($product as $aspect) {
    echo '<td><div style="text-align:center">'.($aspect !== null ? htmlentities($aspect, ENT_QUOTES) : '')."</td></div>
";
    }
    echo '<td width="50"><div align="center"><input name="quantity" type="text" size="27" maxlength="50" placeholder="Enter quantity"></div></td>';

}
echo "</table>
";
}
echo "<div style='text-align:center'><input type='submit' value='Comprar'></div>";
echo"</form>";
?>

</body>
</html>

These are two of the Javascripts that I have tried so far to complete this, but they fail to tell me when one has been selected, I don't know if I can try adding this code to a button, and when I click it, it will tell me which row from the radio box was checked, and then save it:

<script type="text/javascript" src="js/jquery.js"> </script>
<script type="text/javascript"> 
var user_cat = $("input[radio1='user_cat']:checked").val();
if (!$("input[radio1='radio1']").is(':checked')) {
   alert('Nothing is checked!');
}
else {
   alert('One of the radio buttons is checked!');
}

$(document).ready(function() {
   $('#btnStatus').click(function(){
      var isChecked = $('#rdSelect').prop('checked');
      alert(isChecked);
   });
});

</script>

When is use this line echo "<td><div style='text-align:center'><label><input type='radio' name='radio1' value='valor'></label></td></div>"; it works and displays this results

But when I use this line echo sprintf('<td><div style="text-align:center"><label><input type="radio" name="product" value="%d"></label></td></div>', $product['product_id']);it doesn't work and displays this results

  • 写回答

2条回答 默认 最新

  • dongmi8980 2015-03-18 19:21
    关注

    OK, picking up the information from the comments to the question and doing a little guess work I will try to point you into the right direction. It is not possible to give a read-to-use answer, since still there are things not clear, but let's have a try to get started...

    I see you have an html form which includes a table. That table has a header row and dynamic generated rows holding some product information each. You want to have a radio button in front of each row to allow to select a row. And you want a text input field at the end of each row which allows to enter a quantity. Then you want to post that information to the server to be able to process it.

    I will stick with the "conservative" html approach and not introduce scripting here. Reason is that for the purpose described before that is not required. So let's keep things simple. Obviously nothing speaks against making things more complicated later on :-)

    Your radio buttons have to be changed, they currently make no sense. You have to give the an individual value, so that you can identify which row has been selected later on. Currently you give them all the same static value 'value'. So change the loop that iterates over the products to something like:

    while ($product = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
        echo "<tr>
    ";  
        echo sprintf('<td><div style="text-align:center"><label><input type="radio" name="product" value="%s"></label></td></div>'."
    ", $product['product_id']);
    
        foreach ($row as $aspect) {
            echo '<td><div style="text-align:center">' 
                .($aspect !== null ? htmlentities($aspect, ENT_QUOTES) : '')
                ."</td></div>
    ";
        }
        echo '<td width="50"><div align="center"><input name="quantity" type="text" size="27" maxlength="50" placeholder="Enter quantity"></div></td>'."
    ";
    }
    

    Note: I took the liberty to change the chosen names to be more logical to product, aspect and quantity...

    That is all... Now when you press the submit button the form should get posted to the target you specified: slips.php which is probably a script of yours... Inside that script you now can access the data like that:

    $product  = $_POST['product'];
    $quantity = $_POST['quantity'];
    

    There are more issue worth discussing and modifying, but as said: let's keep things simple and take one step after the other!


    ChangeLog:

    • changed the literal key of the array element used as a value inside the radio button definition from id to procduct_id according to one of the comments below
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能