I'm working on a real basic chat that is mostly php powered. I'd like to use jQuery to check for new messages and update the #cbox div every few seconds. So far I have tried accomplishing that using the code below. For a frame of reference, this is my first time including jQuery into any of my scripts, so the problem is likely a very simple error that I am overlooking in my newbishness.
The problem I'm running into is the #cbox div displays blank, it doesn't seem to be calling to chat_post.php at all. I will post that code as well, in case the problem is with that and not with my jQuery. To fix, I tried also including <div id='cbox'> in chat_post.php, but that yielded no results. At this point, I'm pretty much grasping at straws.
<div id='sb_content'>
<center><div id='chat'>
<div id='ribbon' style='position: relative; margin-top:-50px; margin-left:-32px;'><center><span class='ribbon'>chat box</span></center></div>
<div style='margin-top: -20px;'><center><span style='text-shadow: 0 0 0.2em #000;' class='admin'>admin</span> || <span style='text-shadow: 0 0 0.2em #000;' class='mod'>mod</span></center></div>
<div id="cbox">
<script>
$(document).ready(function() {
setInterval(function(){getUpdates()}, 2000);
});
function getUpdates() {
$("#cbox").load("chat_post.php");
}
</script>
</div>
<form name="message" method='post' action="chat_post.php" id="cbox_input">
<input name="usermsg"id='usermsg' type="text" size="63" maxlength="255" />
</form>
</div></center>
</div>
chat_post.php
<div id='cbox' align='left'>
<?php
$username = $_SESSION['login'];
$ug = $_SESSION['user_group'];
// Display messages
$sql = <<<SQL
SELECT *
FROM `chat_entries`
ORDER BY `ts` DESC
LIMIT 0,50
SQL;
$result = $db->query($sql);
$count = $result->num_rows;
if ($count != 0){
while ($row = mysqli_fetch_array($result)) {
$c_name = $row['author'];
$c_text = $row['text'];
$c_ts = $row['ts'];
$c_id = $row['id'];
$sqlm = <<<SQL
SELECT *
FROM `users`
WHERE username = '$c_name'
SQL;
$resultm = $db->query($sqlm);
$countm = $resultm->num_rows;
if ($count != 0){
while ($rowm = mysqli_fetch_array($resultm)) {
$c_level = $rowm['user_group'];
}
}
if ($c_level == 'mod') {
echo "
<a href='/user/" . $c_name . "' class='mod'>" . $c_name . "</a>
";
}
if ($c_level == 'admin') {
echo "
<a href='/user/" . $c_name . "' class='admin'>" . $c_name . "</a>
";
}
if ($c_level == 'member') {
echo "
<a href='/user/" . $c_name . "' class='member'>" . $c_name . "</a>
";
}
if ($c_level == 'guest') {
echo "
<i>" . $c_name . "</i>
";
}
echo "
: " . $c_text . "<div id='cbox_div'>";
echo "<span style='float:left;'><i>" . $c_ts . "</i></span>";
echo "<span style='float:right;'>";
if ($ug == 'mod' || $ug == 'admin'){
echo " <a href='#'>Delete</a> || <a href='#'>Block</a> ||";
}
if ($_SESSION['login']) {
echo "Report</span></div>
";
}
}
}
?>
After removing the extra id on chat_post.php, my code worked just fine. Funny how something so simple can really mess things up. (: Thank you everyone for your help!
Related
Please you help? I run a query via search form and output results, although, it is not displaying as it should? Still new to php so please excuse if my code is....
the result needed. If te search returns I want one of 2 things. To either show Employed by: xxx or Not currently employed:
<?php
if ($_REQUEST['submit']) {
$term = $_POST['term'];
$row ['employerid'] == $user_data ['user_id'];
$XX = "<br><br><div class='messagebox'><h2> <center> Oops! </h2> <p>We were only to retrieve a partial record on <strong>$term</strong> you have entered. Please make use of our contact form if you would like us to get you your reference. Be sure to enter the three required fields. <a href='Mailforms/refrequest.php' class='lightbox'>Click Here!</a> or to validate the id <a href='idverification.php'> Click here</a></p>
<br />
</div>";
$sql = mysql_query("select * from ref_employees where `idnumber`= '$term'")
or die('Error in query : $sql. ' .mysql_error());
{
if (mysql_num_rows($sql) > 0 )
{
while ($row = mysql_fetch_array($sql)){
if ($row ['employed'] == '1') {
echo '<h4>Currently Employed By : '.$row['companyname'];
echo '</h4> ';}
if ($row ['employed'] == '0') {
echo '<h4>Not Currently employed : '.$row['companyname'];
echo '</h4> ';}
echo '<h4> ID : '.$row['idnumber'] ;
echo '<br> First Name : '.$row['firstname'];
echo '<br> Last Name : '.$row['lastname'];
echo '<br> Gender : '.$row['gender'];
echo ' </h4>';
echo '<br />';
echo '<h2>Some Additional Options</h2>';
echo '<br />';
}
}
else
{
print ("$XX");
}
mysql_free_result($sql);
mysql_close($connection);
}
You have put an extra opening curly brace { above if (mysql_num_rows($sql) > 0 ) {
Corrected Code:
<?php
if ($_REQUEST['submit']) {
$term = $_POST['term'];
$row ['employerid'] == $user_data ['user_id'];
$XX = "<br><br><div class='messagebox'><h2> <center> Oops! </h2> <p>We were only to retrieve a partial record on <strong>$term</strong> you have entered. Please make use of our contact form if you would like us to get you your reference. Be sure to enter the three required fields. <a href='Mailforms/refrequest.php' class='lightbox'>Click Here!</a> or to validate the id <a href='idverification.php'> Click here</a></p>
<br />
</div>";
$sql = mysql_query("select * from ref_employees where `idnumber`= '$term'")
or die('Error in query : $sql. ' .mysql_error());
if (mysql_num_rows($sql) > 0 ) {
while ($row = mysql_fetch_array($sql)){
if ($row ['employed'] == '1') {
echo '<h4>Currently Employed By : '.$row['companyname'];
echo '</h4> ';}
if ($row ['employed'] == '0') {
echo '<h4>Not Currently employed : '.$row['companyname'];
echo '</h4> ';}
echo '<h4> ID : '.$row['idnumber'] ;
echo '<br> First Name : '.$row['firstname'];
echo '<br> Last Name : '.$row['lastname'];
echo '<br> Gender : '.$row['gender'];
echo ' </h4>';
echo '<br />';
echo '<h2>Some Additional Options</h2>';
echo '<br />';
}
}
else
{
print ("$XX");
}
mysql_free_result($sql);
mysql_close($connection);
}
I am having a problem with pagination within the tabs. In the second tab (candidate), when I press pagination 2 to navigate to the second page of the candidate table, I am returned to the first page of the contact table. If I go to the candidate tab after that I am on the right page. Where have I gone wrong?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"> </script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/start/jquery-ui.css"/>
<link type ="text/css" rel ="stylesheet" href = "testData.cs"/>
<script>
$(function(){
$('#tabs1,#tabs2').tabs();
});
</script>
<head>
<title>Candidate DB</title>
</head>
<body>
<div id ="tabs1" class ="contactForm">
<ul>
<li>List of Contacts</li>
<li>List of Candidates</li>
<li>Advanced Search</li>
</ul>
<div id ="tab1" class="contact" >
<table border="1" id="contact_info">
<tr>
<th>Contact Name</th>
<th>County</th>
</tr>
<?php
$DB_NAME = "Candidate";
$DB_USER ="root";
$DB_PASSWORD ="";
$DB_HOST ="localhost";
$con=mysql_connect("$DB_HOST", "$DB_USER", "$DB_PASSWORD") or die("Could not connect to MySQL");
mysql_select_db("$DB_NAME") or die ("No Database");
echo "Connected to Candidate Database </br></hr>" ;
$per_page=5;
$pages_query= mysql_query("SELECT COUNT(contact_id) FROM contact");
$pages = ceil(mysql_result($pages_query,0)/$per_page);
$page=(isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start=($page-1) * $per_page;
$sql=mysql_query("SELECT first_name, last_name, county from contact ORDER BY last_name ASC LIMIT $start, $per_page" );
$Fname = 'first_name';
$Lname = 'last_name';
$county = 'county';
while ( $row = mysql_fetch_object($sql)) {
echo "<tr>";
echo "<td>" . $row ->first_name . " " . $row->last_name. "</td>";
echo "<td>" . $row->county . "</td>";
echo "</tr>";
}
// close the loop
if ($pages>=1 && $page<= $pages ) {
for ($x=1; $x<=$pages; $x++)
{
echo ($x ==$page)? '<strong><a style="color:green" href="? page='.$x.'">'.$x. '</a></strong>_':''.$x. '_';
}
}
?>
</table>
</div>
<div id ="tab2" class="candidate" >
<table border="1" id="candidate_info">
<tr>
<th>Candidate Name</th>
<th>County</th>
</tr>
<?php
$per_pageR=5;
$pages_queryR= mysql_query("SELECT COUNT(candidate_id) FROM p_candidate");
$pagesR = ceil(mysql_result($pages_queryR,0)/$per_pageR);
$pageR=(isset($_GET['pageR'])) ? (int)$_GET['pageR'] : 1;
$startR=($pageR-1) * $per_pageR;
$sql2=mysql_query("SELECT R_first_name, R_last_name, R_county from p_candidate ORDER BY R_last_name ASC LIMIT $startR, $per_pageR" );
$R_name = 'R_first_name';
$R_name = 'R_last_name';
$R_county = 'R_county';
while ( $rowR = mysql_fetch_object($sql2)) {
echo "<tr>";
echo "<td>" . $rowR ->R_first_name . " " . $rowR->R_last_name. "</td>";
echo "<td>" . $rowR->R_county . "</td>";
echo "</tr>";
}
// close the loop
if ($pagesR>=1 && $pageR<= $pagesR ) {
for ($y=1; $y<=$pagesR; $y++)
{
echo ($y ==$pageR)? '<strong><a style="color:green" href="? pageR='.$y.'">'.$y. '</a></strong>_':''.$y. '_';
}
}
?>
</table>
</div>
</div>
</body>
If I understand correctly, try using the active option - http://api.jqueryui.com/tabs/#option-active - when you do .tabs() to make the candidate tab the active tab.
<script>
$(function(){
$('#tabs1,#tabs2').tabs(<?php if(isset($_GET['pageR'])) echo "{ active: 1 }"; ?>);
});
</script>
or
<script>
$(function(){
$('#tabs1,#tabs2').tabs(<?php if(isset($_GET['pageR'])) echo '"option", "active", -1'; ?>);
});
</script>
edit
You could bind the .tabs() first, and then set the active option. This should hopefully fix your formatting issue.
<script>
$(function(){
$('#tabs1,#tabs2').tabs();
<?php if(isset($_GET['pageR'])) { ?>
$('#tabs1').tabs("option", "active", -1)
<?php } ?>
});
</script>
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I need to be able to put part of a site i had built previously into another site im making now. However, both are in php and there are issues with the syntax (ie too many subsets of " and '). How can I put this code:
if(mysql_num_rows($result) == 0)
{
echo 'No categories defined yet.';
}
else
{
//prepare the table
echo '<table border="1">
<tr>
<th>Category</th>
<th>Last topic</th>
</tr>';
while($row = mysql_fetch_assoc($result))
{
echo '<tr>';
echo '<td class="leftpart">';
echo '<h3>' . $row['cat_name'] . '</h3>' . $row['cat_description'];
echo '</td>';
echo '<td class="rightpart">';
//fetch last topic for each cat
$topicsql = "SELECT
topic_id,
topic_subject,
topic_date,
topic_cat
FROM
topics
WHERE
topic_cat = " . $row['cat_id'] . "
ORDER BY
topic_date
DESC
LIMIT
1";
$topicsresult = mysql_query($topicsql);
if(!$topicsresult)
{
echo 'Last topic could not be displayed.';
}
else
{
if(mysql_num_rows($topicsresult) == 0)
{
echo 'no topics';
}
else
{
while($topicrow = mysql_fetch_assoc($topicsresult))
echo '' . $topicrow['topic_subject'] . ' at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
}
}
echo '</td>';
echo '</tr>';
}
}
into this code (i want the table itself to be inside the div main and under the welcome)
echo
"<div id=holder>
<div id='nav'>
<a href='chat.php'>Chat with your friends!</a>
<br><br><a href='changepassword.php'>Change password</a><br><br>
<a href='logout.php'>Logout</a>
</div>
<div id='main'>
Welcome, ".$_SESSION['user_name']."!
</div>
</div>";
//fetch last topic for each cat
$topicsql = "SELECT
topic_id,
topic_subject,
topic_date,
topic_cat
FROM
topics
WHERE
topic_cat = " . $row['cat_id'] . "
ORDER BY
topic_date
DESC
LIMIT
1";
$topicsresult = mysql_query($topicsql);
if(!$topicsresult)
{
echo 'Last topic could not be displayed.';
}
else
{
if(mysql_num_rows($topicsresult) == 0)
{
echo 'no topics';
}
else
{
while($topicrow = mysql_fetch_assoc($topicsresult))
echo '' . $topicrow['topic_subject'] . ' at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
}
}
echo '</td>';
echo '</tr>';
}
}
};
I recognize that this is pretty complicated, but I am so lost in the syntax at this point, so if someone could help me a little bit on this itd be very appreciated
Assuming that you can have both files present on the same location you can include the file with the table inside the file with the "welcome" message:
echo "
<div id=holder>
<div id='nav'>
<a href='chat.php'>Chat with your friends!</a>
<br><br>
<a href='changepassword.php'>Change password</a>
<br><br>
<a href='logout.php'>Logout</a>
</div>
<div id='main'>
Welcome, ".$_SESSION['user_name']."!";
include("path_to_the_table_file.php");
echo "
</div>
</div>";
...
Link to documentation: The include statement includes and evaluates the specified file.
Did you know? You don't have to mess with complicated echo syntax. This will work too:
<div id=holder>
<div id='nav'>
<a href='chat.php'>Chat with your friends!</a>
<br><br><a href='changepassword.php'>Change password</a><br><br>
<a href='logout.php'>Logout</a>
</div>
<div id='main'>
Welcome, <?php echo $_SESSION['user_name'] ?>!
<?php include("yourotherfile.php"); ?>
</div>
</div>
<?php
//fetch last topic for each cat
$topicsql = "SELECT
topic_id,
topic_subject,
topic_date,
topic_cat
FROM
topics
WHERE
topic_cat = " . $row['cat_id'] . "
ORDER BY
topic_date
DESC
LIMIT
1";
$topicsresult = mysql_query($topicsql);
if(!$topicsresult)
{
echo 'Last topic could not be displayed.';
}
else
{
if(mysql_num_rows($topicsresult) == 0)
{
echo 'no topics';
}
else
{
while($topicrow = mysql_fetch_assoc($topicsresult))
echo '' . $topicrow['topic_subject'] . ' at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
}
}
echo '</td>';
echo '</tr>';
}
}
};
?>
I've a search form in which a user enters the keyword and the results are displayed with pagination. everything works fine except for the fact that when the user clicks on the 'Next' button, the pagination panel disappears as well when the page loads to retrieve the data through ajax.
How do I make the pagination panel static, while the data is being retrieved?
search.html:
<form name="myform" class="wrapper">
<input type="text" name="q" id="q" onkeyup="showPage();" class="txt_search"/>
<input type="button" name="button" onclick="showPage();" class="button"/>
<p> </p>
<div id="txtHint"></div>
</form>
ajax:
var url="search.php";
url += "?q="+str+"&page="+page+"&list=";
url += "&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
} //end if
} //end function
search.php:
$self = $_SERVER['PHP_SELF'];
$limit = 3; //Number of results per page
$adjacents = 2;
$numpages=ceil($totalrows/$limit);
$query = $query." ORDER BY idQuotes LIMIT " . ($page-1)*$limit . ",$limit";
$result = mysql_query($query, $conn)
or die('Error:' .mysql_error());
?>
<div class="search_caption">Search Results</div>
<div class="search_div">
<table class="result">
<?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
$cQuote = highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
?>
<tr>
. . .display results. . .
</tr>
<?php } ?>
</table>
</div>
<hr>
<div class="searchmain">
<?php
//Create and print the Navigation bar
$nav="";
$next = $page+1;
$prev = $page-1;
if($page > 1) {
$nav .= "<a onclick=\"showPage('','$prev'); return false;\" href=\"$self?page=" . $prev . "&q=" .urlencode($search_result) . "\">< Prev</a>";
$first = "<a onclick=\"showPage('','1'); return false;\" href=\"$self?page=1&q=" .urlencode($search_result) . "\"> << </a>" ;
}
else {
$nav .= " ";
$first = " ";
}
for($i = 1 ; $i <= $numpages ; $i++) {
if($i == $page) {
$nav .= "<span class=\"no_link\">$i</span>";
}else{
$nav .= "<a onclick=\"showPage('',$i); return false;\" href=\"$self?page=" . $i . "&q=" .urlencode($search_result) . "\">$i</a>";
}
}
if($page < $numpages) {
$nav .= "<a onclick=\"showPage('','$next'); return false;\" href=\"$self?page=" . $next . "&q=" .urlencode($search_result) . "\">Next ></a>";
$last = "<a onclick=\"showPage('','$numpages'); return false;\" href=\"$self?page=$numpages&q=" .urlencode($search_result) . "\"> >> </a>";
}
else {
$nav .= " ";
$last = " ";
}
echo $first . $nav . $last;
?>
</div>
Not sure what you mean. Just change the result table not the whole page in showPage function.
I seem to be having a problem inserting a <div> element in the correct area. I want to push a button, in this case "more info" and display more information under the first div area using jquery.
example:
<div id="Main-info">
<div id="more-info">
</div>
</div>
The problem I'm having is the "Main-info" area is calling a php script and grabbing info from the database and displaying it in a table. The second php file I have loads in the "more-info" div so when I push the more info button the second php files goes under the entire first div but i want it to go under the specific information that I click.
A better example of what I am trying to accomplish is like the wefollow.com site, where you push the more info button and more info display right under the info you click. In my case the additional info goes under the entire table at the buttom.
Here is the code:
index.php:
<?php
include("buy.functions.php");
?>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="listing.js"></script>
<div id="article-area">
<h1>Welcome</h1>
<div id="output-listings">
<div id="more-information">
</div>
<?php outputListingsTable(); ?>
</div><!--end output-listings-->
buy.functions.php:
<?php
function outputListingsTable()
{
$mysql = new mysqli('localhost', 'root', 'root', 'ajax_demo') or die('you\'re dead');
$result = $mysql->query("SELECT * FROM explore") or die($mysql->error);
if($result)
{
echo "<table> \n";
while($row = $result->fetch_object())
{
$id = $row->id;
$siteName = $row->site_name;
$siteDescription = $row->site_description;
$siteURL = $row->site_url;
$sitePrice = $row->site_price;
echo "<div id=\"" . $id . "\"> \n";
echo " <tr> \n";
echo " <td>" . $siteName . "</td> \n";
echo " <td>" . $siteURL . "</td> \n";
echo " <td><a id=\"" . $id . "\" class=\"more-info\" href=\"#\">More info</a></td> \n";
echo " </tr> \n";
echo "</div> \n";
}
echo "</table> \n";
}
}
?>
getinfo.php:
<?php
function outputDescriptionTable($id)
{
$mysql = new mysqli('localhost', 'root', 'root', 'ajax_demo') or die('you\'re dead');
$result = $mysql->query("SELECT * FROM explore WHERE id=" . $id) or die($mysql->error);
if($result)
{
echo "<table> \n";
while($row = $result->fetch_object())
{
$siteName = $row->site_name;
$siteDescription = $row->site_description;
$siteURL = $row->site_url;
$sitePrice = $row->site_price;
echo "<div id=\"more-information\"> \n";
echo " <tr> \n";
echo " <td>" . $siteDescription . "</td> \n";
echo " <td>" . $sitePrice . "</td> \n";
echo " </tr> \n";
echo "</div> \n";
}
echo "</table> \n";
}
}
?>
<?php $id = $_GET['id']; ?>
<?php outputDescriptionTable("$id"); ?>
listing.js:
$(function(){
$("tr").hover(function(){
$(this).addClass("hover");
}, function() {
$(this).removeClass("hover");
});
$('.more-info').click(function() {
$('#more-information').show().load('getinfo.php?id=' + $(this).attr('id'));
return false;
});
});
jQuery's closest should solve your problem. Something like:
$('.more-info').click(function(e) {
$(e.target).closest('.more-information').show().load('getinfo.php?id=' + $(this).attr('id'));
return false;
});
From the doc:
Closest works by first looking at the
current element to see if it matches
the specified expression, if so it
just returns the element itself. If it
doesn't match then it will continue to
traverse up the document, parent by
parent, until an element is found that
matches the specified expression. If
no matching element is found then none
will be returned.
Also, your 'more-information' divs should have the class more-information, as opposed to the ID, as IDs should be unique, else strange things might happen.