Textarea input not saving to mysql db

Associate
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
Been staring at this all night and I can't seem to figure out where it's going wrong. Basically I have implemented a content management system tutorial I found. I want to add tinyMCE so users of the system can post articles or whatever and allow them a bit of customisation, it worked fine previously, now i've added tinyMCE, the information in one section does not seem to be passed in. Is there something stupid I am missing? I realise the pagebody textbox isn't assigned a value but I tinkered with that and had no joy. A point in the right direction would be very much appreciated. The textarea in question is called 'pagebody,' here is my code:

Code:
<?php 
session_start();
include_once "admin_check.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<title>Creating New Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>

<!-- OF COURSE YOU NEED TO ADAPT NEXT LINE TO YOUR tiny_mce.js PATH -->
<script type="text/javascript" src="../tinymce/jscripts/tiny_mce/tiny_mce.js"></script>

<script type="text/javascript">
tinyMCE.init({
    mode : "textareas",
    theme : "advanced",    
    theme_advanced_buttons1 :"save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect"
});


function validate_form ( ) { 
    valid = true;
    if ( document.form.pagetitle.value == "" ) { 
    alert ( "Please enter the page title." ); 
    valid = false;
    } else if ( document.form.linklabel.value == "" ) { 
    alert ( "Please enter info for the link label." ); 
    valid = false;
    } else if ( document.form.pagebody.value == "" ) { 
    alert ( "Please enter some info into the page body." ); 
    valid = false;
    }
    return valid;
}
</script>
<style type="text/css">
<!--
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
}
-->
</style></head>

<body>
<table width="100%" border="0" cellpadding="8">
  <tr>
    <td><h3>Creating a New Page&nbsp;&nbsp;&bull;&nbsp;&nbsp; <a href="index.php">Admin Home</a> &nbsp;&nbsp;&bull;&nbsp;&nbsp;<a href="../" target="_blank">View Live Website</a></h3></td>
  </tr>
  <tr>
    <td>Be sure to fill in all fields, they are all required.<br /></td>
  </tr>
  <tr>
    <td>
    
<table width="100%" border="0" cellpadding="5">
    <form id="form" name="form" method="post" action="page_new_parse.php" onsubmit="return validate_form ( );">
  <tr>
    <td width="12%" align="right" bgcolor="#F5E4A9">Page Full Title</td>
    <td width="88%" bgcolor="#F5E4A9"><input name="pagetitle" type="text" id="pagetitle" size="80" maxlength="64" value="<?php echo $pagetitle; ?>" /></td>
  </tr>
  <tr>
    <td align="right" bgcolor="#D7EECC">Link Label</td>
    <td bgcolor="#D7EECC"><input name="linklabel" type="text" id="linklabel" maxlength="24"  value="<?php echo $linklabel; ?>" /> 
      (What the link to this page will display as)</td>
  </tr>
  <tr>
    <td align="right" valign="top" bgcolor="#DAEAFA">Page Body</td>
    <td bgcolor="#DAEAFA"><textarea name="pagebody" id="pagebody" cols="88" rows="16"><?php echo $pagebody; ?></textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="button" id="button" value="Create this page now" /></td>
  </tr>
  </form>
</table>

    
    </td>
  </tr>
</table>
</body>
</html>

This is to create a new page with the information provided but it fails when the user clicks the 'create new page' button. It hits the validate alert button. I removed a filter on another page which allowed me to create the new page, but the element used to hold the pagebody text was empty despite me entering data. Here is the parse page the form is submitted to:

Code:
<?php

// You may want to obtain refering site name that this post came from for security purposes here
// exit the script if it is not from your site and script
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$pagetitle = $_POST['pagetitle'];
$linklabel = $_POST['linklabel'];
$pagebody = $_POST['pagebody'];
// Filter Function -------------------------------------------------------------------
function filterFunction ($var) { 
    $var = nl2br(htmlspecialchars($var));
    $var = eregi_replace("'", "'", $var);
    $var = eregi_replace("`", "'", $var);
    return $var; 
} 
$pagetitle = filterFunction($pagetitle);
$linklabel = filterFunction($linklabel);
$pagebody = filterFunction($pagebody);
// End Filter Function --------------------------------------------------------------
include_once "../scripts/connect_to_mysql.php";
// Add the info into the database table
$query = mysqli_query($myConnection, "INSERT INTO pages (pagetitle, linklabel, pagebody, lastmodified) 
        VALUES('$pagetitle','$linklabel','$pagebody',now())") or die (mysqli_error($myConnection));

echo 'Operation Completed Successfully! <br /><br /><a href="index.php">Click Here</a>';
exit();
?>
 
For starters enclose the table within the form tags.

You have them placed in the wrong place.

i.e. <form><table>...</table></form>
 
For starters enclose the table within the form tags.

You have them placed in the wrong place.

i.e. <form><table>...</table></form>

The form is inside the table, that is not the problem. It functioned correctly before I added TinyMCE, now it seems to think the textarea is empty when I click submit for some reason. Despite clearly seeing text is there, I thought i'd have an issue with the tags not actually being implemented rather than this. I've no idea why this is going on
 
You need to ask the TinyMCE object for the text value, like so:

tinyMCE.get('pagebody').getContent();

You can then check whether this is blank, or in this case not '<p></p>' or something.

This is because the MCE object replaces the textarea with an Iframe.

Oh and please move your form tags to the correct location! Pretty sure you can't have them inside a table row like you have them. It only renders correctly because the browsers are bodging it.
 
I've fixed the table and form tags, this is only a prototype thats why I wasn't bothered as I never use tables normally, it was just what was used in the tutorial. Im not too sure how to use the getContent you mentioned, I had a play setting it to the value and calling it elsewhere and such with no luck.

If this is how you fix it, its handy the documentation mentions none of it the gimps! Thank you for your help SMB30
 
Just use the getContent in your javascript, like so:

Code:
function validate_form ( ) { 
    valid = true;
    if ( document.form.pagetitle.value == "" ) { 
    alert ( "Please enter the page title." ); 
    valid = false;
    } else if ( document.form.linklabel.value == "" ) { 
    alert ( "Please enter info for the link label." ); 
    valid = false;
    } else if ( tinyMCE.get('pagebody').getContent() == "" ) { 
    alert ( "Please enter some info into the page body." ); 
    valid = false;
    }
    return valid;
}

That should give you the correct value.
 
Ingenius, I actually did it but left an extra tag which buggered it up completely. Thank you very much for your help mate, I know exactly where to go from here :)
 
Back
Top Bottom