Not able to retrieve data from database PHP, AJAX - php

this is my first post here.
I am making a simple website in which user can comment and its comment will be saved in database.
I have created a table in database with three columns "id", "name", "comment" (name is not imp.).
Now i wanted to make a "See more" button. I Have three files in root directory "config.php","index.php","ajax_more.php".
This is my ajax_more.php
<?php
include("config.php");
if(isSet($_POST['lastmsg']))
{
$lastmsg=$_POST['lastmsg'];
$result=mysql_query("select * from commenttable where id<'$lastmsg' order by id desc limit 5");
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$id=$rows['id'];
$name=$rows['name'];
$comment=$rows['comment'];
?>
<li>
<?php echo "#" . $id . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<hr size="0"/>'; ?>
</li>
<?php
}
?>
<div id="more<?php echo $id; ?>" class="morebox">
more
</div>
<?php
}
?>
this is index.php
<?php
include('config.php');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<div id="fb-root"></div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
//More Button
$('.more').live("click",function()
{
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();
}
});
}
else
{
$(".morebox").html('The End');
}
return false;
});
});
</script>
<style>
body{font-family:Arial,Helvetica,sans-serif;
width:50%;
position:relative;
margin-left:auto;
margin-right:auto;
}
a { text-decoration:none; color:#0066CC}
a:hover { text-decoration:underline; color:#0066cc }
*
{ margin:0px; padding:0px }
ol.timeline
{ list-style:none}ol.timeline li{ position:relative;border-bottom:1px #dedede dashed; padding:8px; }ol.timeline li:first-child{}
.morebox
{
font-weight:bold;
color:#333333;
text-align:center;
border:solid 1px #333333;
padding:8px;
margin-top:8px;
margin-bottom:8px;
-moz-border-radius: 6px;-webkit-border-radius: 6px;
}
.morebox a{ color:#333333; text-decoration:none}
.morebox a:hover{ color:#333333; text-decoration:none}
#container{margin-left:60px; width:580px }
</style>
<title>Comment box</title>
</head>
<body>
<center>
<form action="index.php" method="POST">
<table font-family="inherit">
<tr><td colspan="2">Comment: </td></tr>
<tr><td colspan="5"><textarea name="comment" rows="10" cols="50"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
</table>
</form>
<div id='container'>
<ol class="timeline" id="updates">
<?php
$dbLink = mysql_connect("mysql17.000webhost.com", "a1360777_tester", "9868364058?");
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
$getquery=mysql_query("SELECT * FROM commenttable ORDER BY id DESC LIMIT 5");
while($rows=mysql_fetch_array($getquery))
{
$id=$rows['id'];
$name=$rows['name'];
$comment=$rows['comment'];
?>
<li>
<?php echo "#" . $id . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<hr size="0"/>'; ?>
</li>
<?php } ?>
</ol>
<div id="more<?php echo $id; ?>" class="morebox">
more
</div>
</div>
</body>
</html>
This is my config.php
<?php
mysql_connect("mysql17.000webhost.com","a1360777_tester","9868364058?");
mysql_select_db("a1360777_test");
$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];
$dbLink = mysql_connect("mysql17.000webhost.com", "a1360777_tester", "9868364058?");
mysql_query("SET character_set_client=utf8", $dbLink);
mysql_query("SET character_set_connection=utf8", $dbLink);
if($submit)
{
if($comment)
{
$insert=mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment') ");
?>
<script type="text/javascript">
window.location = "http://sonymobile.comule.com/";
</script>
<?php
}
else
{
echo "Please Fill out all Fields";
}
}
?>
After i click on more button it posts only (echo) '#' from ajax and not the comment.
please help, Dont getting what to do.

Change your while statement to a foreach as such:
$row=mysql_fetch_array($result);
foreach($row as $key)
{
$id=$key['id'];
$name=$key['name'];
$comment=$key['comment'];
?>
<li>
<?php echo "#" . $id . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<hr size="0"/>'; ?>
</li>
<?php
}
?>
<div id="more<?php echo $id; ?>" class="morebox">
more
</div>
<?php
}
?>
Let me know if that works

Related

How to make an id stick when trying to leave a post comment

I'm having trouble on having my id stick when I'm trying to make a comment on a post on my site. So far when I click on the comment it has its id (ex. id=4), however, when I click on 'leave comment' the id doesn't stick and just says (id=) and not the number so I can't leave a comment because it doesn't know where to leave the comment.
I've tried defining a variable $comments = $_GET['id']; and putting it in the form action but that didn't seem to work. I've tried putting "?id= . $_GET['id']; in the form action as well, but that also didn't seem to work.
I have put starts next to what I think is the important part of my code
<?php
session_start();
?>
<html>
<!-- this includes the header and mysqli pages-->
<?php
include('includes/header.html');
include('includes/mysqli_connect.php');
?>
<head>
</head>
<body>
<!-- this styles the css for my form to make it look nice -->
<style type="text/css">
body{
font-family: Arial, Gadget, sans-serif;
background-color: #FFD9D9;
}
strong{
color: red;
font-weight: bold;
}
center{
margin: auto;
}
</style>
<!-- have access to posting special characters and will trim unnecessary spaces-->
<?php
$userid = mysqli_real_escape_string($dbc, trim($_SESSION['user_id']));
$blogid = mysqli_real_escape_string($dbc, trim($_GET['id']));
$comment = mysqli_real_escape_string($dbc, trim($_POST['comment']));
***$comments = $_GET['id'];***
#This adds in the comment based on their ID, email,fname,lname and date they made the comment
if (($title != NULL) && ($post != NULL)){
$query = "INSERT INTO comments (comid, userid, blogid, comment, comdate) VALUES (NULL, '$userid', '$blogid', '$comment', NOW());";
$result = mysqli_query($dbc, $query);
#This displays an error if there was an error in making a comment
#EDIT: The "There was an error" never will display since my form validates if criteria was filled out or not, if it's not it makes the guest fill out that area
if ($result) {
echo "<center><strong>Thank you, the comment has been added.</strong></center><br /><br />";
$title = NULL;
$title = NULL;
}
else {
echo "<strong>There was an error! </strong>" . mysqli_error($dbc);
}
}
?>
<form action="<?php echo basename(__FILE__) . ***"?id=" . $comments;*** ?>" method="post">
<?php
if(($_POST['comment'] == NULL) && ($_SERVER['REQUEST_METHOD'] == 'POST')){
echo "<strong><center>Please fill in a comment!</center></strong><br />";
}
?>
<p><center><label><b>Leave comments here: </p></center></label></b>
<p align="center"><textarea name="comment" cols="40" rows="5">
<?php echo $_POST['comment'];
?>
</textarea>
</p>
<br />
<p align="center"><input type="submit" name="submit" value="Submit" /></p>
</form>
<!--this includes the footer page-->
<?php
include('includes/footer.html');
?>
</body>
</html>
comments.php code:
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<!-- this styles the css for my form to make it look nice -->
<style type="text/css">
body{
font-family: Arial, Gadget, sans-serif;
background-color: #FFD9D9;
}
strong{
color: red;
font-weight: bold;
}
center{
border-left: .17em dashed;
border-top: .17em solid;
border-right: .17em dashed;
border-bottom: .17em solid;
padding-left:25px;
padding-bottom: 20px;
width: 1000px;
background-color: #E1A0A0;
border-color: black;
margin: auto;
text-align: left;
}
h3 {
text-align: center;
color: red;
}
</style>
<!-- this includes the header and mysqli pages-->
<?php
include('includes/header.html');
include('includes/mysqli_connect.php');
?>
<?php
$blogid = $_GET['id'];
?>
<?php
$query = "SELECT blogid, title, post, DATE_FORMAT (postdate, '%M, %d, %Y') AS date, post FROM blogposts WHERE blogid=$blogid";
$result = mysqli_query($dbc, $query);
?>
<!-- this will display the ID, fname, lname, email, comment, and date posted gathered from the MYSQL database-->
<?php
while ($rowb = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<center><h2>" . $rowb['title'] . "</h2>" . "<b>Posted On: </b>" . $rowb['date'] . "<br/>" . '<br/>Leave Post Comment' . "\n";
if($_SESSION['user_id'] == 3) {
echo '| <a href="update.php?id=' . $row['blogid'] . '" >Update Blog Post</a> | <a href="' . basename(__FILE__) . '?id=' . $row['blogid'] . '" >Delete Blog Post</a>';
}
echo "</center>";
}
?>
<div align="center">
<h2>
Comments
</h2>
</div>
<?php
//define query
$q = "SELECT * FROM comments JOIN users USING (userid) WHERE $blogid";
$r = mysqli_query ($dbc, $q); //run query
?>
<!-- this will display the ID, fname, lname, email, comment, and date posted gathered from the MYSQL database-->
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo "<center><br/>" . "<b>Comment: </b>" . $row['comment'] . "<br /><br/>" . "<b>ID: </b>" . $row['comid'] . "<br />" . "<b>First Name: </b>" . $row['fname'] . "<br />" . "<b>Last Name: </b>" . $row['lname'] . "<br />" . "<b>Email: </b>" . $row['email'] . "<br />" . "<b>Posted At: </b>" . $row['comdate'] . "<br/></center>\n";
}
?>
<!--this includes the footer page-->
<?php
include('includes/footer.html');
?>
</body>
</html>
I would like the id to stick so I can put in a comment and it will show up on my site.
Yep, its all ok, but you have bad prompt in your code:
<a href="leavecomments.php?id=' . $row['blogid'] . '" not use $row but $rowb

To reload the insert page when deletion occurs in php

here I am going to delete the database record, when i delete the record, the record deletes successfully but it dosen't reload the insert_search page it displays the result and deleted result will not go, so i need to type every time insert_search in url to get the updated result, please can u tell me where i need to reload the page and how to implement it, thank you in advance...
<?php
$user = "root";
$server = "localhost";
$password = "";
$db = "coedsproddb1";
$dbconn = mysql_connect($server, $user, $password);
mysql_select_db($db, $dbconn);
?>
<html>
<head>
<title>Insert</title>
<link rel="stylesheet" href="css/jquery-ui.css">
<script src="js/jquery-1.12.4.js"></script>
<script src="js/jquery-ui.js"></script>
<style>
#display {
color: red;
font-size: 12px;
text-align: center;
}
.logo {
padding: 5px;
float: left;
}
header {
background-color: #074e7c;
height: 60px;
width: 100%;
text-align: center;
color: white;
font-size: 40px;
}
#wrap {
text-align: center;
}
</style>
</head>
<body>
<header><img src="images/ipoint.png" class="logo" /> USER REGISTRATION</header>
<div class="container">
<h1 style="text-align:center">ADDING THE USER DETAILS</h1>
<form name="useradd" id="useradd" action="#" method="post">
<table align='center' border='1'>
<tr>
<td>
<label for="userName">UserName</label>
</td>
<td>
<input id="userName" name="userName" type="text" />
</td>
</tr>
<tr>
<td>
<label for="userEmail">Email</label>
</td>
<td>
<input id="userEmail" name="userEmail" type="text" />
</td>
</tr>
<tr>
<td>
<label for="userPassword">password</label>
</td>
<td>
<input id="userPassword" name="userPassword" type="password" />
</td>
</tr>
</table>
<br>
<div id="wrap">
<input type="submit" name="add" value="add" id="add">
<input type="submit" name="update" value="update" id="update">
</div>
</form>
<div id="display">
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#add").click(function(e) {
var userName = $("#userName").val();
var userEmail = $("#userEmail").val();
var userPassword = $("#userPassword").val();
var dataString = 'userName=' + userName + '&userEmail=' + userEmail + '&userPassword=' + userPassword;
alert(dataString);
if (userName == "" || userEmail == "" || userPassword == "") {
document.getElementById("display").innerHTML = "Please Enter The Fields";
} else if (!validate1($.trim(userName))) {
document.getElementById("display").innerHTML = "Please Enter The Valid UserName";
document.getElementById("display").focus();
} else if (!ValidateEmail($.trim(userEmail))) {
document.getElementById("display").innerHTML = "Please Enter The Valid Emailid";
document.getElementById("display").focus();
} else {
$.ajax({
type: "POST",
url: "insert.php",
data: dataString,
cache: false,
success: function(result) {
//alert("submitted"+result);
$('#display').html(result);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
e.preventDefault();
});
function validate1(userName) {
var u = userName;
var filter = /^[a-zA-Z0-9]+$/;
if (filter.test(u)) {
return true;
} else {
return false;
}
}
function ValidateEmail(userEmail) {
var e = userEmail;
var filter = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (filter.test(e)) {
return true;
} else {
return false;
}
}
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#update").click(function(e) {
alert("hi");
var userName = $("#userName").val();
var userEmail = $("#userEmail").val();
var userPassword = $("#userPassword").val();
var dataString = 'userName=' + userName + '&userEmail=' + userEmail + '&userPassword=' + userPassword;
alert(dataString);
if (userEmail == "" || userPassword == "") {
document.getElementById("display").innerHTML = "Please Enter The Fields";
} else {
$.ajax({
type: "POST",
url: "user_update.php",
data: dataString,
cache: false,
success: function(result) {
//alert("submitted"+result);
$('#display').html(result);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
e.preventDefault();
});
});
</script>
</body>
</html>
insert.php
<html>
<head>
<title>Insertion</title>
</head>
<body>
<div id="display">
<?php
include('db.php');
$userName = mysql_real_escape_string($_POST['userName']);
$userEmail = mysql_real_escape_string($_POST['userEmail']);
$userPassword = mysql_real_escape_string($_POST['userPassword']);
$regDate = date("Y-m-d");
function generateCode($characters)
{
$possible = '23456789abcdefghjkmnpqrstuvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible) - 1), 1);
$i++;
}
return $code;
}
$registration_key = generateCode(10);
$str = "insert into coeds_user(userName,userEmail,userPassword,regDate,registration_key) values('$userName','$userEmail','$userPassword','$regDate','$registration_key')";
echo $str;
$query = mysql_query($str);
if ($query) {
$display = "Success";
} else {
$display = "Failed";
}
$string = "select * from coeds_user";
$query2 = mysql_query($string);
$display .= "<table border='1'>";
$display .= "<tr><th>UserId</th><th>UserName</th><th>UserEmail</th><th>UserPassword</th><th>RegDate</th><th>RegistrationKey</th></tr>";
while ($result = mysql_fetch_array($query2)) {
$display .= "<tr>";
$display .= "<td>" . $result['userId'] . "</td>";
$display .= "<td>" . $result['userName'] . "</td>";
$display .= "<td>" . $result['userEmail'] . "</td>";
$display .= "<td>" . $result['userPassword'] . "</td>";
$display .= "<td>" . $result['regDate'] . "</td>";
$display .= "<td>" . $result['registration_key'] . "</td>";
$display .= "<td><a href='user_update.php?user_Id=" . $result['userId'] . "'>Edit</a></td>";
$display .= "<td><a href='user_delete.php?user_Id=" . $result['userId'] . "'>Delete</a></td>";
$display .= "</tr>";
}
$display .= "</table>";
location . reload();
echo $display;
?>
</div>
</body>
</html>
user_delete.php
<html>
<head>
<title>Deletion</title>
<link rel="stylesheet" href="css/jquery-ui.css">
<script src="js/jquery-1.12.4.js"></script>
<script src="js/jquery-ui.js"></script>
<style>
#display {
color: red;
font-size: 12px;
text-align: center;
}
.logo {
padding: 5px;
float: left;
}
header {
background-color: #074e7c;
height: 60px;
width: 100%;
text-align: center;
color: white;
font-size: 40px;
}
#wrap {
text-align: center;
}
</style>
</head>
<body>
<header> <img src="images/ipoint.png" class="logo" /> USER REGISTRATION</header>
<div class="container">
<h1 style="text-align:center">DELETE WINDOW</h1>
<div id="display">
<?php
include('db.php');
if (isset($_GET['user_Id'])) {
$userid = $_GET['user_Id'];
}
?>
<form action="user_delete.php" name="user_delete" method="post">
<input type="hidden" name="user_Id" id="user_Id" value="<?php
if (isset($userid))
echo $userid;
?>">
<?php
include('db.php');
$s = "delete from coeds_user where userId=$userid";
$query3 = mysql_query($s);
if ($query3) {
$display = "Delete Is Successful";
} else {
$display = "Delete Is Unsuccessful";
}
$string = "select * from coeds_user";
$query5 = mysql_query($string);
$display .= "<table border='1'>";
$display .= "<tr><th>UserId</th><th>UserName</th><th>UserEmail</th><th>UserPassword</th><th>RegistrationDate</th><th>RegistrationKey</th></tr>";
while ($res1 = mysql_fetch_array($query5)) {
$display .= "<tr>";
$display .= "<td>" . $res1['userId'] . "</td>";
$display .= "<td>" . $res1['userName'] . "</td>";
$display .= "<td>" . $res1['userEmail'] . "</td>";
$display .= "<td>" . $res1['userPassword'] . "</td>";
$display .= "<td>" . $res1['regDate'] . "</td>";
$display .= "<td>" . $res1['registration_key'] . "</td>";
}
echo $display;
?>
</div>
</form>
</div>
</body>
</html>
user_update.php
<html>
<head>
<title>Updation</title>
</head>
<body>
<div id="display">
<?php
include('db.php');
if (isset($_GET['user_Id'])) {
$userid = $_GET['user_Id'];
echo $userid;
$s = "select * from coeds_user where userId=$userid";
echo $s;
$query1 = mysql_query($s);
$res = mysql_fetch_array($query1);
?>
<input type="hidden" name="userPassword" id="userPassword">
<input type="hidden" name="userEmail" id="userEmail">
<form action="user_update.php" name="user_update" method="post">
<input type="hidden" name="user_Id" id="userId" value="<?php
if (isset($userid))
echo $userid;
?>">
<table align='center' border='1'>
<tr>
<td>
<label for="userName">UserName</label>
</td>
<td>
<input id="userName" name="userName" type="text" value="<?php
echo $res['userName'];
?> " />
</td>
</tr>
<tr>
<td>
<label for="userEmail">Email</label>
</td>
<td>
<input id="userEmail" name="userEmail" type="text" value="<?php
echo $res['userEmail'];
?> " />
</td>
</tr>
<tr>
<td>
<label for="userPassword">password</label>
</td>
<td>
<input id="userPassword" name="userPassword" type="password" value="<?php
echo $res['userPassword'];
?> " />
</td>
</tr>
</table>
<input type="submit" name="modify" id="modify" value="modify">
<?php
}
include('db.php');
if (isset($_POST['user_Id'])) {
$userid = $_POST['user_Id'];
echo $userid;
}
if (isset($_POST['modify'])) {
echo $userid;
$userName = mysql_real_escape_string($_POST['userName']);
$userEmail = mysql_real_escape_string($_POST['userEmail']);
$userPassword = mysql_real_escape_string($_POST['userPassword']);
$string = "update coeds_user set userName='$userName',userEmail='$userEmail', userPassword='$userPassword' where userId=$userid";
echo $string;
$query = mysql_query($string);
if ($query) {
$display = "Update Successful";
} else {
$display = "Update Failed";
}
$s = "select * from coeds_user";
$query = mysql_query($s);
$display .= "<table border='1'>";
$display .= "<tr><th>UserId</th><th>UserName</th><th>UserEmail</th><th>UserPassword</th><th>RegDate</th><th>RegistrationKey</th></tr>";
while ($res = mysql_fetch_array($query)) {
$display .= "<tr>";
$display .= "<td>" . $res['userId'] . "</td>";
$display .= "<td>" . $res['userName'] . "</td>";
$display .= "<td>" . $res['userEmail'] . "</td>";
$display .= "<td>" . $res['userPassword'] . "</td>";
$display .= "<td>" . $res['regDate'] . "</td>";
$display .= "<td>" . $res['registration_key'] . "</td>";
$display .= "</tr>";
}
$display .= "</table>";
echo $display;
}
?>
</div>
</body>
</form>
</html>
Use
echo "<script>location.replace('user_delete.php');</script>";
instead of header location.
Hope this helps.

