Jump to content

else problem


bries

Recommended Posts

im trying to get this code to determine whether or not the form entered had anything in it, and if it doesn't show an error message and redirect the user back to the form. and if it does have content sumbit it.

 

the problems i've had so far is either it will display the message, and submit the empty form or just not work at all. ill attatch the entire code.

 

<?php
$msg=fopen('data1.txt','r');
$read=fgets($msg);
print "\"";
echo $read;
print "\"";
fclose($msg);
?>

<?php
    $submit = $_REQUEST['submit'];
    $data = $_POST['data'];
    $file = "data1.txt"; 
    $fp = fopen($file, "w") or die("ohno something went wrong"); 
if(empty($data)) { echo "no text entered, redirecting... or click <a href=\"index.php\">here</a><meta http-equiv=\"refresh\" content=\"3;url=index.php\" />";   
}    
else($submit == 1) {
    fwrite($fp, $data);
    fclose($fp); 
echo "post submitted, redirecting... or click <a href=\"index.php\">here</a><meta http-equiv=\"refresh\" content=\"3;url=index.php\" />";
}
?>

<form name="form" method="post" action="index.php?submit=1">
<textarea name="data" rows="4"></textarea>
<input type="submit" value="write" />
</form>

Link to comment
Share on other sites

$data isn't empty because you are putting $_POST['data'] into it. I think even tho there's no value it still won;t qualify as empty. Try:

if(!isset($_POST['data'])) { echo "no text entered, redirecting... or click <a href=\"index.php\">here</a><meta http-equiv=\"refresh\" content=\"3;url=index.php\" />";   
} 

 

maybe?

 

Link to comment
Share on other sites

echo var_dump($data); and see what it shows for values. Also, shouldn't this be an elseif?

else($submit == 1) {

 

I was thinking the same ... but his form action makes submit = 1 anyway. Else if would be pointless because submit is always one when it gets to this point. It should probably just be else with no ( .. )

Link to comment
Share on other sites

I'm looking at in input form on my site that I validate.. i did this:

$fn = $_POST['Fname'];
...
if($fn == ""){
echo 'Please fill out all fields.';
}
else{
echo 'Thank you'
}

 

Can you try if($data=="") { echo "no text perhaps?

 

 

But then a single space would be validated as good input.

 

if( strlen(trim($_POST['var'])) > 0 ) { // Or greater than the minimum valid value for the data type . . .
     //  then perhaps go a step farther and run a preg_match() or other check on it

Link to comment
Share on other sites

yeah i've tried

if($data=="") { echo...

and its still letting the code execute all the way through to the posting action.

 

the php dump gives me this: string(0) "" so i know that the $data is actually empty when no text is entered.

 

(submit == 1) just isn't working to put under an else or elseif, is there another way i can write this part?

Link to comment
Share on other sites

I assume you want to change the file's contents if a valid string is entered in the <textarea></textarea>. This should work just fine. Read through the comments so you can see what exactly it does, and why. I only quickly tested it, but if anyone sees any problems, I'm sure they'll point them out . . .  :P

 

<?php
$file = 'data1.txt'; // name of file to be used, if not in same directory as script, include path
$min_input = 5;  // Minimum string length to be considered as valid input. Any less will show error, and leave file unchanged.

if( $_POST['submitted'] == 1 ) { // presence of hidden field value indicates form has been submitted.
   if(strlen(trim(strip_tags($_POST['data']))) < $min_input ) { // if prepared string will be less than $min_input chars, assume bad input.
      $errors[] = 'Entered text was too short, file not updated'; // string too short, store error in array
   } else {
      $data = trim(strip_tags($_POST['data'])); // remove leading/trailing whitespace, and any markup tags to help prevent XSS attacks
      if( !$fp = fopen($file, "w") ) { // open file for writing
         $errors[] = "File open failed for writing.";  // if no open, store error as above
         trigger_error( "Opening of file '$file' for writing failed" ); // log, if file open errors
      } else {
         fwrite($fp, $data); // write the string
         fclose($fp);  // close the file handle
         unset( $_POST['data'] ); // clear the sring from $_POST['data'] so form is no longer sticky.
      }
   }
}

// code below is executed always

$handle = fopen("$file", "r"); // open file for reading
if ($handle) {
   while (!feof($handle)) {
      $buffer = fgets($handle, 4096); // allow for larger file size
      $read .= $buffer; // add each chunk to $buffer
   }
   fclose($handle); // close the file
   echo "File '$file' currently contains:<br /><pre>\"" . $read . "\"</pre><br />"; // display the contents of $file
} else {
   $errors[] = 'Unable to open file.'; // if file won't open, store and log error
   trigger_error("Opening of file: '$file' for reading failed");
}

if( !empty($errors) ) { // if there were errors, loop through the array and display them.
   foreach( $errors as $v ) {
      echo "<font color=\"red\">$v</font><br />";
   }
}
?>
<form name="form" method="post" action=""> <?php // Form submits to itself. No need for meta redirects. // ?>
<textarea name="data" rows="4"><?php if( !empty($_POST['data'])) { echo $_POST['data']; } ?></textarea> <?php // If text entered doesn't validate, it is redisplayed // ?>
<input type="submit" value="write" />
<input type="hidden" name="submitted" value="1" /> <?php // Added hidden field to pander to some versions of M$ Internet Exploder's non-compliance with standards // ?>
</form>

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.