Jump to content

Quick form handling question!


benji87

Recommended Posts

Could some point me in the right direction or give me a code example to submit a form on the same page, so as i wont have to link the form to a processing script but have them on the same page. Im sure its simple but ive never done it before and i cant find any examples!  :-\ Cheers

Link to comment
https://forums.phpfreaks.com/topic/43815-quick-form-handling-question/
Share on other sites

Cheers, unexpected { on line 3  ???

 

<?php
if (isset($_POST['submit']) 
{
$topicid = $_POST['id'];
$comment = $_POST['message'];
$commentby = $_POST['commentby'];

$date = date("j F");

$sql = mysql_query("INSERT INTO ssrfc_noticecomments (topicid, comment, commentby, date)
VALUES('$topicid', '$comment', '$commentby', '$date')") or die (mysql_error());

if(!$sql){
echo 'There has been an error while trying to submit your post.';
} else {
$id = mysql_insert_id();

}
<?

 

Dont see why that is unexpected??

Try this code:

 

<?php
if (isset($_POST['submit']) 
{
$topicid = $_POST['id'];
$comment = $_POST['message'];
$commentby = $_POST['commentby'];

$date = date("j F");

$sql = mysql_query("INSERT INTO ssrfc_noticecomments (topicid, comment, commentby, date)
VALUES('$topicid', '$comment', '$commentby', '$date')") or die (mysql_error());

if(!$sql){
echo 'There has been an error while trying to submit your post.';
} else {
$id = mysql_insert_id();

}
}
?>

Here is my code, it all seems to process fine it just wont insert into the database  :-\ can anyone help?

 

<?php
session_start();
include 'includes/db.php';

$username = $_SESSION['username'];
$id = $_GET['id'];

$query=("SELECT * FROM ssrfc_noticecomments WHERE topicid = $id ORDER BY date ASC LIMIT 0,10");
$result=mysql_query($query);
$num=mysql_numrows($result);

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

$commentby=mysql_result($result,$i,"commentby");
$date=mysql_result($result,$i,"date");
$comment=mysql_result($result,$i,"comment");

<table width="100%" border="0" cellpadding="2" cellspacing="2" bgcolor="eaeaea">
  <tr>
    <td class="headers">Posted by: <? echo "$commentby" ?>  Date: <? echo "$date" ?></td>
  </tr>
  <tr>
    <td class="navigation"><? echo "$comment" ?></td>
  </tr>
</table>

$i++;
}
mysql_close();

<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
  <table width="100%" border="0" cellspacing="2" bgcolor="eaeaea">
    <tr>
      <td><div align="center">
        <textarea name="message" cols="40" rows="10" class="navigation" id="message"></textarea>
      </div></td>
    </tr>
    <tr>
      <td><div align="center">
        <input type="submit" name="Submit" value="Submit" />
      </div></td>
    </tr>
    <tr>
      <td><div align="center">
        <input name="id" type="hidden" id="id" value="<? echo "$id" ?>" />
        <input name="commentby" type="hidden" id="commentby" value="<? echo "$username" ?>" />
</div></td>
    </tr>
  </table>
</form>

if (isset($_POST['submit']))
{
$topicid = $_POST['id'];
$comment = $_POST['message'];
$commentby = $_POST['commentby'];

$date = date("j F");

$sql = mysql_query("INSERT INTO ssrfc_noticecomments (topicid, comment, commentby, date)
VALUES('$topicid', '$comment', '$commentby', '$date')") or die (mysql_error());

if(!$sql){
echo 'There has been an error while trying to submit your post.';
$id = mysql_insert_id();
}
}
?>

Are you getting an error? If so what error.

 

$sql = mysql_query("INSERT INTO ssrfc_noticecomments (`topicid`, `comment`, `commentby`, `date`)
VALUES('$topicid', '$comment', '$commentby', '$date')") or die (mysql_error());

 

Try that, date is usually reserved word in SQL.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.