How to do editing and deleting function in auto generated table in php ,mysql

Actually am new in php, i created one fetching page in php. Where am fetching data from the database and will display on the auto generated table. I added two button also. One for Delete the specific row from database and another one for edit the details from the database. In database email_id column is unique. So both the Delete Edit operation will do by email_id. Can Any one tell me , how i write Ajax function for editing and deleting. Code is given below
<html>
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><!--Dquip--></title>
<link href="http://fonts.googleapis.com/css?family=Abel|Arvo" rel="stylesheet" type="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.dropotron-1.0.js"></script>
<script type="text/javascript" src="jquery.slidertron-1.0.js"></script>
<style>
label
{
display:inline-block;
width:150px;
margin-right:10px;
text-align:;
}
table,tr,th,td
{
border: 2px solid dodgerblue;
border-collapse: separate;
}
table
{
width:75%;
margin-top: 8%;
}
th,td
{
height: 50px;
}
td
{
text-align:center;
vertical-align: middle;
}
.button
{
width:75px;
height: 25px;
background-color:dodgerblue;
color:white;
border:1px solid transparent;
}
</style>
<center>
<script>
function deleteABC()
{
}
function editABC()
{
}
</script>
</head>
<body>
<h4 align="right">Logout</h4>
<body>
<div id="wrapper">
<div id="header-wrapper">
<div id="header">
<div id="logo">
<h1>..</h1>
</div>
</div>
<div id="menu-wrapper">
<ul id="menu">
<li class="current_page_item"><span>Homepage</span></li>
<li><span>Blog</span>
<ul>
<li class="first"> About US </li>
<li> Function Area </li>
<li class="last"> Contact US </li>
</ul>
</li>
<li><span>Photos</span></li>
<li><span>About</span></li>
<li><span>Datas</span>
<ul>
<li class="first"> Add Details </li>
<li> Map view </li>
<li class="last"> view Details </li>
</ul>
</li>
<li><span>Contact</span></li>
</ul>
<script type="text/javascript">
$('#menu').dropotron();
</script>
</div></br>
<h1><font color="white">Enter the dates to retrieve the data</font></h1></br></br>
<form method="POST" action="fetchinghome.php">
<label>Role ID:</label><input type="text" name="role" placeholder="Enter the starting date">
<input type="submit" id="submit" name="submit" value="Go" class="button">
</form>
<?php
//include "loginpage.php";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(isset($_POST['submit']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "calender";
$role=$_REQUEST['role'];
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
if ($role == 'admin')
{
$sql="SELECT * FROM registration";
}
elseif($role=="M%")
{
$sql="SELECT * FROM registration where reporting_manager='$role' or role='$role'";
}
else
{
$sql="SELECT * FROM registration WHERE role='$role'";
}
// $sql="SELECT * FROM registration where reporting_manager='$role'";
$retval = mysqli_query( $conn,$sql );
if(! $retval )
{
die('Could not get data: ' . mysqli_error($conn));
}
$arr;
$i=0;
while($row = mysqli_fetch_assoc($retval))
{
$arr[$i]=$row;
$i++;
/*
echo "<tr><td>";
echo $row[0]."</td><td>";
echo $row[1]."</td><td>";
echo $row[2]."</td><td>";
echo $row[3]."</td><td>";
echo $row[4]."</td>";
echo "</tr>";
*/
}
$str='';
for($i=0;$i<count($arr);$i++)
{
$brn='<input type="button" value="Delete" onClick="deleteABC('.$arr[$i]['email_id'].')">';
$brn1='<input type="button" value="Edit" onClick="editABC('.$arr[$i]['email_id'].')">';
$str=$str . '<tr><td>'. $arr[$i]['name'].'</td><td>'.$arr[$i]['email_id'].'</td><td>'.$arr[$i]['mobile_no'].'</td><td>'.$arr[$i]['address'].'</td><td>'.$arr[$i]['role'].'</td><td>'.$brn.'</td><td>'.$brn1.'</td></tr>';
}
echo "<table id='example' class='display' cellspacing='0' width='100%'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Mobile Number</th>
<th>Address</th>
<th>Role</th>
<th>Delete</th>
<th>Edit</th>
</tr>".$str."</table>";
}
echo "Fetched data successfully\n";
mysqli_close($conn);
}
?>
</body>
</html>
Please help me to write the Ajax function for those.
Thanks in advance
function deleteABC(email_id)
{
if(confirm("Are you sure you want to delete..!"))
{
$.ajax
({
url: "your url here",
type: 'POST',
data: {email_id: email_id},
success: function (data)
{
//your success code if deleted..
}
});
}
}
function editABC(email_id)
{
$.ajax
({
url: "your url here",
type: 'POST',
data: {email_id: email_id},
success: function (data)
{
//your success code if edited
}
});
}

