简体   繁体   中英

How to post the information using javascript for a preview table

Hey I need to know how to post the information to another php page using javascript to provide a preview table.

Here's my HTML that has a onchange tag that sends the productID to the function.

<form action="Home.php" method="Post">
    <div>
        <p>
            <span class="text">Please Select a product:</span>
            <select id="Select_Product" name="Select_Product" onchange="productInfo(this.value)" class="select">
            <?php
                //setting the select statement and running it
                $search = "SELECT * FROM Library.Products order by Name";
                $return = mysql_query($search);
                //echo "<select id=Select_Product name=Select_Product onchange=productInfo(this.value) class=select>";
                  while ($row = mysql_fetch_array($return)) {
                    echo "<option value='" . $row['ProductID'] . "' selected='selected'>".$row['Name'] . "</option>";
                }
            ?>
            </select>
        </p>
        <table>    
            <tr>
                <td>
                    <input name="action" type="submit"class="button" id="button_Add" value="Add"/>
                </td>
                <td>
                    <input name="action" type="submit" class="button" id="button_Remove" value="Remove"/>
                </td>
                <td>
                    <input name="action" type="submit" class="button" id="button_empty" value="Empty"/>
                </td>
            </tr>
        </table>
    </div>

From there I want it to send it to catalogue.php.

<script>
    function productInfo(key) {
        //Send key to catalogue.php
    }
</script>

If I can get the other page to get that variable I can run a MYSQL command to get the information. Here is what catalogue.php looks like at the moment.

<?php
$sql = "SELECT Name, Price FROM Library.Products WHERE ProductID = " . $product_id;
echo "<table border=\"1\" padding=\"3\" width=\"650px\"><tr><th>Name</th><th>Description</th><th>Price</th><th width=\"80px\">Image</th></tr>";
    echo "<tr>";
        echo "<td>" .$product_id . "</td>";
        echo "<td> Hi</td>";
        echo "<td></td>";
        echo "<td align=\"center\"><img alt=\"\" src=\"productImages/".$product_id.".jpg\ width=\"120\" height=\"120\"/></td>";
    echo "</tr>";
echo "</table><br>";
document.
?>

So in a sense I want to turn the key in productInfo(key) to be assigned to the variable $product_id in catalogue.php. Thanks for helping.

You can add an invisible form :

<script>
    function productInfo(key) {
        //Send key to catalogue.php
        document.getElementById( "key" ).value = key;
        document.getElementById( "frm" ).submit(); // SUBMIT FORM.
    }
</script>
<form action="catalogue.php" method="post" id="frm"
      style="display:none" target="_blank">
  <input type="text" id="key" name="key"/>
</form>

catalogue.php needs one little change :

<?php
$product_id = $_POST[ "key" ]; //<================== VALUE FROM THE INVISIBLE FORM.
$sql = "SELECT Name, Price FROM Library.Products WHERE ProductID = " . $product_id;
echo "<table border=\"1\" padding=\"3\" width=\"650px\"><tr><th>Name</th><th>Description</th><th>Price</th><th width=\"80px\">Image</th></tr>";
    echo "<tr>";
        echo "<td>" .$product_id . "</td>";
        echo "<td> Hi</td>";
        echo "<td></td>";
        echo "<td align=\"center\"><img alt=\"\" src=\"productImages/".$product_id.".jpg\ width=\"120\" height=\"120\"/></td>";
    echo "</tr>";
echo "</table><br>";
document.
?>

Or you can use Ajax :

html file

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type = "text/javascript">
function myAjax ( key ) {
$.ajax( { type : 'POST',
          data : { 'param' : key },
          url  : 'catalogue.php',
          success: function ( data ) {
            document.getElementById( "my_div" ).innerHTML = data;
          },
          error: function ( data ) {
            alert( "error" );
          }
        });
}
function productInfo( key ) {
//Send key to catalogue.php
myAjax( key );
}
    </script>
  </head>
  <body>
    <button onclick="productInfo('123')">Click here to get the data</button>
    <div id="my_div">
      - data will show here -
    </div>
  </body>
</html>

catalogue.php

<?php
$key = $_POST[ "param" ];
echo "<table border='1'>" .
     "  <tr>" .
     "    <td>$key</td>" .
     "    <td>ABC</td>" .
     "  </tr>" .
     "</table>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM