Peter Renwick
Peter Renwick

Reputation: 61

get selected value from select and post to MySQL Table

I am fairly new at PHP HTML and found some help on this site but cannot get the code to work as part of my script

On clicking the SUBMIT button a record is posted to the database table (Transactions) but the FIELD (Code) is empty, it should contain a 3 letter uppercase string e.g. ABC

The ALERT shows that the value is stored in the variable $code but an empty string is posted to the table

Your help is greatly appreciated by this novice

<?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>

Upvotes: 1

Views: 68

Answers (2)

jamal mehmood
jamal mehmood

Reputation: 1

After require_once 'config.php',

Focus on the following line :

$code =  "";

When you submit form and form action is self, this means that your post data will be sent to the same file and when the code is read from your file it makes $code empty every time. of course, an empty value will be sent to DB. move this line inside if condition or change this logic.

Upvotes: 0

niXful-Autistic
niXful-Autistic

Reputation: 217

Javascript and PHP cannot simply communicate to each other live.

try this

function getValue(obj){
        alert(obj);// check if obj has something in it before proceeding to "value"
        alert(obj.value);
}

Upvotes: 1

Related Questions