How to display data from mysql using angular.js PHP?

How to print data recieved from mysql using angular.js PHP.
I want to know how to connect with mysql and angular.js
I tried with node.js mongodb. but wants to know about php,mysql,angular.js
Hi Can you check following sample code
your HTMLpage :
<html ng-app>
<head>
<title>AngularJs Post Example: DevZone.co.in </title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<style>
#dv1{
border:1px solid #DBDCE9; margin-left:auto;
margin-right:auto;width:220px;
border-radius:7px;padding: 25px;
}
.info{
border: 1px solid;margin: 10px 0px;
padding:10px;color: #00529B;
background-color: #BDE5F8;list-style: none;
}
.err{
border: 1px solid; margin: 10px 0px;
padding:10px; color: #D8000C;
background-color: #FFBABA; list-style: none;
}
</style>
</head>
<body>
<div id='dv1'>
<form ng-controller="FrmController">
<ul>
<li class="err" ng-repeat="error in errors"> {{ error}} </li>
</ul>
<ul>
<li class="info" ng-repeat="msg in msgs"> {{ msg}} </li>
</ul>
<h2>Sigup Form</h2>
<div>
<label>Name</label>
<input type="text" ng-model="username" placeholder="User Name" style='margin-left: 22px;'>
</div>
<div>
<label>Email</label>
<input type="text" ng-model="useremail" placeholder="Email" style='margin-left: 22px;'>
</div>
<div>
<label>Password</label>
<input type="password" ng-model="userpassword" placeholder="Password">
</div>
<button ng-click='SignUp();' style='margin-left: 63px;margin-top:10px'>SignUp</button>
</form>
</div>
<script type="text/javascript">
function FrmController($scope, $http) {
$scope.errors = [];
$scope.msgs = [];
$scope.SignUp = function() {
$scope.errors.splice(0, $scope.errors.length); // remove all error messages
$scope.msgs.splice(0, $scope.msgs.length);
$http.post('post_es.php', {'uname': $scope.username, 'pswd': $scope.userpassword, 'email': $scope.useremail}
).success(function(data, status, headers, config) {
if (data.msg != '')
{
$scope.msgs.push(data.msg);
}
else
{
$scope.errors.push(data.error);
}
}).error(function(data, status) { // called asynchronously if an error occurs
// or server returns response with an error status.
$scope.errors.push(status);
});
}
}
</script>
<a href='http://devzone.co.in'>Devzone.co.in</a>
</body>
</html>
////////////////////////////////////////////////////////////////////////
Your php code
<?php
$data = json_decode(file_get_contents("php://input"));
$usrname = mysql_real_escape_string($data->uname);
$upswd = mysql_real_escape_string($data->pswd);
$uemail = mysql_real_escape_string($data->email);
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $con);
$qry_em = 'select count(*) as cnt from users where email ="' . $uemail . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);
if ($res['cnt'] == 0) {
$qry = 'INSERT INTO users (name,pass,email) values ("' . $usrname . '","' . $upswd . '","' . $uemail . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
$arr = array('msg' => "User Created Successfully!!!", 'error' => '');
$jsn = json_encode($arr);
print_r($jsn);
} else {
$arr = array('msg' => "", 'error' => 'Error In inserting record');
$jsn = json_encode($arr);
print_r($jsn);
}
} else {
$arr = array('msg' => "", 'error' => 'User Already exists with same email');
$jsn = json_encode($arr);
print_r($jsn);
}
?>
Please checkthe above code you will be understand how it will work. If any problem you are facing just tell me here in comment line

