Click here to Skip to main content
16,017,502 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a java script code that is suppose to fill in the input box with the meal_price when i select the meal_name in the option box.what could be the problem with my code.
Below is my code.

What I have tried:

<?php
	
	//$uid = $_SESSION['uid'];
	if(isset($_SESSION['uid'])){
    echo $_SESSION['uid'];
}	
	include("../dbConfig.php");
	include "user_navigation.php";
    include "../header.php";
    include "../footer.php";

	$query = "Select Max(meal_id) From meal_table";
	$returnD = mysql_query($query);
	$result = mysql_fetch_assoc($returnD);
	$maxRows = $result['Max(meal_id)'];
	if(empty($maxRows)){
        $lastRow = $maxRows = 101;      
    }else{
		$lastRow = $maxRows + 1 ;
    }

?>



                    <?php
                    //REQUEST FOR Meals...

                        $query = "Select Max(meal_id) From meal_table";
                        $returnD = mysql_query($query);
                        $result = mysql_fetch_assoc($returnD);
                        $maxRows = $result['Max(meal_id)'];
                        if(empty($maxRows)){
                            $lastRow = $maxRows = 101;      
                        }else{
                            $lastRow = $maxRows + 1 ;
                        }

                        if (isset($_REQUEST['mealRequestBtn'])) {
                            
                            $requestid = $_REQUEST['requestid'];
                            $meal_id = $_REQUEST['meal_id'];
                            $meal_name = $_REQUEST['meal_name'];
                            $meal_price = $_REQUEST['meal_price'];
                            $quantity = $_REQUEST['quantity'];
                            $request_name = $_REQUEST['request_name'];
                            $description = $_REQUEST['description'];

                            if(!empty($requestid) && !empty($meal_name) && !empty($request_name)){

                                date_default_timezone_set('Africa/Nairobi');
                                $dt = date("y/m/d h:i:s");

                                $query = mysql_query("INSERT INTO meal_table(requestid,meal_id,meal_name,meal_price,quantity,request_name,description,requestDate) VALUES('$requestid','$meal_id','$meal_name','$meal_price','$quantity','$request_name','$description','$dt')");

                                if ($query) {
                                    $errorMsg = "You successfully requested for a meal.";
                                }
                            }
                            else{
                                $errorMsg = "Please! Enter in the empty field.";
                            }
                             header("location: user_main.php");

                            //include("requestFormeals.php");
                        }

                    ?>

<!-- <script type="text/javascript">
        function getPrice(select) {
          var form = select.form;
          form.meal_price.value = select.options[select.selectedIndex].text;
          form.meal_name.value = select.value;
        }
</script> -->
<script type="text/javascript">
    function Choice()
    {
    x = document.getElementById("meal_price");
    y = document.getElementById("meal_name");

    x.value = y.options[y.selectedIndex].text;
    }
</script>

        <div id="page-wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">Request For Meals</h1>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div class="row">
                <div class="col-md-4 col-md-offset-4">
                    <div class="Register-panel panel panel-default">
                    <!--div class="panel panel-default"-->
                        <div class="panel-heading">
                            Add Items 
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-lg-6">
                                	<div class="panel-body">
										<form role="form" action="">
                                            <div class="row form-inline">
                                                <fieldset>                                   
        											<div class="form-group">
        												<input type="text" name="requestid" class="form-control" value=<?php echo $_SESSION['uid']; ?> readonly>
        											</div>
        											<div class="form-group">
        												<input type="text" name="meal_id" class="form-control" value=<?php echo $lastRow; ?> readonly>
        											</div>
                                                    <div class="inputs">
                                                        <div class="form-group">
                                                            <select name="meal_name" required autofocus onchange= "meal_name(meal_name)" onChange='Choice();'>
                                                                <option value="">Select Food Menu</option>
                                                                <?php
                                                
                                                                    $query="select * from meal_menu_table";
                                                
                                                                    $res=mysql_query($query) or die('wrong query');
                                                
                                                                    while($row=mysql_fetch_assoc($res))
                                                                    {
                                                                        //echo "<option>".$row['activity_name'];
                                                                        echo "<option value='" . $row['meal_name'] . "'>" . $row['meal_name'] . "</option>";
                                                                    }
                                                                ?>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="form-group">
                                                        <input type="text" name="meal_price" id="meal_price" class="form-control" required autofocus placeholder="Meal_Price" readonly >
                                                    </div>
        											<div class="form-group">
        												<input type="text" name="quantity" class="form-control" required autofocus placeholder="Number_of_Dishes" >
        											</div>
        											<div class="form-group">
        												<input type="text" name="request_name" class="form-control" required autofocus placeholder="Your-Name">
        											</div>
        											<div class="form-group">
        												<textarea cols="35" rows="3" name="description" class="form-control" placeholder="Description and your location"></textarea>
        											</div>
        												<input type="submit" name="mealRequestBtn" value="Request" class="btn btn-success btn-sm">
        												<br>
                                                            <?php
                                                                if(isset($errorMsg)){
                                                                    ?>
                                                                    <div class="errorMsg"><?php echo $errorMsg; ?></div>
                                                                    <?php   
                                                                }
                                                            ?>
										</form>

	                                </div>
                                </div>
                                <!-- /.col-lg-6 (nested) -->
                            </div>
                            <!-- /.row (nested) -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
        </div>
        <!-- /#page-wrapper -->
Posted
Updated 23-Jul-18 11:30am

1 solution

PHP
$query = mysql_query("INSERT INTO meal_table(requestid,meal_id,meal_name,meal_price,quantity,request_name,description,requestDate) VALUES('$requestid','$meal_id','$meal_name','$meal_price','$quantity','$request_name','$description','$dt')");

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]

Secondary problem, it is impossible to know what is exactly your query because it depend on the values of the parameters.
On the debugger can show what is the real query.
 
Share this answer
 
Comments
harristars 25-Jul-18 11:09am    
My problem is not sql injection. In fact I have no problem with my db it is doing exactly what is required to do and I have a file that is taking care of security related issue. I think my question is very clear. It's not db related.it about a JavaScript code that is ment to populate data into input box once you select the data in option box.
Patrice T 25-Jul-18 11:20am    
it is just what I said "Not a solution to your question, but another problem you have."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900