Jump to content

How can I use page1.php to input into the database while displaying the messages from the form on page1.php?


bobleny

Recommended Posts

I’m attempting to make a forum using php and it is going well but the way it is currently set up, not so nice. The user logs on and they can see the messages. When the user types in a message, they are taken to another page where there data is entered to the database. They are then automatically redirected to the page where the messages are displayed. The redirect is near instant, in fact only .3 seconds.

Now that’s great but this means I have a lot of extra files that aren’t really necessary. e.g.

You arrive at page1.php, you enter a message, you are then taken to page2.php, and then returned to page1.php. You then decide to edit that post. So, you click edit and your taken to page3.php, you edit the post, click save, where you are then taken to page4.php and then returned to page1.php.

That’s 4 pages for 2 functions and I know its not necessary. So how do I solve this problem? How can I use page1.php to input into the database while displaying the messages from the form on page1.php?

My forum is [a href=\"http://www.firemelt.net/forum.php\" target=\"_blank\"]http://www.firemelt.net/forum.php[/a] if you need to see how I have it, though I do not have the edit function as I have not created the login yet..

Umm thanks if you can help
Link to comment
Share on other sites

[!--quoteo(post=376847:date=May 24 2006, 08:34 PM:name=Bob Leny)--][div class=\'quotetop\']QUOTE(Bob Leny @ May 24 2006, 08:34 PM) [snapback]376847[/snapback][/div][div class=\'quotemain\'][!--quotec--]
That’s 4 pages for 2 functions and I know its not necessary. So how do I solve this problem? How can I use page1.php to input into the database while displaying the messages from the form on page1.php?
[/quote]
I'm not sure what you mean... you can post back to the same page, process user input to the DB, print a location header to the same page with a status=<whatever> or rc=<some code>, and then show this message/code to the user in a predefined area. What's the problem?
Link to comment
Share on other sites

This is what I’m trying to accomplish but this doesn’t work. And its all on one page.

[!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--]<?php
include("top.php");
$text="Melt Forum";

$username="bobleny_all";
$password="**********";
$database="bobleny_all";

$name = str_replace('<','&lt;',$_POST['name']); $name = str_replace('>','&gt;',$name);
$subject = str_replace('<','&lt;',$_POST['subject']); $subject = str_replace('>','&gt;',$subject);
$message = str_replace('<','&lt;',$_POST['message']); $message = str_replace('>','&gt;',$message);
$time=$_POST['time'];

mysql_connect('db4.awardspace.com:3306',$username,$password);
mysql_select_db($database) or die( "Unable to select database");

$insert = "INSERT INTO forum VALUES ('$name','$subject','$message','$time','')";
$query="SELECT * FROM forum ORDER BY id DESC";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>

<span class="pleft">To post a message, fill in the boxes below.</span>

<form action=".03page1.php" method="post">
Name: <input type="text" name="name" size="11" maxlength="10"><br>
Subject: <input type="text" name="subject" size="21" maxlength="20"><br>
<input type="hidden" name="time" value="<?php echo date("n-j-y g:i A",time()+21600) ?>">
Message: <br>
<textarea name="message" cols="45" rows="3" wrap="soft" maxlength="500"></textarea><br>
<input type="Submit" value="Post">
</form>

<br />

<?php

$i=0;
while ($i < $num) {

$name=mysql_result($result,$i,"name");
$subject=mysql_result($result,$i,"subject");
$message=nl2br(mysql_result($result,$i,"message"));
$time=mysql_result($result,$i,"time");
$id=mysql_result($result,$i,"id");
?>

<table border=0 background=table_declare.png cellpadding=0 cellspacing=0 align=center width=353 height=27>
<tr><td width=353>
<center>Posted by: <?php echo $name ?></center>
</td></tr>
</table>

<table border=0 background=table_top4.png cellpadding=0 cellspacing=0 align=center width=703 height=27>
<tr>
<td>&nbsp;<span class="pleft">Subject: <?php echo $subject ?></span></td>
<td align=right><span class="pleft">Posted on: <?php echo $time ?>&nbsp</span></td>
</tr>
</table>

<table border=0 background=table_back.png cellpadding=0 cellspacing=0 align=center width=703>
<tr><td align=center>

<table border=0 cellpadding=0 cellspacing=0 align=center width=691>
<tr><td width=691>

<?php echo $message ?>

</td></tr>
</table>

</td></tr>
<tr><td><img src=table_bottom.png width=703 height=5></td></tr>
</table>

<!------------------------------------------------------------------------>
<br /><br />
<!------------------------------------------------------------------------>

<?php

$i++;
}

include("bot.php");
?>[!--sizec--][/span][!--/sizec--]
Link to comment
Share on other sites

You're doing the DB work all the time, not based on the POST itself; also, there's no status message. Incidentally, you're still adding the time via PHP/hidden fields -- I think I mentioned in another thread that you should be using MySQL's NOW() function instead.
Link to comment
Share on other sites

Ok, I got it to so what I want it to do finally! And this is what I wanted below... But! theres a problem, when you refresh the page the post will re submit its self and thats not good! so how do I keep it from reposting when you refresh the page?

[!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--]
<?php
session_start();
header("Cache-control: private");

include("top.php");
$text="Database Test";

$username="bobleny_all";
$password="********";
$database="bobleny_all";

$name = str_replace('<','&lt;',$_POST['name']); $name = str_replace('>','&gt;',$name);
$subject = str_replace('<','&lt;',$_POST['subject']); $subject = str_replace('>','&gt;',$subject);
$message = str_replace('<','&lt;',$_POST['message']); $message = str_replace('>','&gt;',$message);
$postedtime = date("n-j-y g:i A",time()+21600);


mysql_connect('db4.awardspace.com:3306',$username,$password);
mysql_select_db($database) or die( "Unable to select database");
mysql_query("INSERT INTO forum (`name`,`subject`,`message`,`postedtime`) VALUES ('$name','$subject','$message','$postedtime')");
mysql_close();

?>

<span class="pleft">To post a message, fill in the boxes below.</span>

<form action=".05question.php" method="post">
Name: <input type="text" name="name" size="11" maxlength="10"><br />
Subject: <input type="text" name="subject" size="21" maxlength="20"><br />
Message: <br /><textarea name="message" cols="45" rows="3" wrap="soft" maxlength="500"></textarea><br />
<input type="Submit" value="Post">
</form>

<br />

<?php

mysql_connect('db4.awardspace.com:3306',$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM forum ORDER BY id DESC";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$name = mysql_result($result,$i,"name");
$subject = mysql_result($result,$i,"subject");
$message = nl2br(mysql_result($result,$i,"message"));
$postedtime = mysql_result($result,$i,"postedtime");
$id = mysql_result($result,$i,"id");
?>

<table border=0 background=table_declare.png cellpadding=0 cellspacing=0 align=center width=353 height=27>
<tr><td width=353>
<center>Posted by: <?php echo $name ?></center>
</td></tr>
</table>

<table border=0 background=table_top4.png cellpadding=0 cellspacing=0 align=center width=703 height=27>
<tr>
<td>&nbsp;<span class="pleft">Subject: <?php echo $subject ?></span></td>
<td align=right><span class="pleft">Posted on: <?php echo $postedtime ?>&nbsp</span></td>
</tr>
</table>

<table border=0 background=table_back.png cellpadding=0 cellspacing=0 align=center width=703>
<tr><td align=center>

<table border=0 cellpadding=0 cellspacing=0 align=center width=691>
<tr><td width=691>

<?php echo $message ?>

</td></tr>
</table>

</td></tr>
<tr><td><img src=table_bottom.png width=703 height=5></td></tr>
</table>

<!---------------------------------------------------------------------------------------------------------------------->
<br /><br />
<!---------------------------------------------------------------------------------------------------------------------->

<?php

$i++;
}

include("bot.php");
?>
[!--sizec--][/span][!--/sizec--]
Link to comment
Share on other sites

I don't think you've read a word of what I've said here -- you're ALWAYS handling the POST data, and you need to REDIRECT after you're done. I see no code to that effect. If you can't figure it out, I suggest you move this question to a different forum, since it's not really MySQL-related anyway.
Link to comment
Share on other sites

Yeah sorry, I didnt relize that it wasnt a mysql question. Its more of a php question. Oh and yes i did in fact read your posts all of, some of them twice actually. I didn't know what you ment on the last post. And the hidden field part I just hadnt gotten around to fixing that. I fixed that now though! Thanks for your help anyways. Ill ask my new question on the php help part.
Link to comment
Share on other sites

It's just that I never saw any code changes that were related to my comments... and if you don't understand something, it's better to simply ask for clarification than to restate your question. But yes, you'll get more appropriate guidance in a non-MySQL forum. Good luck.
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.