从select中获取选定值并发布到MySQL Table

时间:2018-01-24 06:57:27

标签: javascript php html

我在PHP HTML上相当新,并在此网站上找到了一些帮助,但无法让代码作为我脚本的一部分工作

单击SUBMIT按钮时,记录将发布到数据库表(Transactions),但FIELD(代码)为空,它应包含3个字母的大写字符串,例如ABC

ALERT显示该值存储在变量$ code中,但是将空字符串发布到表中

这位新手非常感谢您的帮助

<?php
// Include config file
require_once 'config.php';

// Define variables and initialize with empty values
$code =  "";
$code_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{
//  Validate Code
$code = strtoupper($code);



// Check input errors before inserting in database
if(empty($code_err)){
    // Prepare an insert statement
    $sql = "INSERT INTO Transactions (Code) 
        VALUES (?)";

    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_code);

        // Set parameters
        $param_code = $code;

        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Records created successfully. Redirect to landing page
            $url = 'http://localhost:8888/portfolio/index.php';
            echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
            exit();
        } else{
            echo "Something went wrong. Please try again later.";
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);
}

}

// Close connection
mysqli_close($link);


?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Create Record</title>
    <link rel="stylesheet"      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <link href="style1.css" rel="stylesheet" type="text/css"/>
    <style type="text/css">
        .wrapper{
            width: 450px;
            margin: 0 auto;
        }
    </style>
    <script>
        function getValue(obj){
            alert(obj.value);
            $code=(obj.value);
            **alert($code);**
        }
    </script>

    <!--Display heading at top center of screen-->
    <div>
        <center><h3>Peter's Portfolio - Shares</h3></center>    
    </div>  <!-- end of Div -->
</head>

<body style="background-color:#fcf8d9">
     <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-6">
                    <div class="page-header">
                        <h2>Create Transaction Record</h2>
                    </div>

                    <form class="form-horizontal" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                        <!-- ASX CODE   -->
                        <div class="form-group">
                            <label for="name" class="control-label col-xs-6">ASX Code:</label>
                            <div class="col-xs-6">
                                <?php
                                    $conn = new mysqli('localhost', 'root', 'root', 'Portfolio') or die ('Cannot connect to db');
                                    $result = $conn->query("SELECT Code, Coy_Nm from Companies ORDER BY Code");
                                    echo "<html>";
                                    echo "<body>";
                                    echo "<select Name='Code' ID='Code'onchange='getValue(this)'>";
                                        while ($row = $result->fetch_assoc()) {
                                            unset($Code, $Coy_Nm);
                                            $Code = $row['Code'];
                                            $Coy_Nm = $row['Coy_Nm']; 
                                            echo '<option value="'.$Code.'">'.$Coy_Nm.'</option>';
                                        }
                                    echo "</select>";
                                    echo "<input type='hidden' value='submit'>";
                                    //$code=$_POST['Code'];
                                    //echo $code;
                                ?>



                            </div>
                        </div>

                        <!--Submit and Cancel Buttons-->
                        <input type="submit" class="btn btn-primary" value="Submit">
                        <a href="index.php" class="btn btn-default">Cancel</a>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

Javascript PHP 无法直接互相沟通。

试试这个

{{1}}

答案 1 :(得分:0)

require_once 'config.php'之后,

关注以下一行:

$code =  "";

当您提交表单并且表单操作为self时,这意味着您的发布数据将被发送到同一个文件,当从您的文件中读取代码时,每次都会使$code为空。当然,一个空值将被发送到DB。如果条件移动此行或更改此逻辑。