how to open multiple textboxes on a click using ajax - php

<table>
<tbody id="eb_body">
<tr>
<td>Booking Bills:</td>
<td><input type="text" id="e_brochure0" name="e_brochure0" placeholder="From Bill No........" required/></td>
</tr>
<tr>
<td>
<a onClick="add_brochures()" style="text-decoration:none !important;"><span style="cursor:pointer; font-size:10px; ">+Add more bill nos.</span></a>
</td>
</tr>
<tr>
<td>
<input type="submit" value="save" name="save" />
</td>
</tr>
</tbody>
</table>
there is an anchor tag on that click i am calling a function as below..
<script type="text/javascript">
var b=1;
function add_brochures()
{
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("zone").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","five.php",true);
xmlhttp.send();
b=b+1;
}
</script>
with this i am directing the page to five.php where i am displaying a select box....
code for five.php is this...
<?php
include 'dbcon.php';
$sql="select * from tb_zone";
$query=mysql_query($sql);
?>
<tr>
<td>
<select>
<?php
while($row=mysql_fetch_array($query))
{
?>
<option>
<?php echo $row["zone_name"]; ?>
</option>
<?php } ?>
</select>
</td>
</tr>
when i click on anchor tag it is opening one selectbox but bot more....what i want is that when i click on anchor tag then a selectbox appears...if i click on it 5 times then i should open selectbox 5 times....right now it is opening just 1 selectbox....please help

Change:
document.getElementById("zone").innerHTML=xmlhttp.responseText;
to:
document.getElementById("zone").innerHTML += xmlhttp.responseText;
+= will concatenate the response to the existing HTML, which will add a new row to the table.

Related

Enable and disable button without page load

I have a form with a dropdown box, login button, and logout button. My requirement is to insert date and time when user click on the login or logout button after username selection from dropdown. Also, when user clicks on login button, it has to enable by changing the background color without page load. Same functionality required for logout option also.
Now, I can select the user's name from the dropdown which is coming from database. I stucked on the insertion part which i need without page load.
I need to insert data without page load once click on the login button.
Any help would be appreciable. Thanks in advance.
Code:
index.php
<?php
include_once "database.php";
?>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$sql_user="SELECT * FROM users";
$result_user=mysql_query($sql_user);
?>
<form>
<select name="users" onchange="showUser(this.value)" style="width: 210px;height: 36px; font-size:17px;">
<option style="height:30px;" value="">Select a person:</option>
<?php while($row_user=mysql_fetch_array($result_user)){?><option style="height:30px;" value="<?php echo $row_user['id'];?>"><?php echo $row_user['username'];?></option><?php }?>
</select>
</form>
<br>
<div id="txtHint"></div>
</body>
</html>
getuser.php
<style>
button{
width: 100px;
height: 38px;
background:white;
color:black;
}
</style>
<?php
$q = intval($_GET['q']);
include_once "database.php";
$sql="SELECT * FROM users WHERE id = '$q'";
$result = mysql_query($sql);
$row=mysql_fetch_array($result);
$entry=$row['entry'];
?>
<form method="post" name="">
<table>
<tr><td>
<button id="btnSave" <?php if($entry=='0'){?> style=" background:#6DAAD4;"<?php }?>> Login</button>
</td><td>
<button <?php if($entry=='1'){?> style=" background:#6DAAD4;"<?php }?>> Logout</button>
</td></tr>
</table>
</form>
in you getuser.php remove form tag and add function 'insertDateTime()' to buttons click event like:
<table>
<tr>
<td>
<button id="btnSave" onclick="insertDateTime(this,'<?php echo $q;?>');"> Login</button>
</td>
<td>
<button id="btnSave" onclick="insertDateTime(this,'<?php echo $q;?>')"> Logout</button>
</td>
</tr>
</table>
Add one function to your index.php
<script src="http://code.jquery.com/jquery-1.11.1.min.js"> </script> //better to download and us locally
<script>
function insertDateTime(obj, id)
{
$("[id^='"+obj.id+"']").css('background', '#FFFFFF');
$(obj).css('background', '#6DAAD4');
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var params = "id="+id;
xmlhttp.open("POST","insertdate.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
//alert(xmlhttp.responseText);
}
}
xmlhttp.send(params);
}
</script>
and write your code in insertdate.php to insert date and time in database. You can get your id using $_POST['id']; in insertdate.php.
Bind onclick = "func()" in button
inside func method, change the color this way :
document.getElementById("formID").style.backgroundColor="lightblue";

Stop inserting if user clicks on the active button

