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
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??

Link to comment
Share on other sites

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();

}
}
?>

Link to comment
Share on other sites

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();
}
}
?>

Link to comment
Share on other sites

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.

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.