html - Submit form without reloading or leaving current page (php- mysql) -
thank in advance. every time user call summarize in form, user validation not necessary since there few of them idea every time form submitted username goes input field, user doesn't have type names on , over. problem way have php code insert values db reloading same form-url if remove line, once submit takes me blank page. have php code takes username in session , write in input field fine. since reloading page session gets killed.
the question how set username in input field after submit form.
here code:
<?php if(isset($_post['add'])) { $dbhost = 'localhost'; $dbuser = 'xxxxx'; $dbpass = 'xxxxx'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('could not connect: ' . mysql_error()); } $sql = "insert callwrapper ". "(data,user,date) ". "values('$_post[dataentered]','$_post[user_name]',curdate())"; mysql_select_db('ugsports'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('could not enter data: ' . mysql_error()); } header('location: http://careerdev.im.priv/test/thistest2.php'); /* here i'm reloading page if remove line of code, after submit take blank page*/ mysql_close($conn); } else { ?> /* stores username in session return in input */ <?php session_start(); $name = $_post['username']; $_session['user'] = $name; echo $_session['user']; ?> </head> <body style="background-color:#d8d8d8;"> <form method="post" action="<?php $_php_self ?>"> <table style="width:320px;" cellpadding="5" cellspacing="5" align="center"> <tr> <td colspan="4" style="background-color:#ff8000;"> <h1 align="center" style="margin:10px;padding:10px;">call wrapper</h1> </td> </tr> <tr> <td colspan="4" style="background-color:#eeeeee;"> <h5 align="center" style="margin:10px;padding:10px;"><?php echo date("d-m-y"); ?></h5> <!--here--> <input type = 'text' name ='user_name' value="<?php echo $name ?>"> </td> </tr> <!-- start menu sidebar --> <tr> <td style="background-color:#6e6e6e;width:150px;vertical-align:top;"> <p align="center"><b >menu</b></p> <br><br> <button onclick="showtag('agent_call');" type="button" style="width: 100px; height: 40px;">agent call</button><br><br> <button onclick="showtag('player_call');" type="button" style="width: 100px; height: 40px;">player call</button><br><br> <button onclick="showtag('runner_call');" type="button" style="width: 100px; height: 40px;">runner call</button><br><br> </td>
<td align="center" style="background-color:#eeeeee;height:400px;width:300px;vertical-align:top;"> <select size="23" id="agent_call" class="dropdown" style="display: none" name="dataentered"> <optgroup label="player adjustment"> <option value="agent called make turn on/off">turn on/off</option> <option value="agent called make credit change">credit change</option> <option value="agent called make temp cred change ">temp cred change</option> <option value="agent called open new customer">open new customer</option> <option value="agent called other reason">other</option> </optgroup> </select> <select size="22" id="player_call" class="dropdown" style="display: none" name="dataentered"> <optgroup label="account adjustment"> <option value="acc adj-change pw">change pw</option> <option value="acc adj-more credit">more credit</option> <option value="acc adj-turn off account">turn off account</option> </optgroup> </select> <select size="5" id="runner_call" class="dropdown" style="display: none" name="dataentered"> <option value="runner-check item it">check item it</option> <option value="runner-check item it">receive work</option> <option value="runner-check item it">confirm/ask information</option> <option value="runner-check item it">problem</option> <option value="runner-check item it">other</option>
</td> </tr>
<tr> <td colspan="4" style="background-color:#ff8000;text-align:center;"> <img onclick="openwin();" src="images/minimize.jpg" width="50" height="30" alt="20"> <input name="add" type="submit" id="add" style="font-size:10pt;color:white;background-color:#ff8000;border:1px solid #fcfff4;padding:10px" value="submit" > </td> </tr> </table> </form> </body> <?php } ?>
php works post / get submit methods. means page reload... but, can use ajax submit withoud reloading.
$('form').on('submit', function (e) { e.preventdefault(); //prevent reload page $.ajax({ type: 'post', //hide url url: 'formvalidation.php', //your form validation url data: $('form').serialize(), success: function () { alert('the form submitted successfully'); //display alert whether form submitted okay } }); });
Comments
Post a Comment