Simple script with something wrong

You probably want your

if (!$_POST['textblock'])
{

inside the body.

1 Like

Thank you, that was the problem.

1 Like

One thing, if you say

if ( something ) {

rather than saying

} elseif ( !something ) {

later on, it is sufficient just to say

} else {
2 Likes

All this is to sharpen up my scripting. The above was taken directly from a tutorial which as per usual didn’t work, well it did work until I started adding the css. Having a time getting my head around this.

Yes, you have to choose your tutorials carefully. Anyone with a PC can write tutorials. It doesn’t mean they are any use!

Do you understand why it didn’t work? Putting the if statement right at the beginning meant the HTML statement specifying the CSS wasn’t output.

Since this is a PHP issue I will move it to the PHP forum.

Thanks for your help and thanks for shifting post, I do try to post in the correct forums.

1 Like

You have the first condition ABOVE html, head and body tags so they are only “used” when NOT $_POST[‘textblock’].

Move your opening condition and final closing bracket within the body tags so html, head and body tags are always used.

<?php
error_reporting(0);
?>
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="writing2.css">

<title>Write to a text file</title>
</head>
<body> 
<?php
if (!$_POST['textblock'])
{
?>

<h1>Adding a text block to a text file:</h1>
<form action="writing2.php" method='post'>
<textarea name="textblock"></textarea>
<input type='submit' value='Add text'>
</form>

<?php
}
elseif ($_POST['textblock'])
{
// Open the text file

$f = fopen("textfile.txt", "w");

	// Write text
	fwrite($f, $_POST["textblock"]); 

	// Close the text file
	fclose($f);

	// Open file for reading, and read the line
	$f = fopen("textfile.txt", "r");

	// Read text
?>	
	
<div>
			<h3><span>Date<span>-</span>Heading</span> </h3>
			<br><h1>This was added to the text file:</h1></br>
				<p> <?php echo fgets ($f); ?>
				
				
				</p>
			<?php fclose($f);  ?>
</div>

<br>
	<h1>Write Something:</h1>
<form action="writing2.php" method='post'>
<textarea name="textblock"></textarea>
<input type='submit' value='Add text'>
</form>

<?php
}
?>

</body>

</html>

"Move your opening condition and final closing bracket within the body tags "

NO, NO, and NO.

Your form should not even be in the PHP in the first place. All the Php goes at the top of the page. The HTML goes below it.

You need to check the REQUEST METHOD, not look for an element name to be submitted in order for your script to work. You need to make sure the $f variable is available before you try to use it in your “Success” message.

Since you are submitting to the same page you can just leave the action out completely.

Why in the world are you turning off error reporting when you are developing?

The Correct way:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//Process your form
}
?>
HTML GOES HERE

Well I do agree with you on the way the structure of the page should be. I was just responding to the OP question of why css is not working.

1 Like

Sheeesh, can I get any more wrong. Long time since I did all this. Now I read php is on its way out.

Thanks guys, do really appreciate th comments.

I don’t know what you’re reading but Php is far from being on its way out.

1 Like

Not to get off topic but on a whole the industry is moving toward JavaScript driven front-ends with server-side Web APIs. In my experience those Web APIs are more and more built with .NET and Java rather then open source technologies like PHP, Ruby, and Python. Thats what I’ve been noticing the last few years. Whether that means php will eventually become obsolete that remains to be seen. One thing that is keeping the language well and alive are the monolithic content managements systems that are mostly unmatched in any other language. Node.js is great but the open source CMSs pale in comparison to the ones available in php.

2 posts were merged into an existing topic: Python crashing when I load numpy surface

Relying on Javascript also has it’s pitfalls. Not everyone is going to allow websites to run Javacript on their browsers. I know that some of the staff here don’t even allow their browsers to use Javascript. Only for websites that they know they can trust. PHP is fine where it is. Going back on topic, it’s best to separate business logic and presentation. It allows for best practice and it’s more easier to maintain. Though not everything can work this way so using spaghetti code is inevitable.

Not only that, but sometimes javascript can fail to load / get downloaded properly from the server.

2 Likes

I love php, but I also have a passion to learn computer science. Have always wanted to build apps for different things.

Doesn’t matter which road I take really, I’m 63 and hardly doing it for employment. This is truly a love thing for me. Sadly they didn’t have all this when I was at school.

1 Like

A post was merged into an existing topic: Python crashing when I load numpy surface

As a more general piece of advice, developing with error_reporting(0); is a really bad idea! You will get blank pages rather than more meaningful error messages which can help you identify where issues are.

1 Like

Yes thank you, I copied it as is directly from a tutorial complete with error_reporting(0). Some one else pointed out is was a bad idea also, immediately I remembered from years ago I had E_ALL or something.

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