Adding a new page to a web site

I am trying to set up a CMS that will allow an administrator to add new pages to a web site. When PHP reads the added text I insert it into a MySQL database using placeholders. PHP correctly gets the added text, but when it inserts them into the database the text is preceded by an amount of white space that corrupts the display of the saved text. Any ideas about what might be causing this?

Tony Holland

Hard to say without seeing the code that you use to update the database.

1 Like

Thanks droopsnoot.

Here’s a code extract that sets up the form from which the added text is read :

	<div id = "admin_heading">
			<label for = "title">Heading</label>
				<textarea id = "title" name = "title"  cols = "60" >
				<?php if(isset($title)):
					htmlout($title);
					endif;?>
					</textarea>
		</div>			
		
		<div id = "admin_date">
			<label for = "lecturedate">Date</label>
				<textarea id = "lecturedate" name = "lecturedate"  cols = "60" >
				<?php if(isset($lecturedate)):
					htmlout($lecturedate);
					endif;?>
					</textarea>
		</div>
		
		<div id = "admin_sub_heading">
			<label for = "sub_heading">Sub Heading</label>
				<textarea id = "sub_heading" name = "sub_heading" rows = "2" cols = "60"></textarea>
		</div>
		
		<div id = "admin_content">
			<label for = "content">Content</label>
				<textarea id = "content" name = "content" rows = "2" cols = "80">
				<?php if(isset($text)):
					htmlout($text);
					endif;?>
				</textarea>					
		</div>	
		
		<div id = "form1">
			
			
			<input type = "submit" name = "action" id = "action" value = "New">				
			<input type = "submit" name = "action" id = "action" value = "Edit">
			<input type = "submit" name = "action" id = "action" value = "Delete">
			<input type = "submit" name = "action"  onClick = "myFunction" value = "Clear">
		</div>
		
	</form>
	
<script type = "text/javascript">
	function myFunction()
	{
		document.getElementById("myForm").reset();
		document.getElementById("tableform").reset();
	}
</script>

And here’s an extract of the code for the database update:

if (isset($_POST[‘action’]) and $_POST[‘action’] == “New”)
{ htmlout(‘New visit’);
//include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’;

try{
	$sql = 'INSERT INTO lectures SET lecturedate = :lecturedate, title = :title, description = :description';
	$s = $pdo->prepare($sql);
	$s->bindvalue(':lecturedate', $_POST['lecturedate']);
	$s->bindvalue(':title', $_POST['title']);
	$s->bindvalue(':description', $_POST['content']);
	$s->execute();
	}

catch(PDOException $e){
	$error = 'Error inserting data into table';
	include 'error.html.php';
	exit();
}
// Check all set up correctly
$text = $_POST['content'];
$lecturedate = $_POST['lecturedate'];
$title = $_POST['title'];
htmlout($title);
htmlout($lecturedate);
htmlout($text);

}

Many thanks for your help

Tony Holland

I don’t know what the htmlout function is doing. but my guess is it’s appending to the textareas’ “col=60” instead of replacing it.

Maybe change to text inputs instead of textareas?

Though I think using trim() will probably do the trick

1 Like

Thanks for your help, Mittineague. I’ve not come across trim(), but I’ll give it a go!

Best wishes

Tony Holland

It might just be a matter of removing all space within textarea.

<div id="admin_heading">
    <label for="title">Heading</label>
    <textarea id="title" name="title"  cols="60" ><?php if(isset($title)):htmlout($title);endif;?></textarea>
</div>

And as suggested use trim on POST.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.