Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear friends
I am using ajax and php ! I have three php files ,I want to display post using ajax and insert it in my databse,Want to display previous posts on same page !!but Iam getting output another page ! here is my three php file codes !! Help me to solve this friends !!

index.php

PHP
</div>!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Freak </title>
<link rel="stylesheet" href="root/Style/Style.css">
<link rel="stylesheet" href="root/Style/content.css">
<script language="javascript" type="text/javascript">

function ajax_post(){
// Creat out XMLHttpRequest Object

var hr = new XMLHttpRequest();
// Creat some variables we need to send to out php file
var url = "my_parse_file.php";
	
var fn = document.getElementById("first_name").value;
//var ln = document.getElementById("last_name").value;	
//var vars = "firstname="+fn+"&lastname="+ln;
var vars = "firstname="+fn;
hr.open("POST",url,true);
//set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function(){
	if(hr.readyState==4 && hr.status==200) {
		var return_data = hr.responseText;
			document.getElementById("status").innerHTML = return_data;
		}
	}


// send the data to PHP now ... and wait for response to update the status div
hr.send(vars);// actually execute the request
document.getElementById("status").innerHTML = "processing...";
document.getElementById("first_name").value="";
}
</script>   
</head>
<body>
<?php include_once("Template_PageTop.php");>
<div id="pageMiddle">
<div id="sidebar">
<img src="root/Style/noimage.gif" alt="" id="profilepic" >
<div id="profilename">Jems Saiyed </div>
<a href=""><img src="root/Style/friend reqest_sidebar.png" alt=""></a> <a href=""> Friends </a>
<br>
<a href=""><img src="root/Style/classmates_sidebar.png" alt=""></a> <a href=""> Classmates </a>
<br>
<a href=""><img src="root/Style/photos_sidebar.png" alt=""></a> <a href="">Photos</a>
<br>
<a href=""><img src="root/Style/message_sidebar.png" alt=""></a> <a href="">Messages</a>
</br></br></br></div>
<div id="sidebar-b"> Status of the week </div>
<div id="content">
<br>
<a href=""><img src="root/Style/photos_sidebar.png" alt=""> Share Photo/Video</a>
<br>
<form action="inst.php" method="post">
<textarea  name="first_name"  style="width:50%;height:100px;"> </textarea>
<input name="myBtn" type="submit"  value="post" onClick="javascript:ajax_post();" style="margin-left:45%">
</form>
<div id="contener">
<div id="status" style="margin:10px;"></div>
</div>
</br></br></div>
</div>
<?php include_once("Template_PageBottom.php"); >
</body>
</html>

my_pase_file.php

<php <br mode="hold" />echo $_POST['firstname'];

>>inst.php

$con = mysqli_connect('localhost',"root","");
mysqli_select_db($con,"dbsocial");
$firstname = $_POST['first_name'];
echo $firstname;
$sql = "SELECT * FROM status";
mysqli_query($con,"INSERT INTO status (data) VALUES ('$firstname') ");

$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
	echo "<hr>".$row['data'];"<hr>";	
}
$output = mysqli_query($con,$sql);

mysqli_close($con);
Posted
Updated 8-Jul-13 17:39pm
v4
Comments
Zoltán Zörgő 8-Jul-13 14:03pm    
Please, use proper formatting. Your post is looking ugly and it is unreadable.
Md Jamaluddin Saiyed 9-Jul-13 3:25am    
now you can properly read the code ! Its edited !
Sudhakar Shinde 8-Jul-13 23:41pm    
Hi Md Jamaluddin, Tried to format the code however please check if the code matches with your original post.
Md Jamaluddin Saiyed 9-Jul-13 3:24am    
Thanks for editing the code

1 solution

Replace

Quote:
<textarea name="first_name" id = "first_name" style="width:50%;height:100px;"> </textarea>
<input name="myBtn" type="submit" value="POST" onClick="javascript:ajax_post();" style="margin-left:45%">


instead of -
Quote:
<form action="inst.php" method="post">
<textarea name="first_name" style="width:50%;height:100px;"> </textarea>
<input name="myBtn" type="submit" value="post" onClick="javascript:ajax_post();" style="margin-left:45%">
</form>


and other file inst.php

Quote:

$firstname = $_POST['firstname'];
$con = mysqli_connect('localhost',"root","");
mysqli_select_db($con,"dbsocial");
$sql = "SELECT * FROM status Order by Id DESC";
mysqli_query($con,"INSERT INTO status (data) VALUES ('$firstname')");
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "
".$row['data'];"
";
}
$output = mysqli_query($con,$sql);
mysqli_close($con);
?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900