I have two buttons(Login and logout). When logout is active, if user clicks again on the logout button then shouldn't do insertion or updation. The same for login button also.
Now, it's inserting when user clicks on the active button. I need to avoid that.
Thanks in advance.
Code:
<script>
function insertDateTime(obj, id)
{
$("[id^='"+obj.id+"']").css('background', '#FFFFFF').css('font-weight', 'normal').css('font-size', '14px').css('color', 'grey');
$(obj).css('background', '#D4B357').css('font-weight', 'bold').css('font-size', '15px').css('color', 'black');
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var params = "id="+id;
xmlhttp.open("POST","insertdate.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
/* xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");*/
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
//alert(xmlhttp.responseText);
}
}
xmlhttp.send(params);
}
</script>
getuser.php
<table>
<tr>
<td>
<button id="btnSave" <?php if($entry=='1'){?> style=" background:#D4B357; font-weight:bold; font-size:15px; color:black;outline:0px;"<?php }?> onclick="insertDateTime(this,'<?php echo $q;?>');"> Login</button>
</td>
<td>
<button id="btnSave" <?php if($entry=='0'){?> style=" background:#D4B357; font-weight:bold; font-size:15px;color:black;outline:0px;"<?php }?> onclick="insertDateTime(this,'<?php echo $q;?>');"> Logout</button>
</td>
</tr>
</table>
insertdate.php
date_default_timezone_set("Asia/Kolkata");
$date=date("Y-m-d", time());?>
<?php $time=date("H:i:s",time());
include_once "database.php";
$q = $_POST['id'];
$sql_check="select * from entries where user_id='$q' order by id desc";/*last row*/
$res_check=mysql_query($sql_check);
$row=mysql_fetch_array($res_check);
$logout_date=$row['logout_date'];
$num=mysql_num_rows($res_check);
$id=$row['id'];
if($logout_date != "" || $num=='0'){
$sql1="insert into entries(user_id,login_date,login_time,logout_date,logout_time)values('$q','$date','$time','','')";
$res1=mysql_query($sql1);
$sql="update users set entry='1' where id='$q'";
$res=mysql_query($sql);
}
else{
$sql="update entries set logout_date='$date', logout_time='$time' where id='$id'";
$res=mysql_query($sql);
$sql1="update users set entry='0' where id='$q'";
$res1=mysql_query($sql1);
}
You can stop the user from clicking the buttons second and consecutive times by disabling the buttons after the first click using a simple JavaScript code.
Once the action is complete, you can enable the button back (or hide it as they are for login/logout).

How to insert into database using AJAX, PHP and CodeIgniter?

Please tell me how to use AJAX with CodeIgniter. I wrote the view like this but I am not sure how to send data to model with the controller.
My view is:
<html>
<head>
<script>
function insert(fname,lname,age)
{
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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_db_php.php?fname=fname&lname=lname&age=age",true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<table>
<tr><td>First Name : </td><td> <input type="text" fname="fname"/> </td> </tr>
<tr><td>Last Name : </td><td> <input type="text" fname="lname"/> </td> </tr>
<tr><td>City : </td><td> <input type="text" fname="age"/> </td> </tr>
<input type="button" onclick="insert(fname,lname,age)">
</table>
</form>
</body>
</html>
Please help me with writing the controller and model for this case.
This is the idea:
Better use post for submitting forms using ajax:
xmlhttp.open("POST","user_ajax",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname="+fname+"&lname="+lname+"&city="+city);
Model
class user extends CI_Model{
function insert(){
$data = array(
"fname" => $this->input->post('fname'),
"lname" => $this->input->post('lname'),
"city" => $this->input->post('city')
);
return $this->db->insert("TABLE_NAME",$data);
}
}
Controller
class user_ajax extends CI_Controller{
function index(){
$this->load->model("user");
if(isset($_POST['fname'])){
$ths->user->insert();
}
}
}

Modification For AJAX Instant Search

I have build an instant search with AJAX using this tutorial, I am getting instant result but the problem is the div in which result comes stays open there.
I want to modify it like after getting instant result, then
when clicked anywhere on page the result(div) should disappear.
and when mouse-over again on input field result(div) should reappear.
AJAX Code
<script type="text/javascript">
function showResult(str)
{
if (str.length==0)
{
document.getElementById("search-result").innerHTML="";
document.getElementById("search-result").style.border="0px";
return;
}
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("search-result").innerHTML=xmlhttp.responseText;
document.getElementById("search-result").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","instant-search.php?keyword="+str,true);
xmlhttp.send();
}
</script>
HTML Code
<div id="search">
<form action="" method="get" id="search-form" >
<input name="" type="" size="" id="search-input" onkeydown="showResult(this.value)"/>
<select name="" id="search-select">
<option value="all" >All</option>
<input type="hidden" name="" value="" />
</select>
<input type="submit" value="Search" id="search-btn" />
</form>
</div>
<div id="search-result"></div>
PHP Codes are simple MySQL Full Text Search query.
I tried like adding this but nothing happens
<input name="keyword" type="text" size="50" id="search-input" onkeydown="showResult(this.value)" onclick="(this.value==this.defaultValue) this.value='';" onmouseout="showResult(this.value='';)"/>
Please suggest any way of doing this.
Thanks.
A nasty way
<body>
<div id="main" onclick="fx(0)">
<div id="search">
<form action="" method="get" id="search-form" >
<input name="" type="" size="" id="search-input" onkeydown="showResult(this.value)"/>
<select name="" id="search-select">
<option value="all" >All</option>
<input type="hidden" name="" value="" />
</select>
<input type="submit" value="Search" id="search-btn" />
</form>
</div>
<div id="search-result" onclick="fx(1)"></div>
</div>
</body>
<script>
function fx(var flag)
{
// document.getElementById(theIdYouWantToShowHide).style.display="none" or inline or block ...
}
</script>
Got it finally, hope this helps others too.
<script type="text/javascript">
function showResult(str)
{
if (str.length==0)
{
document.getElementById("search-result").innerHTML="";
document.getElementById("search-result").style.border="0px";
return;
}
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("search-result").innerHTML=xmlhttp.responseText;
document.getElementById("search-result").style.border="1px solid #A5ACB2";
document.getElementById("search-result").style.display="block";
document.getElementById("search-input").onmouseover = function(){show_box()};
}
}
xmlhttp.open("GET","instant-search.php?keyword="+str,true);
xmlhttp.send();
}
function close_box(){
document.getElementById("search-result").style.display="none";
}
function show_box(){
document.getElementById("search-result").style.display="block";
}
document.onclick = function(){close_box()};
</script>

