ewhefner
ewhefner

Reputation: 25

how to pass input data to another php through ajax?

Can somebody tell me what is wrong with this?

I have an index page where the user will input 2 dates and send it through ajax to another php page namely month.php. month.php will then use those user inputted dates in the mysql query and select data from the database. My method doesn't seem to work.

index.php

<SCRIPT>
        function loadMonth()
        {
            var xmlhttp;
            if (window.XMLHttpRequest)
                  {// code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp=new XMLHttpRequest();
                  }
            else
                  {// code for IE6, IE5
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                  }
            xmlhttp.onreadystatechange=function()
              {
                  if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                    document.getElementById("myDivs").innerHTML=xmlhttp.responseText;
                    }
              }
            xmlhttp.open("GET","month.php",true);
            xmlhttp.send(); 
        }
</SCRIPT>

<div class="form" align="center">
        Select Dates<br>
        <input type="date" name="date1" ><br><br>
        To<br>
        <input type="date" name="date2" ><br><br>
        <input type="submit" onclick="loadMonth()" value="Search">
        <div id="myDivs"></div>
</div>

month.php

<?php
        $getdate1 = $_POST['date1'];
        $getdate2 = $_POST['date2'];
        $conn = mysqli_connect("localhost", "root", "", "table1");

            // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
                }

            $today = date("Y-m-d");
            $sql = "SELECT items, COUNT(*) as Number
            FROM table1
            WHERE (date_table BETWEEN '".$getdate1."' AND '".$getdate2."')
            GROUP BY items";
            $result = mysqli_query($conn, $sql);
?>

My code doesn't seem to be working

Upvotes: 1

Views: 161

Answers (2)

Sourabh Kumar Sharma
Sourabh Kumar Sharma

Reputation: 2807

use the below code in your open method

xmlhttp.open("GET","month.php?date1=yourdate&date2=yourdate",true);

date1 and date2 are being recieved using GET method so in server side you should have:

    $getdate1 = $_GET['date1'];
    $getdate2 = $_GET['date2'];

Edited:

I am also showing that how will you pick up the date values from the input type using

In your html give id attribute to the input fields, so that it becomes easy to access in javascript

 <input type="date" name="date1" id = "iddate1" ><br><br>
 <input type="date" name="date2" id = "iddate2" ><br><br>

your script part:

    var fdate1 = document.getElementById('iddate1').value;
    var fdate2 = document.getElementById('iddate2').value;

    xmlhttp.open("GET","month.php?date1="+fdate1+"&date2="+fdate2,true);

Upvotes: 1

ecarrizo
ecarrizo

Reputation: 2809

You are not sending any parameters to the month.php, you can send it using GET Method like this:

xmlhttp.open("GET","month.php?date1=someDate&date2=anotherDate",true); 

And in the PHP Code :

$getdate1 = $_GET['date1'];
$getdate2 = $_GET['date2'];

Or using POST Method:

xmlhttp.open("POST","month.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("date1=someDate&date2=anotherDate");

And keeping the php code like it is.

IMPORTANT: Make sure that you escape the values before using it in the SQL Query or use prepared statements otherwhise you are vulnerable to SQL Injection.

You should search for 'Prevent SQL Injection PHP'

Upvotes: 3

Related Questions