Jump to content

AUTO REFRESH problem


kleb

Recommended Posts

please this code is not working,i want when users comment on a page it should refresh this is my code:

			<h3>Comments:</h3>

				<table>
				<tr class="row-a">
					<td class="first"></td>
					<td><?php
                                 include"header.php";
					       
                                  $sql="SELECT post_content,post_by FROM post ORDER BY topicsID";
							  
                            $result=mysql_query($sql)or die(mysql_error());
                                while($row=mysql_fetch_array($result))
                             {

                               echo"<strong>{$row['post_by']}</strong>:  {$row['post_content']}"."</br>";
                             }
                           if(mysql_affected_rows()>0)
					   {
					   header("location:".$_SERVER['PHP_SELF']);
					   }
                                         ?></td>

				</tr>
				</table>

			<h3>Post your comments here</h3>
			<form action='reply.php'method='post'>
				<textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea>
				<br />	
				Name:<input type="text"name="name"/> 		

				<input class="button" type="submit"name="submit"value="submit" />
				</p>		
			</form>	

			<br />				
			[code]

Link to comment
Share on other sites

Try the following (change the query to match your table and values

<h3>Comments:</h3>

<table>
<tr class="row-a">
<td class="first"></td>
<td>
<?php
include"header.php";

if($_POST['submit'])
{
                $post = $_POST["comment"];
                $by = $_POST["name"];
	$add = "INSERT INTO `post` values(`post_content`,`post_by`,) ('$post','$by')";
	$add_values = mysql_query($add) or die(mysql_error());
}

$sql="SELECT post_content,post_by FROM post ORDER BY topicsID";

$result=mysql_query($sql)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
	echo"<strong>{$row['post_by']}</strong>:  {$row['post_content']}"."</br>";
}


?>
</td>

</tr>
</table>

<h3>Post your comments here</h3>
<form action='<?php $_SERVER['PHP_SELF']; ?>' method='post'>
<textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea>
<br />	
Name:<input type="text"name="name"/> 		

<input class="button" type="submit"name="submit"value="submit" />
</p>		
</form>	

I hope this helps

Link to comment
Share on other sites

thanks so much SOLON but it gave me error concerning my id(it says index id undefined)i retrieved the id from the page that contains the topic although it worked fine before but since i changed the <form action> to <form action='$_SERVER['PHP_SELF']> it gave the error.please what can i do?

thanks :(

Link to comment
Share on other sites

Try it now (there was a syntax error in the query):

<h3>Comments:</h3>

<table>
<tr class="row-a">
<td class="first"></td>
<td>
<?php
include"header.php";

if($_POST['submit'])
{
                $post = $_POST["comment"];
                $by = $_POST["name"];
	$add = "INSERT INTO `post` values(`post_content`,`post_by`) ('$post','$by')";
	$add_values = mysql_query($add) or die(mysql_error());
}

$sql="SELECT post_content,post_by FROM post ORDER BY topicsID";

$result=mysql_query($sql)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
	echo"<strong>{$row['post_by']}</strong>:  {$row['post_content']}"."</br>";
}


?>
</td>

</tr>
</table>

<h3>Post your comments here</h3>
<form action='<?php $_SERVER['PHP_SELF']; ?>' method='post'>
<textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea>
<br />	
Name:<input type="text"name="name"/> 		

<input class="button" type="submit"name="submit"value="submit" />
</p>		
</form>	

It should work now

Link to comment
Share on other sites

You dont get me solon..let me post the full code to you:

				<span class="date"><?php 
			include"header.php";
			$topicid=$_GET['id'];
			$sql="SELECT*FROM topics WHERE topicsID=$topicid";
			$result=mysql_query($sql) or die(mysql_error());
			while($row=mysql_fetch_array($result))
			     {
                 	
			echo"{$row['topics_date']}";
			}
			?></span>	
			</p>

		</div>


			<h3>Comments:</h3>

				<table>
				<tr class="row-a">
					<td class="first"></td>
					<td>

					<?php
                       include"header.php";
                       if(isset($_POST['submit']))
                       {
                        $comment=mysql_real_escape_string(trim($_POST['comment']));
                        $name=mysql_real_escape_string(trim($_POST['name']));
                        if($comment!=='' && $name!=='')
                        {
                        $ins="INSERT INTO post(post_content,post_by)VALUES('$comment','$name')";
                        mysql_query($ins) or die(mysql_error());
                         $sql="SELECT post_content,post_by FROM post ORDER BY topicsID";  
                            $result=mysql_query($sql)or die(mysql_error());
                                while($row=mysql_fetch_array($result))
                             {

                               echo"<strong>{$row['post_by']}</strong>:  {$row['post_content']}"."</br>";
                             }
                               }
						   }
                                       ?></td>

				</tr>
				</table>

			<h3>Post your comments here</h3>
			<form action='<?php $_SERVER['PHP_SELF'];?>''method='post'>
				<textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea>
				<br />	
				Name:<input type="text"name="name"/> 		

				<input class="button" type="submit"name="submit"value="submit" />
				</p>		
			</form>	

			<br />	

