0

Below is a form in my index page where user will select an option and click submit. On submitting it'll be posted in "display.php" file where i'm fetching data from database.

index page code:

<form action="display.php" method="post">
<select name="mnth">
<option value="Jan">January</option>
<option value="Feb">February</option>
<option value="Mar">March</option>
<option value="Apr">April</option>
<option value="May">May</option>
<option value="Jun">June</option>
<option value="Jul">July</option>
<option value="Aug">August</option>
<option value="Sep">September</option>
<option value="Oct">October</option>
<option value="Nov">November</option>
<option value="Dec">December</option>
</select>
<input id="abc" type="submit" name="Display"></input> 
</form>

The display.php file contains the following php code which fetches data from table and displays it in a tabular form in a different white page. But want to display this table in my index page instead. How do i do that?

<?php

$empid=$_COOKIE["empid"];
$deptt=$_COOKIE["deptt"];
$password=$_COOKIE["password"];

$month=$_POST["mnth"];



$empid=mysql_real_escape_string($empid);
date_default_timezone_set('Asia/Kolkata');


$con=mysql_connect("localhost","root","","");
if(!$con)
{
die("Error in connection");
}

$db=mysql_select_db("swan",$con);

if(!$db)
{
 echo mysql_errno($con);
}

$query_login="SELECT * FROM `".$empid."_login` WHERE month='$month' ";
$query_logout="SELECT * FROM `".$empid."_logout` WHERE month='$month' ";
$query_hrs="SELECT * FROM `".$empid."_hrs` WHERE month='$month' ";
$result1=mysql_query($query_login,$con);
$result2=mysql_query($query_logout,$con);
$result3=mysql_query($query_hrs,$con);
$value1=mysql_fetch_array($result1);
$value2=mysql_fetch_array($result2);
$value3=mysql_fetch_array($result3);




$day=date('d');

echo '<table id="displayTable" border="1px"> <th>Date</th><th>Login_Time</th>
      <th>Logout_time</th><th>Hours</th>';


for($i=0;$i<31;$i++)
{
echo '<tr><td>'.($i+1).'</td><td>'.$value1[($i+1)].'</td><td>'.$value2[($i+1)].'</td>
<td>'.$value3[($i+1)].'</td></tr>';

}

echo '</table>';



?>
1
  • you want PHP code in seprate file and html code in index.php?? Commented Jun 18, 2013 at 8:44

2 Answers 2

1

You could use if $_POST so it would look something like this:

<?php
if($_POST)
{
// code from display.php
}
?>
<form action="index.php" method="post">
<select name="mnth">
<option value="Jan">January</option>
<option value="Feb">February</option>
<option value="Mar">March</option>
<option value="Apr">April</option>
<option value="May">May</option>
<option value="Jun">June</option>
<option value="Jul">July</option>
<option value="Aug">August</option>
<option value="Sep">September</option>
<option value="Oct">October</option>
<option value="Nov">November</option>
<option value="Dec">December</option>
</select>
<input id="abc" type="submit" name="Display"></input> 
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

but i want to keep the display code in a separate file instead of putting all the code in my index page
0

On Same page use - AJAX or action="" and code on same page and check if form submit or not.

1st Option -

if (!empty($_POST))
{
// Do form processing, taking care of sql injections
}
else
{
// Show Form
}

2nd Option -

    $.ajax({
        type: "POST",
        url: "yourURL",// your display.php
        data: formData,
        processData: false,
        contentType: false,
        error: function(jqXHR, textStatus, errorMessage) {
           console.log(errorMessage); // Optional
        },
        success: function(data) {console.log(data)} 
    });

//using jQuery for AJAX here.

2nd option usage is sexy, but hey what if JS disabled, so keep these things in your mind as well.

I am using console.log here, so to check it use browser console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.