Javascript drag and drop div styling

So I'm trying to make a "Simple" php + javascript drag and drop, here's my code:
<title>Web Editor</title>
<?php
mysql_connect("$host","$user","$pass");
mysql_select_db("$db");
if(isset($_POST['submit'])){
$name=$_FILES['file']['name'];
$temp=$_FILES['file']['tmp_name'];
move_uploaded_file($temp,"uploaded/".$name);
$url="http://www.bluejayke.com/edit/uploaded/$name";
}
?>
<?php
if(isset($_POST['submit'])){
mysql_query("INSERT INTO uploadedvideos(id,name,url) VALUES('','$name','$url')");
echo "</br>" . $name . " uploaded";
}
$query="SELECT * FROM uploadedvideos";
$result = mysql_query($query);
?>
<div class='wrapper' id='wrapper'>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td style="width:400px; height:50px;"><form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="Upload">
</form></td>
</tr>
<tr>
<td style="width:400px; height:500px;">
<div style="width:100%; height:100%; overflow:auto; ">
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
</script>
<?php
while($row=mysql_fetch_array($result))
{
$id=$row['id'];
$name=$row['name'];
$url=$row['url'];
echo "<div style='position:absolute;cursor:pointer;' id='".$id."'><a href='javascript: click(\"$url\")'>$name</br><embed id='$id' src='$url' width='120' height='120' draggable='true' ondragstart='drag(event)'></embed></a><input type='button' id='delete' value='X' onclick='return Deleteqry($id)' /></div><br> ";
?>
<script type="text/javascript">
var dragging = false
$(function () {
var target<?php echo $id ?> = $('#<?php echo $id; ?>')
target<?php echo $id ?>.mousedown(function() { dragging<?php echo $id; ?> = true })
$(document).mouseup(function() { dragging<?php echo $id; ?> = false })
$(document).mousemove(function(e) {
if(dragging<?php echo $id; ?>){
target<?php echo $id ?>.css('left', e.pageX)
target<?php echo $id ?>.css('top', e.pageY)
}
})
})
</script>
<?php
}
?>
</div>
</table>
</div>
<div class='wactch' id='watching' height='480' width='720'>Loading..</div>
<script>
/*document.onmousemove=mouseMove;
function mouseMove(ev)
{
ev = ev || window.event;
var mousePos=mouseCords(ev);
}
function mouseCords(ev)
{
if(ev.pageX || ev.pageY)
{
return{x:ev.pageX,y:ev.pageY);
}
return
{
x:ev.clientX+document.body.scrollLeft-document.body.clientLeft, y:ev.clientY.document.body.scrollTop-document.body.clientTop
};
}
}*/
function Deleteqry(id)
{
if(confirm("Are you sure you want to delete this picture?")==true)
window.location="index.php?del="+id;
return false;
}
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
window.onload=function()
{
click("http://www.bluejayke.com/edit/uploaded/dpnewslogo.png");
}
function click(url)
{
document.getElementById("watching").innerHTML='<embed src="'+url+'" width=720 height=480></embed>';
}
</script>
<?php
if($_GET['del']){
$delete_id=$_GET['del'];
mysql_query("DELETE FROM `uploadedvideos` WHERE `uploadedvideos`.`id` = $delete_id");
header("location: index.php");
}
?>
<style type="text/css">
.wrapper
{
width:500px;
overflow: hidden;
float: left;
}
#watching {
float: right;
}
.div1 {width:750px;height:120px;padding:10px;border:1px solid #aaaaaa; oveflow: hidden;}
</style>
The important part is in the while loop. The result of this code is all the divs I made in the while loop are piled up on top of each other, when I want them in a vertical line-like structure. Anyone have any insight on this? It's something with the div's styling I think, but I'm not sure how to resolve it while still keeping the drag and drop function.
Your divs are being positioned absolute. You simply need to change them to position:relative;
http://www.w3schools.com/css/css_positioning.asp

Categories