通过ajax从数据库中检索数据

时间:2015-10-13 07:09:04

标签: php ajax

我创建了一个页面form.php。它工作正常。当我点击更新按钮时,它以形式检索数据 <a href="form.php?uid='.$row["u_id"].'" style="color:black;" id="edit" >编辑 它工作正常。在适当的领域给我所有的价值。

但是现在我希望在ajax或jquery的帮助下进行相同的操作。

任何想法?

的index.php

<?php

include'config.php';

?>
<html>

<head>
<title> User List </title>
  <link rel="stylesheet" href="css1/datacss.css" />
<script type="text/javascript" src="js1/jquery-latest.js"></script>
<script type="text/javascript" src="js1/jquery.tablesorter.js"></script>
<script type="text/javascript" src="js1/jquery.tablesorter.widgets.js"></script>

<script>
$(function() {
$( "#datepicker") .datepicker({changeMonth: true,
                        changeYear: true,
                        yearRange:"-100:+0"});

});
</script>

<script language="javascript" type="text/javascript">

<!--
//Browser Support Code
function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('add');
            var ajaxDisplay = document.getElementById('img');
            var ajaxDisplay = document.getElementById('myform');
            var ajaxDisplay = document.getElementById('edit');


            ajaxDisplay.innerHTML = ajaxRequest.responseText;
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
            ajaxDisplay.innerHTML = ajaxRequest.responseText;


        }
    }
     $("html").load("../docs/Form.php");

}

//-->
</script>

  <script>
 $(document).ready(function() {
   $("#mytable").tablesorter();  // <-- 'myTable' is the id of your table
});
  </script>
</head>

<body>
<form action="index.php" method="get">
<div id="search"><input type="text" name="query" />
<input type="submit" value="Search" id="se1" />
<input type="hidden" value="1" name="doSearch" id="display"/>

</div>



<div id="main">
<div id="img">
<a href= "#"><img src="images/Add-Male-User-icon.png" class="img" alt="Add User"  onclick='ajaxFunction()'/></a>
</div>



<div id="btn">
<a href='index.php'  ><input type='button' value='Cancel 'id="cancel" /></a>
</div>
 </div>
<div>

          <table id="mytable" class="tablesorter">
                            <thead >

                            <tr>
                                <th id="name" >Uid</th>
                                <th id="email">User Name</th>

                                <th id="gender">Email Address</th>
                                <th id="hobbies">Gender</th>
                                <th id="dob">DOB</th>



                                <th id="edit">EDIT </th>

                                <th>DELETE</th>


                            </tr>
                           </thead>
                              <tbody>

                            <?php

                            if(isset($_GET['doSearch'])){

                                if($_GET['doSearch']==1){

                                    include'search.php';

                                }

                            }
                            else  {

                               extract($_POST);


                                $query = mysqli_query($con,"SELECT * FROM u_reg");
                                 //Counting total number of rows in the table 'data',
                                $total_rows = mysqli_num_rows($query);

                                 $base_url = 'https://localhost/Assignment/Docs/';    //Provide location of you index file
                                 $per_page = 5;                           //number of results to shown per page
                                 $num_links = 8;                           // how many links you want to show

                                 $cur_page = 1;                           // set default current page to 1


                                if(isset($_GET['page']))
                                    {
                                        $cur_page = $_GET['page'];
                                        $cur_page = ($cur_page < 1)? 1 : $cur_page;            //if page no. in url is less then 1 or -ve
                                    }
                                    $offset = ($cur_page-1)*$per_page;                //setting offset

                                    $pages = ceil($total_rows/$per_page);
                                    $start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
                                    $end   = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;





                                    $result = mysqli_query($con,"SELECT * FROM u_reg LIMIT ".$per_page." OFFSET ".$offset) ;
                                    $n=mysqli_num_fields($result);

                                    if ($result->num_rows >= 1){
                                         // output data of each row



                                         while($row=mysqli_fetch_assoc($result)) {

                                             echo "<tr>
                                             <td>" . $row["u_id"] . "</td>
                                             <td> ". $row["u_name"]. " </td>
                                             <td>". $row["u_mail"]. "</td>

                                             <td>" . $row["u_gender"] . "</td>

                                             <td>" . $row["u_dob"] . "</td>



                                                  ";

                                                echo'
                                                <td> <a href="#?uid='.$row["u_id"].'" style="color:black;" id="edit" onclick=ajaxFunction()> Edit</a> </td>
                                                <td> <a href="con_delete_user.php?uid='.$row["u_id"].'"  style="color:black;"  >Delete</a> </td>

                                                 </tr>';




                                }
                           }
                        }
                                ?>
                               </tbody>

                        </table>






