i am printing the values using ajax. Can i store those value in database?
jhilmil123 0 Newbie Poster
LastMitch
Can i store those value in database?
Yes. You can insert data to the database and used ajax to fetch the data.
jhilmil123 0 Newbie Poster
my data is printing through ajax query.But i don't know the way how to insert those values in my database?
LastMitch
my data is printing through ajax query.But i don't know the way how to insert those values in my database?
Read and try this (there's a few codes in link that you can modify to suited your code):
http://zwaroop.wordpress.com/2011/12/27/insert-data-into-mysql-using-php-jquery-ajax/
jhilmil123 0 Newbie Poster
sorry LastMitch, i do not want this. i want to print values with ajax and values which is printing by ajax... that particular values i want to store in database....
i do not to enter values manually by a html form.
please help me.........:(
LastMitch
i want to print values with ajax and values which is printing by ajax... that particular values i want to store in database....
What do you have so far? Can you post your AJAX code it should look something like this:
$(function() {
$('#data').change(function() {
$.get( '../data.php?id=' + $(this).val(),
function(html) {
$('#txt').html(html);
});
});
});
Do you have at least a form?
In the end you still need a form and query and database in order for this to work.
Edited by LastMitch because: grammer
broj1 356 Humble servant Featured Poster
You basically need another ajax call passing values to a php script that will insert/update database.
jhilmil123 0 Newbie Poster
i have two pages
1)batchinfo.php
<?php
require_once "Connections/config.inc.php";
if(isset($_REQUEST[add]))
{
$id1 =$_GET[id5];
$type1 = 'New';
$type = $_GET[type];
$batchno= $_REQUEST[batchno];
$session= $_REQUEST[session];
$startdate = $_REQUEST[startdate];
$enddate = stripslashes($_REQUEST[enddate]);
$CID2 = $_REQUEST[select2];
$courseid= addslashes($_REQUEST[courseid]);
if($type=="edit")
{
echo $Updat ="UPDATE batchinfo SET courseid='$CID2',batchno='$batchno',session='$session',startdate='$startdate',enddate='$enddate',coursename='$coursename',courseduration='$courseduration',feedetails='$feedetails' WHERE batchid='$id1'";
$relts =mysql_query($Updat)or die(mysql_error());
if($relts)
{
?>
<script>
alert(" Student Entry successfully Updated!!!");
window.location.href="?inc=manage-batchinfo";
</script>
<?php
}
}
else
{
echo $query ="INSERT into batchinfo(courseid,batchno,session,startdate,enddate,coursename,courseduration,feedetails)values('$CID2','$batchno','$session','$startdate','$enddate','$coursename','$courseduration','$feedetails')";
$res =mysql_query($query)or die(mysql_error());
if($res)
{
?>
<script>
alert(" Student Entry successfully added!!!");
window.location.href="?inc=manage-batchinfo";
</script>
<?php
}} }
$id1 =$_GET[id5];
if($id1)
{
$relt =mysql_query("SELECT * from batchinfo where batchid='$id1'")or die(mysql_error());
$rows1 =mysql_fetch_array($relt);
$id4 =$rows1[batchid];
}
?>
<html>
<head>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("select2").value;
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("responsdiv").innerHTML=xmlhttp.responseText;
}
}
alert(str);
xmlhttp.open("GET","batchinfo1.php?q="+str,true);
xmlhttp.send();
}
</script>
<script language="javascript" type="text/javascript" src="datetimepick/datetimepicker.js">
</script>
</head>
<body >
<p align="center">
<form action="" method="post" enctype="multipart/form-data" name="form-product" >
<input type="hidden" name="type" value="<?php $type?>">
<input type="hidden" name="id4" value="<?php $id1?>">
<input type="hidden" name="select" value="<?php $id3?>">
<table width="96%" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th> </th>
<td colspan="2"> </td>
</tr>
<tr>
<th align="left"><span class="style1">Batch No:</span></th>
<td colspan="2" align="left">
<input type="text" name="batchno" id="batchno" value="<?php echo $rows1[batchno]?>" /><br />
</td>
</tr>
<tr>
<th> </th>
<td colspan="2"> </td>
</tr>
<tr>
<th align="left" class="style1"><strong>Session:</strong></th>
<td colspan="2" align="left">
<input type="text" name="session" id="session" value="<?php echo $rows1[session]?>"/>
</td>
</tr>
<tr>
<th> </th>
<td colspan="2"> </td>
</tr>
<tr>
<th align="left" class="style1"><strong>Start Date:</strong></th>
<td colspan="2" align="left">
<input id="demo1" name="startdate" value="<?php echo $rows1[demo1]?>" size="25"><a href="javascript:NewCal('demo1','mmddyyyy')"><img alt="Pick a date" src="datetimepick/cal.gif" border="0" width="17" height="16"></a></p>
<p class="mycode">
</td>
</tr>
<tr>
<th> </th>
<td colspan="2"> </td>
</tr>
<tr>
<th align="left" class="style1"><strong>End Date:</strong></th>
<td colspan="2" align="left">
<input id="demo2" size="25" name="enddate" value="<?php echo $rows1[demo2]?>"><a href="javascript:NewCal('demo2','mmddyyyy')"><img alt="Pick a date" src="datetimepick/cal.gif" border="0" width="17" height="16"></a></p>
<p class="mycode">
</td>
</tr>
<?php
$relt =mysql_query(" SELECT * from courseinfo" )
or die(mysql_error());
$rows =mysql_fetch_array($relt);
$couid =$rows[courseid];
?>
<tr>
<th> </th>
<td colspan="2"> </td>
</tr>
<tr>
<th align="left"><span class="style1">Course Category: </span></th>
<td colspan="2">
<select name="select2" id="select2" onChange="showUser(this.value)" >
<option value="">========= Select Course =========</option>
<option value="0" class="headline"> course name </option>
<?php
$sql2=mysql_query(" select * from courseinfo ");
while($row2=mysql_fetch_array($sql2))
{
if($CID2 == $row2[courseid])
{
?>
<option value="<?php echo $row2[courseid]?>" selected><?php echo $row2[coursename]?></option>
<?php
}
else
{
?>
<option value="<?php echo $row2[courseid]?>"><?php echo $row2[coursename]?></option>
<?php
}
}
?>
</select>
</td>
</tr>
<tr>
<td>
<div id="responsdiv">
</div>
</td>
</tr>
<tr>
<th> </th>
<td colspan="2"> </td>
</tr>
<tr>
<th> </th>
<td colspan="2" align="left">
<?php
if($_GET[type]=='edit')
{
?>
<input type="submit" name="add" value="UPDATE" border="0"/>
<input name="cancel" type="button" value="CANCEL" onClick="javascript: alert('Cancelled!');self.location='home.php'"/>
<?php
}
else
{
?>
<input type="submit" name="add" value="INSERT" border="0"/>
<input name="cancel" type="button" value="CANCEL" onClick="javascript: alert('Cancelled!');self.location='home.php'"/>
<?php
}
?>
</td>
</tr>
</table>
</form>
</body>
</html>
in this page i use ajax query for fetching the data from table and this coding is running properly and also printing data. but i want the store that particular data which is coming from the ajax query.... so please tell me can it possible if yes then how?
and my second page is batchinfo1.php from where ajax query get its response.
2)batchinfo1.php
<?php
require_once "Connections/config.inc.php";
echo $q=$_GET["q"];
$sql="SELECT * FROM courseinfo WHERE courseid = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Course Name</th>
<th>Course Duration</th>
<th>Fee Details</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['coursename'] . "</td>";
echo "<td>" . $row['courseduration'] . "</td>";
echo "<td>" . $row['feedetails'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
jhilmil123 0 Newbie Poster
Guys, if u feel any kind of problem please inform me....i'll help you...
jhilmil123 0 Newbie Poster
please give me some solution whatever you have... i need your help......Thanks in advance
broj1 356 Humble servant Featured Poster
If I understood: you get data about courses with batchinfo1.php through ajax call. The result is displayed in a html table. Now you want to save some of this info into some other database table, right? Which data would you like to save (some of it or all of it)? The data is already in the database so it would be sensible to store only IDs or something like that. What is the trigger for saving (is it a submit button)?
I any case you would have to give some ID to each cell with the data so you can read it using getElementById(). So i.e. on click of a submit button you will read the data and shoot the ajax, calling another php script that will save the data.
That is all I can say at the moment since I do not have enough information. I am not even sure whether I got the problem right.
LastMitch
in this page i use ajax query for fetching the data from table and this coding is running properly and also printing data. but i want the store that particular data which is coming from the ajax query
There's something wrong with your <form> on line 104 to line 107.
What error did you get when you ran the code? There should be an error.
So far I notice there's something wrong with that. The javascript won't submit.
There's a few errors already, like what bro1 mention I also not sure where to pinpoint the issue.
The reason is you never mention anything regarding about errors and why it's not working.
You only mention you need help so that's not very helpful to anyone.
jhilmil123 0 Newbie Poster
broj1 u are right, i am getting correct output and i want to store that html table data in batchinfo table....and my id is also passing correctly.....
can it possible? if yes then how?
jhilmil123 0 Newbie Poster
LastMitch there is no problem in my form and its not giving any kind of error.its running correctly.
i just want to know can i save that data which i print through ajax.....??????
broj1 356 Humble servant Featured Poster
In the batchinfo1.php that generates the table, give the table an ID so you can refer to it later:
echo "<table id='result-table' border='1'>
You did not answer my question about what will triggers the saving of data into database. I assume here that it will be a submit button at the end of the table.
Add a submit button after the table code:
echo '<input type="button" value="Save" onclick="saveTableData()" />';
Add javascript code that will run onclick of a submit button (adapted from here). Another assumtion here is that you have only text in cells no other html elements.
function saveTableData() {
// create an array that will hold values which you will pass to
var resultArray = new Array();
// get the table by it's ID
var refTab = document.getElementById("result-table")
// Loop through all rows
for(var i = 0; i < refTab.rows.length; i++) {
// one row
var row = refTab.rows.item(i);
// loop through cells in a row
for(var j = 0; j < row.cells.length; j++) {
// one cell
var cell = row.cells.item(j);
// add cell value to the result array
resultArray[row][cell] = cell.firstChild.innerText);
}
}
stringify it so it can be POSTed
var parameterString = JSON.stringify(resultArray);
// do an ajax call to another php script that will store the data in the DB
// using parameterString as a parameter
// (you will use json_decode($_POST['jsondata']) to decode it in php script)
...
}
I haven't tested the code but you will get the concept from comments. You can research deeper and adapt it to suit your needs. It also isn't the only way you can do it.
And very important: do not forget to clean the input in the PHP script using mysql_real_escape_string() and htmlentities() or similar function otherwise you are open to trouble.
Edited by broj1
broj1 356 Humble servant Featured Poster
It only ocurred to me after preparing the previous post: whay do you want to save data that is already in the database to another table? Are you sure your data is normalized properly? Or, maybe you could do it oin the batchinfo1.php script?
Edited by broj1
jhilmil123 0 Newbie Poster
<?php
require_once "Connections/config.inc.php";
$q=$_GET["q"];
$sql="SELECT * FROM courseinfo WHERE courseid = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1' id='result-table'>
<tr>
<th>Course Name</th>
<th>Course Duration</th>
<th>Fee Details</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['coursename'] . "</td>";
echo "<td>" . $row['courseduration'] . "</td>";
echo "<td>" . $row['feedetails'] . "</td>";
echo "</tr>";
echo $insert='INSERT INTO batchinfo(coursename, courseduration, feedetails) VALUES ("'.$row['coursename'].', '.$row['courseduration'].','.$row['feedetails'].'")';
$sql=mysql_query($insert);
}
echo "</table>";
?>
i want to insert those values in the table. how can i do it?
i guess now u will clear and understand with my problem
broj1 356 Humble servant Featured Poster
Repeating my question: what is the trigger to do insert? If there is no trigger other than displaying the table, then you can do it in the batchinfo1.php script. But this database would not be normalized in other words you will get duplicate values in the courseinfo and batchinfo tables which is not the right way of doing it.
jhilmil123 0 Newbie Poster
ohoh... i dont know about the trigger... please tell me how to do it?
Thanks broj1....
broj1 356 Humble servant Featured Poster
Sorry to keep running in circles but I am trying to get what is the purpose of all this. Please confirm that:
- you read data about the courses from the courseinfo table and display information about one course (depending on the course ID)
- you would like to save all of this same data about one course into batchinfo table which is another table in the same database
If this is true be aware that this is duplication of data and against the normalization rules. But you can do it. Please confirm that I see it correctly.
jhilmil123 0 Newbie Poster
yes you are right broj1. please tell me the way how can i do it.
thanks for replying....
broj1 356 Humble servant Featured Poster
I am trying to test this in my environment but it is hard without the data. Can you post the structure and some data for the courseinfo and the batchinfo tables. Please do it in SQL INSERT format. The easiest way of doing it is exporting the tables in phpmyadmin with default values.
jhilmil123 0 Newbie Poster
hello broj1, i am not getting you properly maens what you want? please tell me exactly what you need for this code?
broj1 356 Humble servant Featured Poster
I need some data so I can do a test in my environment. I hope you use phpmyadmin to manage your database. Phpmyadmin allows you to export data from a table. So click on the table name in the left pane and select the Export menu on the top of the right (main) pane. Leave options as they are and save the SQL file on your machine and then post the contents here. If the table contains confidential information, remove it before posting.
If you do not use phpmyadmin then you can run the following queries from mysql client or a PHP script. To get the table structure run these two queries:
SHOW CREATE TABLE courseinfo;
SHOW CREATE TABLE batchinfo;
To display some data run the following two queries:
SELECT * FROM courseinfo LIMIT 10;
SELECT * FROM batchinfo LIMIT 10;
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.