Jump to content

[SOLVED] Validation Help


Trium918

Recommended Posts

I have two problems with this code.

 

The first problem is getting to program to

validate the form to see if the user enter

data into the <textarea></textarea> because

without it, the use is able to click the submit

button and a blank line is inserted. I donnot want

that, but instead promt the user to enter data.

A new line is insert when someone refresh the

page.  I donnot want that.

 

The second problem is that I tried to create one

myself but it exited out because I called the exit;

command. I commented it out and refreshed the

page and it still

echo "There was no data entered!!";

.

Some how it stayed in memory even after I cleared history and

deleted cookies. I had to rename the php file in order to get it

to show in the browse. Why does it do that and how to fix it?

 

Both of these links have the same code that I provided

below!!

Example: of echo "There was no data entered!!";

 

Example: of how validation help

 

 

<?
/*if(!isset($content))
{
echo "There was no data entered!!";
//exit;
}*/
?>

<form method="POST">
<table border="1" align="center" width="300">
<tr><td colspan="2" align="center">How to Change the World?</td></tr>
<tr>
<td align="center"><img src="../images/atlas.jpg" width="100" height="165" /></td>
<td ><textarea name = "content" cols="50" rows="10"></textarea></td>
</tr>
<tr><td colspan="2" >
<?
$content = $_POST['content'];
$info = $_POST['info'];

@$fp = fopen("project4.txt", "a");
 
fwrite($fp, $content."\r\n");

$info = file('project4.txt');
for ($i = 0; $i < count($info); $i++) {
   echo '<br><hr>';
   echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES));
   
}
fclose($fp);
?>
</td></tr>
<tr><td colspan="2" align="center"><input type = submit name="submit"></td></tr>
</form>

 

Link to comment
https://forums.phpfreaks.com/topic/43879-solved-validation-help/
Share on other sites

<?
if(isset($_POST)) { //Page was submitted
    if (!$_POST['content']) { //value of content is empty
        $output = "There was no data entered!";

    } else { //Content has value
        $content = $_POST['content'];
        $info = $_POST['info'];

        @$fp = fopen("project4.txt", "a");
  
        fwrite($fp, $content."\r\n");

        $info = file('project4.txt');
        for ($i = 0; $i < count($info); $i++) {
           $output  =  '<br><hr>';
           $output .= nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES));
        }
        fclose($fp);
    }
}
?>

<form method="POST">
<table border="1" align="center" width="300">
  <tr>
    <td colspan="2" align="center">How to Change the World?</td>
  </tr>
  <tr>
    <td align="center"><img src="../images/atlas.jpg" width="100" height="165" /></td>
    <td><textarea name = "content" cols="50" rows="10"></textarea></td>
  </tr>
  <tr>
    <td colspan="2"><?=$output?></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type = submit name="submit"></td>
  </tr>
</table>
</form>

Well, you marked this thread as completed, but your last response sounds as if that's not the case. And your links above seem to have working pages now (except one possible issue). So, I'm not sure how to respond.

 

The one "issue" that i see is that the previous responses are not displayed until the user enters their response. This may be by design. And, if the user doesn't enter data the responses (if displayed) dissapear. You might want to try out this code:

 

<?php
if (isset($_POST) && !$_POST['content']) { //value of content is empty
    $error = '<span style="color:red;">There was no data entered!</span>';
} else {
    $error = '';
}

@$fp = fopen("project4.txt", "a");

if ($_POST['content']) {
    $content = $_POST['content'];
    $info = $_POST['info'];
    fwrite($fp, $content."\r\n");
}

$info = file('project4.txt');
for ($i = 0; $i < count($info); $i++) {
   $output  =  '<br><hr>';
   $output .= nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES));
}

fclose($fp);


?>

<form method="POST">
<table border="1" align="center" width="300">
  <tr>
    <td colspan="2" align="center">How to Change the World?</td>
  </tr>
  <tr>
    <td align="center"><img src="../images/atlas.jpg" width="100" height="165" /></td>
    <td><textarea name = "content" cols="50" rows="10"></textarea></td>
  </tr>
  <tr>
    <td colspan="2"><?=$error?></td>
  </tr>
  <tr>
    <td colspan="2"><?=$output?></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type = submit name="submit"></td>
  </tr>
</table>
</form>

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.