0

First sorry for my bad english. below is my form image i have a form in my My Website first how to get the values from menu to text box then store the values in to database using Php .i have attached my code and image.I am beginner in Php . Please help me

HTML CODE

 <div class="taxi-form-full">
        <div class="container"> 
            <form  method="POST"  action="">
                <div class="menu-types">
                    <a href="#" data-value="Standart" class="type-1 active">Standart</a>
                    <a href="#" data-value="Business" class="type-2">Business</a>
                    <a href="#" data-value="VIP" class="type-3 red">Vip</a>
                    <a href="#" data-value="Bus" class="type-4">Bus-Minivan</a>

                </div>
                <div class="row forms">
                    <div class="col-md-3">
                        <div class="form-group">
                            <input type="text" id="searchTextSrc" name="source" value="" placeholder="From Address..." class="ajaxField"><span class="fa fa-map-marker"></span>
                            <input type="hidden" id="citySrc" name="citySrc" />
                            <input type="hidden" id="cityLatSrc" name="cityLatSrc" />
                            <input type="hidden" id="cityLngSrc" name="cityLngSrc" />
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group">
                            <input type="text" id="searchTextDes" name="destination" value="" placeholder="To..." class="ajaxField"><span class="fa fa-map-marker"></span>
                            <input type="hidden" id="cityDes" name="cityDes" />
                            <input type="hidden" id="cityLatDes" name="cityLatDes" />
                            <input type="hidden" id="cityLngDes" name="cityLngDes" />
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <input type="text" id="phone" name="phone" value="" placeholder="Phone Number" class="ajaxField required"><span class="fa fa-phone"></span>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">

                            <input type="text" id="text" name="datetime" value="" placeholder="Date and Time" class="ajaxField js-datetimepicker"><span class="fa fa-calendar"></span>
                        </div>
                    </div>
                </div>
                <input type="submit" name="submit" value="submit" class="btn btn-lg btn-red aligncenter">

            </form>
        </div>
    </div>

Php Code

    <?php
 $hname = 'localhost';
    $uname = 'root';
    $pword ='';
    $dbase ='sample';
$con = mysqli_connect($hname, $uname , $pword ,$dbase );
  if ($con->connect_error) {
            die("Connection failed: " . $con->connect_error);
        }
if(isset($_POST['submit'])){
$phone = $_POST['phone'];
$datetime = $_POST['datetime'];
$query = mysqli_query($con,"insert into test(phone, datetime) values ('$phone', '$datetime')");

}
Macroid
  • 1
  • 3
  • You should check out this as well https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – R. Chappell Sep 19 '18 at 10:21
  • Could you elaborate on *"how to get the values from menu to text box"* ? Do you simply mean retrieve the value of a specific menu item *AND* the value of a textbox? Or do you wish to put the menu item value *INTO* the textbox? – Martin Sep 19 '18 at 10:32

2 Answers2

0

Instead of using anchors as part of your navigation you can use radio fields. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio

This will then send you the appropriate menu item chosen via the $_POST variable.

<label class="type-1">
    <input type="radio" name="menu" value="Standart" checked /> Standart
</label>
<label class="type-2">
    <input type="radio" name="menu" value="Business" /> Business
</label>
<label class="type-3 red">
    <input type="radio" name="menu" value="VIP" /> Vip
</label>
<label class="type-4">
    <input type="radio" name="menu" value="Bus" /> Bus-Minivan
</label>

In PHP you'll receive the value based on what option was checked.

<?php
$menu = $_POST['menu'];

You'll need to change the styling of the radio fields to match what you had for your anchors. As the input is within the label you can style the label and hide the input field to keep the same look and feel. Styling radio button

This method is far more attractive than capturing the clicks with JavaScript and injecting the value into a text field as it'll work with or without JavaScript.

R. Chappell
  • 1,184
  • 6
  • 17
0

One of the ways you could achieve this is by using AJAX. In my example, I'll use jQuery AJAX, which will require you to include a jQuery library to your application. The reason I personally use jQuery is because it simplifies the JavaScript code a lot, and so it makes it easier to write and understand.

Now, in your question, you mention the element textbox, but I do not see that element anywhere? Do you mean your input fields, which is an entirely different thing? Since I don't see any textbox elements, I am going to assume that you mean input fields.

Before jumping straight into the AJAX function, I'd like to explain a few things. Basically what we will do with the AJAX function is to parse values to another page, which will handle our database logic, such as INSERT the data etc.

The key elements in an AJAX function is its options that you define in the function.

  1. type, which is the data type of your data that you are going to parse. Examples would be GET and POST
  2. url, which is where you define the page where the data that will get parsed.
  3. data, which is where you will define your data variables that you will be parsing.
  4. optionally, success, which is a function where you can define certain actions to occur upon success of your AJAX function.

The AJAX function:

function submitForm() {
    $.ajax({
        type : "POST",
        url : "/your_page.php",
        data : {
            standart: $("[data-value='Standart']").val(),
            business: $("[data-value='Business']").val(),
            vip: $("[data-value='VIP']").val(),
            bus: $("[data-value='Bus']").val(),
            searchTextSrc: $("#searchTextSrc").val(),
            citySrc: $("#citySrc").val(),
            cityLatSrc: $("#cityLatSrc").val(),
            searchTextDes: $("#searchTextDes").val(),
            cityDes: $("#cityDes").val(),
            cityLatDes: $("#cityLatDes").val(),
            cityLngDes: $("#cityLngDes").val(),
            phone: $("[name='phone']").val(),
            datetime: $("[name='datetime']").val()
        },
        success: function (html) {
            //do something on success
        }
    })
}

Note that I encapsulated the AJAX function into another function that I named submitForm(). You can choose to call this function onclick on your button in order to trigger your AJAX function.

<button onclick="submitForm();">Submit content</button>

Since you weren't entirely specific on precisely which elements values you'd like to retrieve, I targeted them all. I am actually uncertain if $("[data-value='whatever']") is a valid selector. If it's not, simply give them an id or a name attribute to go by instead.

Now, since I defined the type in the AJAX function to be POST, you will have to use $_POST in your PHP file to fetch the data that has been parsed from the AJAX function.

In your PHP page:

<?php
$standart = $_POST['standart'];
$business = $_POST['business'];
$vip = $_POST['vip'];
$bus = $_POST['bus'];
$searchTextSrc = $_POST['searchTextSrc'];
$citySrc = $_POST['citySrc'];
$cityLatSrc = $_POST['cityLatSrc'];
$searchTextDes = $_POST['searchTextDes'];
$cityDes = $_POST['cityDes'];
$cityLatDes = $_POST['cityLatDes '];
$cityLngDes = $_POST['cityLngDes '];
$phone = $_POST['phone '];
$datetime = $_POST['datetime '];

/* You now have all your parsed variables as PHP variables
   that you can choose to INSERT into your database. */

$hname = 'localhost';
$uname = 'root';
$pword ='';
$dbase ='sample';
$con = mysqli_connect($hname, $uname , $pword ,$dbase );
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}

$sql="INSERT INTO test SET phone='$phone', datetime='$datetime'";
mysqli_query($con, $sql);
?>

However, please note that you are wide open to SQL Injections, which is one of the reasons @R. Chappell dropped this link in the comment section.

Martin
  • 2,326
  • 1
  • 12
  • 22