</div>
 <div id="pagination">
        <div id="pagiCount">
            <?php
                if(isset($pages))
                {
                    if($pages > 1)
                    {    if($cur_page > $num_links)     // for taking to page 1 //
                        {   $dir = "First";
                            echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.(1).'">'.$dir.'</a> </span>';
                        }
                       if($cur_page > 1)
                        {
                            $dir = "Prev";
                            echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page-1).'">'.$dir.'</a> </span>';
                        }

                        for($x=$start ; $x<=$end ;$x++)
                        {

                            echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':'<a href="'.$_SERVER['PHP_SELF'].'?page='.$x.'">'.$x.'</a> ';
                        }
                        if($cur_page < $pages )
                        {   $dir = "Next";
                            echo '<span id="next"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page+1).'">'.$dir.'</a> </span>';
                        }
                        if($cur_page < ($pages-$num_links) )
                        {   $dir = "Last";

                            echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pages.'">'.$dir.'</a> ';
                        }
                    }
                }
            ?>
        </div>
    </div>
         </form>
</body>
</html>

form.php的

<?php

        include 'config.php';
        error_reporting(0);
        //include 'edit.php';
        if(isset($_GET['uid']))
        {
         $userid = $_GET['uid'];
         $result = mysqli_query($con,"select * from u_reg where u_id={$userid}");
         $row = mysqli_fetch_array($result);


         }
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Registration Form</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<link rel="stylesheet" href="css/main.css">

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.js"></script>
<script type="text/javascript" src="js/validation_form.js"></script>


<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="js/statecountry.js"></script>

<script>

$(function() {
$( "#datepicker") .datepicker({changeMonth: true,
                        changeYear: true,
                        yearRange:"-100:+0"});

});
</script>


</head>



<body>
<div class="container">

<?php

if(isset($_GET['uid']))
        {
            if ($_GET['uid']!='')

                {
             echo'<form action="con-edit-user.php?userid='.$row["u_id"].'" id="myForm" method="post" enctype="multipart/form-data" >';

        } }
        else {
            echo '<form action="userclass.php" id="myForm1" method="post" enctype="multipart/form-data" >';
        }
 ?>

<!--<form action="userclass.php" method="post" enctype="multipart/form-data" id="example1" > -->
<label id="dis" ></label><br>
<input type="hidden" name="userid" value="<?php echo($userid); ?>">
 <div class="image1" >


            <?php
            if(isset($row['u_image']))
                  {
                  echo"<img src='".$row['u_image']."'> ";

                  }


             ?>
            </div>

<table align="center"  cellspacing="14"  >
        <tr>
        <td colspan="2">
        <h1 align="center">Registration Form</h1>
        </td>
        </tr>


        <tr>
        <td><label>
            User Name:</label></td>
        <td><input type="text"  name="uname" id="uname" value="<?php if(isset($row['u_name'])) echo($row['u_name']); ?>">

        <td id="nameerror"></td>
        </td>
        </tr>

        <tr>
        <td>
            <label>Gender:</label></td><td id ="gen">
            <input type="radio" name="gender" value="M" <?php if(isset($row['u_gender'])) if($row['u_gender']=="M"){echo("checked='checked'");} ?> id="gender" /> <strong>Male</strong>
            <input type="radio" name="gender" value="F" <?php if(isset($row['u_gender'])) if($row['u_gender']=="F"){echo("checked='checked'");}?>id="gender" /> <strong>Female</strong>
            <td id="gendererror"></td>
        </td>
        </tr>

        <tr>
        <td>
            <label>Email:</label></td><td> <input type="text" name="email"  id="email" value="<?php if(isset($row['u_mail'])) echo($row['u_mail']); ?>"/>
        </td>
        <td id="emailerror"></td>
        </tr>

        <tr>
        <td>
            <label>Password:</label><td> <input type="password" name="pwd" id="pwd"value="<?php if(isset($row['u_pwd'])) echo($row['u_pwd']); ?>" />
            <td id="pwderror"></td>
        </td>
        </tr>

         <tr>
        <td>
            <label>Date Of Birth:</label><td> <input type="text" required   name="DOB" id="datepicker"value="<?php if(isset($row['u_dob'])) echo($row['u_dob']); ?>" />
        <td id="doberror"></td>
        </td>
        </tr>
        <?php

     $sql = "SELECT * FROM country_master ORDER BY c_name";
    $query = mysqli_query($con, $sql);
    ?>
        <td><label>Country :</label></td>
        <td><select name="country" class="country" id="in">
                <option value="select">---Select---</option>
                <?php while ($row1 = mysqli_fetch_array($query)) { ?>
                <option value="<?php echo $row1["c_id"]; ?>" <?php if($row1["c_id"] == $row['u_country']){ echo "selected=selected"; } ?> ><?php echo $row1["c_name"]; ?></option>
        <?php } ?>
        <td id="conerror"></td>