Link to comment
Share on other sites

The error "Undefined index id" is referring to the $_GET['id'] variable. since you don't sent anything using the GET method it comes up us undefined.

You can edit that and line and use the following:

 


				<span class="date"><?php 
			include"header.php";
			if(isset($_GET['id']) //CHECK IF GET ID IS SET ELSE GET IT FROM HIDDEN FIELD IN THE FORM
			{
				$topicid=$_GET['id'];
			}
			else
			{
				$topicid=$_POST['id'];
			}
			$sql="SELECT*FROM topics WHERE topicsID=$topicid";
			$result=mysql_query($sql) or die(mysql_error());
			while($row=mysql_fetch_array($result))
			     {
					echo"{$row['topics_date']}";
				}
			?></span>	
			</p>

		</div>


			<h3>Comments:</h3>

				<table>
				<tr class="row-a">
					<td class="first"></td>
					<td>

					<?php
                       include"header.php";
                       if(isset($_POST['submit']))
                       {
                        $comment=mysql_real_escape_string(trim($_POST['comment']));
                        $name=mysql_real_escape_string(trim($_POST['name']));
                        if($comment!=='' && $name!=='')
                        {
                        $ins="INSERT INTO post(post_content,post_by)VALUES('$comment','$name')";
                        mysql_query($ins) or die(mysql_error());
                         $sql="SELECT post_content,post_by FROM post ORDER BY topicsID";  
                            $result=mysql_query($sql)or die(mysql_error());
                                while($row=mysql_fetch_array($result))
                             {

                               echo"<strong>{$row['post_by']}</strong>:  {$row['post_content']}"."</br>";
                             }
                               }
						   }
                                       ?></td>

				</tr>
				</table>

			<h3>Post your comments here</h3>
			<form action='<?php $_SERVER['PHP_SELF'];?>' method='post'>
				<textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea>
				<br />	
				Name:<input type="text"name="name"/> 		

				<input class="button" type="submit"name="submit"value="submit" />
                    <input type="hidden" name="id" value="<?php echo $topicid; ?>" /> //THIS LINE IS A HIDDEN FIELD SAVING THE TOPIC ID FOR USE AFTER THE FORM IS SUBMITED 
				</p>		
			</form>	

			<br />	

 

Link to comment
Share on other sites

Thanks it works fine but i discover now that every comment in my POST table get echoed out so i introduced a WHERE clause in the SELECT query but it will only echo out only one comment even though i comment again and again.this is the new code:

<p>Posted by <a href=""><?php 
			include"header.php";
			if(isset($_GET['id']))
			{
			$topicid=$_GET['id'];
			}
			else
			{
			$topicid=$_POST['id'];
			}
			$sql="SELECT*FROM topics WHERE topicsID=$topicid";
			$result=mysql_query($sql) or die(mysql_error());
			while($row=mysql_fetch_array($result))
			     {
                 		echo"{$row['topics_by']}</a></p>";



                 		echo"<p> {$row['topics_subject']}";

					 echo"  <p align='left'><img src='speakout_1.png'/>";
                 	
			echo"{$row['topics_content']}";
			echo"<p class='post-footer align-right'>";					

				echo"<a href='' class='comments'>";
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo"Comments:".$_SESSION['views'];

echo"</a>";
				echo"<span class='date'>";

			echo"{$row['topics_date']}";
			}
			?></span>	
			</p>

		</div>


			<h3>Comments:</h3>

				<table>
				<tr class="row-a">
					<td class="first"></td>
					<td>

					<?php
                       include"header.php";
                         $sql="SELECT post_content,post_by FROM post WHERE postID='$topicid'";  
                            $result=mysql_query($sql)or die(mysql_error());
                                while($row=mysql_fetch_array($result))
                             {

                               echo"<strong>{$row['post_by']}</strong>:  {$row['post_content']}"."</br>";
                             }
						  if(isset($_POST['submit']))
                       {
                        $comment=mysql_real_escape_string(trim($_POST['comment']));
                        $name=mysql_real_escape_string(trim($_POST['name']));

                        if($comment=='' && $name=='')
                        {
					echo"You can not leave any field blank";
					}
					else{
                        $ins="INSERT INTO post(post_content,post_by)VALUES('$comment','$name')";
                        mysql_query($ins) or die(mysql_error());
                               }
						   }
						   
                                       ?></td>

				</tr>
				</table>

			<h3>Post your comments here</h3>
			<form action='<?php $_SERVER['PHP_SELF']; ?>'method='post'>
				<textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea>
				<br />	
				Name:<input type="text"name="name"/> 		
				<input type="hidden"name="id"value="<?php echo $topicid;?>"/>
				<input class="button" type="submit"name="submit"value="submit" />
				</p>	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.