php - How can call a page on submit using ajax? -
i want create chat room in php using ajax. therefore want call page clicking on submit. have 2 pages chat.php , getchat.php. wrong , how can call getchat.php . have try function not working. please me.
chat.php
<script> function showuser() { xmlhttp.open("get","getchat.php",true); xmlhttp.send(); } </script> <form onsubmit="showuser(this.value)" method="post"> <label>name :</label> <input type="text" name="username" /><br /> <textarea name="message"></textarea> <input type="submit" name="submit" /> </form>
getchat.php
<?php $con = mysqli_connect('localhost','root','','ajax'); if (!$con) { die('could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"ajax_demo"); $sql="select * chat"; $result = mysqli_query($con,$sql); echo "<table border='1'> <tr> <th>name</th> <th>message</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['message'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?>
i did this
chat.php
<form id="chat_form" method="post"> <label>name :</label> <input type="text" name="username" /><br /> <textarea name="message"></textarea> <input type="submit" name="submit" /> </form> <div id="result"></div> <script src="jquery-2.1.0.js"></script> <script> $(document).ready(function() { $('#chat_form').submit(function(e){ e.preventdefault(); var data = $("#chat_form").serializearray(); $.ajax({ url: 'getchat.php', type: "post", data: data, success: function(info){ $("#result").html(info); }, error:function(){ $("#result").html('there error while submit'); } }); }); }); </script>
getchat.php
not changed yours
Comments
Post a Comment