<!--<option value="india">India</option>
<option value="usa">USA</option>-->
</select>
</td>
</tr>
<tr>
<?php
$sql = "SELECT * FROM state_master ORDER BY s_name";
$query = mysqli_query($con, $sql);
?>



<td><label>State :</label></td>
<td><select name="state1" class="state"  selected="selected">
<option value="select">---Select---</option>
<?php while ($row1 = mysqli_fetch_array($query)) { ?>
                <option value="<?php echo $row1["s_id"]; ?>" <?php if($row1["s_id"] == $row['u_state']){ echo "selected=selected"; } ?> ><?php echo $row1["s_name"]; ?></option>
        <?php } ?>

</select>
</td>
</tr>

        <tr>

     <td>
<label>Hobbies:</label></td><td id="ck1">
            <?php

            $hobbies = explode(" ",$row['u_hobbies']);
            ?>
            <input type="checkbox" value="CR" <?php if(in_array("CR",$hobbies)) { echo "checked=checked"; } ?> name="hobbies1" id="ck" <?php if(isset($_POST['hobbies'])){

$check1='selected'; echo $check1; } ?><?php if($row['u_hobbies']=="CR"){echo("checked='checked'");} ?>  /> Cricket

            <input type="checkbox" value="FO" <?php if(in_array("FO",$hobbies)) { echo "checked=checked"; } ?>name="hobbies2" id="ck" <?php if(isset($_POST['hobbies'])){

$check2='selected'; echo $check2; } ?><?php if($row['u_hobbies']=="FO"){echo("checked='checked'");} ?> /> Football<br />

            <input type="checkbox" value="RE" <?php if(in_array("RE",$hobbies)) { echo "checked=checked"; } ?>name="hobbies3" id="ck" <?php if(isset($_POST['hobbies'])){

$check3='selected'; echo $check3; } ?><?php if($row['u_hobbies']=="RE"){echo("checked='checked'");} ?> /> Reading

              <input type="checkbox" value="PL" <?php if(in_array("PL",$hobbies)) { echo "checked=checked"; } ?>name="hobbies4" id="ck" <?php if(isset($_POST['hobbies'])){

$check4='selected'; echo $check4; } ?><?php if($row['u_hobbies']=="PL"){echo("checked='checked'");} ?> /> Playing Games


</td>
        <td id="ckerror"></td>
        </tr>

        <tr>
        <td>
          <label>   Photo Upload:</label></td>
        <td><input type="file" name="image" id="img1" />
        <td><div>
        <?php
                if(isset($row[' u_image']))
                {
                    echo"<img src='".$row[' u_image']."'> ";
                }
            ?>
         </div>
        </td>
        </tr>

        <tr>
        <td><label>
            About You:</label></td>
         <td><textarea name="about" id="ek"></textarea></td>
         <td id="ekerror">
         </tr>

         <tr>
         <td></td>
         <td><input type="submit" name="submit" value="Submit" id="submit" />
         <input type="reset" name="reset" value="Reset" id="reset"/>
         <a href= "index.php"id="cancel"> <input type="button" name="cancel" value="Cancel" id="cancel"/></a> </td>
         </tr>

</table>

</form>
</div>
</body>
</html>

两个文件都附有que。

0 个答案:

没有答案