How can I send form information from a dynamically populated div #printClass into another div #dbmsg?

I am very new to php mysql ajax jquery programming and am having a problem loading addlabform's formdata from a div into a mysql database, therefore if my code is longwinded please forgive. I have php file teacher.php that has a div #printClass that was appended to with a form of checkbox values from getclass.php. I would like to take the checkbox values and pass them to a php file addlab.php that will load in another div #dbmsg with a message. I have tried using many different methods and have settled on trying to work with onclick and submit but I cannot pick up the values of the form in the first generated div #printClass. This is my html:
<div id="dbmsg"></div>This is
<div id="printClass">Labid: 100Course: 11
<br>
<form name="addlabform">
<input name="classid" value="11" type="hidden">
<input onclick="submitit()" value="SubmitIt" type="submit">
<input onclick="additnow()" value="Add Labs" type="button">
<table border="1">
<tbody>
<tr>
<th>Lastname</th>
<th>Firstname</th>
<th>Taken</th>
<th>Date</th>
</tr>
<tr>
<td>lastname1</td>
<td>firstname1</td>
<td align="center">
<input name="labdone" value="2191" id="2191" type="checkbox">
</td>
<td>
<i>Not Done</i>
</td>
</tr>
<tr>
<td>lastname2</td>
<td>firstname2</td>
<td align="center">
<input name="labdone" value="2042" id="2042" type="checkbox">
</td>
<td>
<i>Not Done</i>
</td>
</tr>
</table>
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script>$("#printClass").ready(additnow())</script>
<script>$("#printClass").ready(submitit())</script>
<script>
$(document).ready(function() {
$("#addlabform").submit(function () {
$.post("addlab.php", $("#addlabform").serialize(), function (data, textStatus) {
$("#dbmsg").append(data);
});
return false;
});
});
</script>
</div>
I have 3 functions get_check_values(), addit() and submitit(). get_check_values() is intended to get the values for the checkbox values, which does not work error console says that document.printClass is undefined. addit() works in loading the information into the div #dbmsg but I cant get the values from #printClass so until get_check_values works it is stuck. submitit() reloads the original php file with the checked values in the $_REQUEST. I would prefer to use submitit() because it would make it unnecessary to add all the input values to the addit() xmlhttp.open line. Here are the functions:
<script type="text/javascript">
function get_check_values()
{
var c_value = "";
for (var i=0; i < document.printClass.addlabform.labdone.length; i++)
{
if (document.printClass.addlabform.labdone[i].checked) {
c_value = c_value + document.printClass.addlabform.labdone[i].value;
alert(c_value);
return false;
}
}
}
function submitit() {
$(document).ready(function() {
$("#addlabform").submit(function () {
$.post("addlab.php", $("#addlabform").serialize(), function (data, textStatus) {
$("#dbmsg").append(data);
});
return false;
});
});
}
function additnow() {
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("dbmsg").innerHTML=xmlhttp.responseText;
}
}
formvals = "tid=$tid";
var classid=encodeURIComponent(document.getElementById("printClass").value);
xmlhttp.open("GET","addlab.php?+tid=$tid&classid="+classid+formvals,true);
xmlhttp.send();
}
Any help would be greatly appreciated as I have been working on this for days.
I suggest just using an ordinary form submit:
Your problem then becomes purely a PHP problem.
If there's some reason reloading the entire page is no good, you can use an iframe to ensure that only the form is reloaded (it'll still be the entire form, though):
<iframe src="addlab.php"></iframe>
Be sure to check $_POST['submitbutton'] in your PHP to check which submit button was clicked.

Categories