如何获取使用数组显示的Submit元素的值

时间:2016-09-04 17:57:49

标签: javascript php html

继承了Theme.php的整个代码,我只是想知道如何获取包含产品名称的输入类型图像的值,但是它是使用循环显示的

let params = "<ItemTrackingDetailsRequest>
        <ItemTrackingNumbers>
        <TrackingNumber>\(trackingNumber.uppercaseString)</TrackingNumber>
        </ItemTrackingNumbers>
        </ItemTrackingDetailsRequest>"

        let xmlRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
        xmlRequest.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
        xmlRequest.HTTPMethod = "POST"
        xmlRequest.addValue("application/xml", forHTTPHeaderField: "Content-Type")

我需要获取sumbit表单的值,特别是输入类型图像的产品名称

<html>
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>EJ's</title>
    <link href="css/design.css" rel="stylesheet">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

    <!-- Bootstrap Core CSS -->

</head>
</body>
<!-- Nav Bar  -->

    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">

            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#"></a>
            </div>

            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                  <li>
                    <img src="image/logo.png" style="width:100px;height:48px;">
                    </li>
                    <li>
                        <a href="index.html">Home</a>
                    </li>

                    <li>
                        <a href="booking.html">Reservation</a>
                    </li>
                    <li>
                        <a href="Catbirthday.php">Catering</a>
                    </li>
                    <li>
                        <a href="Package2.php">Packages</a>
                    </li>
                  <li>
                        <a href="sample.php">Products</a>
                    </li>
                  <li>
                        <a href="Servbirthday.php">Services</a>
                    </li>
                </ul>

            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>
    <?Php
    $connection = new mysqli('localhost', 'root', '', 'mydb');

    if ($connection->connect_errno > 0) {
        die ('Unable to connect to database [' . $connection->connect_error . ']');
    }   
    $sql = "SELECT * FROM product";
    if (!$result = $connection->query($sql)) {
        die ('There was an error running query[' . $connection->error . ']');
    }   
?>
    <?php


    $rows = $result->num_rows;    // Find total rows returned by database
    if($rows > 0) {
        $cols = 3;    // Define number of columns
        $counter = 1;     // Counter used to identify if we need to start or end a row


        $row_class = 'row';    // Row class name
        $col_class = 'col-sm-4'; // Column class name

         echo'<div class="col-sm-2">

            </div>';

        echo'    <div class="col-sm-8">'
        .'<br>'
        .' <ul class="breadcrumb">
         <li><a href="default.html">Event type</a></li>
         <li>Theme</li>
    <li><a href="Tpackbirthday.php">Packages</a></li>
     <li><a href="Tcatbirthday.php">Catering</a></li>      
    <li><a href="Tprodbirthday.php">Products</a></li>
    <li><a href="TServbirthday.php">Services</a></li>
    <li class="active"><a href="Information1.php">Customer information</a></li>
      </ul>'
                    .'<center><h1>Choose your Party Theme</h1>';
        while ($item = $result->fetch_array()) {

            if(($counter % $cols) == 0) {    // Check if it's new row
                echo '<div class="'.$row_class.'">'; // Start a new row
            }




                    echo 
                    '<script type="text/javascript">
                         function SaveFormTheme()
                        {


                              var data ="'.$item['product_status'].'"; 
                              localStorage.setItem("theme", data)
                                alert("tangina");
                                return false;

                        }
                    </script>'
                    .'<form method="post" name="form" onsubmit="return SaveFormTheme()" action="Tpackbirthday.php">'
                    .'<div class="'.$col_class.'">'
                    .'<div class="ew">'

                    .' <input type="image" src ="image/'.$item['image_name'].'" style="width:100%" width="304" height="236">'


                    .'<p style="color:white;">Product name: '.$item['product_name'].'</p>'

                    .'</div>
                    </div>'
                    .'</form>';     // Column with content


            if(($counter % $cols) == 0) {

                // If it's last column in each row then counter remainder will be zero
                echo '</div>';
                //  Close the row

            }

            $counter++;    // Increase the counter
        }


    }
    ?>


    enter code here



    </body>
    </html>

但我总是得到产品名称的最后一个值

1 个答案:

答案 0 :(得分:0)

是的,这是因为您将SaveFormTheme函数定义为每个项目的全局函数,因此在执行SaveFormTheme()时始终只调用最后一个函数。要解决此问题,请在函数名称中包含一些itemID或$counter,这样您就可以SaveFormTheme1()SaveFormTheme2()等。