Page 1 of 1

validate. if bad, refresh (how?)

Posted: Tue Nov 09, 2004 8:14 pm
by Mini-Me
Alright. I have a page that I want to do this:
you type something and hit submit.
it validates that the code is right.
if it's wrong, it changes a variable and refreshes.
when it refreshes, a few if statements decide what goes where etc.

My problem is getting it to refresh right. There's got to be a better way of doing this, but I can't figure it out. Btw, the $testing variable I'd rather have as the $submit variable. In other words, I'd rather $submit = "Errors" if that makes any sense. Here's the entire code with nonessential parts (css, html formatting, etc) taken out:
OH, and wherever {something} is, it's really the square brakets.

Code: Select all

<?php
  $page    = (isset($_REQUEST['page']))    ? $_REQUEST['page']    : "";
  $quote   = (isset($_REQUEST['quote']))   ? $_REQUEST['quote']   : "";
  $submit  = (isset($_REQUEST['submit']))  ? $_REQUEST['submit']  : "";
  $testing = (isset($_REQUEST['testing'])) ? $_REQUEST['testing'] : "";
?>
<!--misc stuff-
   -body:-->
<?php
if ($page == "random" || $page == NULL) { print('Random Quote'); }
else if ($page == "all") { print('Quote List'); }
else if ($page == "add") {
  if ($testing == "Errors") { print ('Errors Found'); }
  else {
    if ($submit == NULL) { print ('Add a Quote'); }
    else if ($submit == "Preview") { print ('Preview'); }
    else if ($submit == "Submit") { print ('Quote Added'); }
  }
}
?>
<!--misc stuff-->
<?php
  if ($page == "all") {
    $qblah = file_get_contents("test.txt");
    print('<pre>' . str_replace("(%%:)", "\n<div><img src="spacer.gif" alt="" class="spacer" /></div>", $qblah) . '</pre>');
  }
  else if ($page == "random" || $page == NULL) {
    $qblah = file_get_contents("test.txt");
    $qlist = explode("(%%:)", $qblah);
    $qlength = count($qlist) - 1;
    $qrand = rand(1,$qlength);
    print('<pre><p>' . $qlist[$qrand] . '</p></pre>');
  }
  else if ($page == "add") {
    if ($submit == "Preview" || $submit == "Submit") {
      $qchk = array("" => "", """ => "", "'" => "");
      $quote = strtr($quote, $qchk);
      $qtb1 = substr_count($quote, "{b}");
      $qtb2 = substr_count($quote, "{/b}");
      $qti1 = substr_count($quote, "{i}");
      $qti2 = substr_count($quote, "{/i}");
      $qtu1 = substr_count($quote, "{url=");
      $qtu2 = substr_count($quote, "{/url}");
      $qtc1 = substr_count($quote, "{color=");
      $qtc2 = substr_count($quote, "{/color}");
      $qtr1 = substr_count($quote, "{");
      $qtr2 = substr_count($quote, "}");
      if ($qtb1 != $qtb2 || $qti1 != $qti2 || $qtu1 != $qtu2 || $qtc1 != $qtc2 || $qtr1 != $qtr2) {
        ################ What goes here? ################
      }
      else {
        $qreal = htmlspecialchars($quote);
        $qcng = array("{i}" => "<i>", "{/i}" => "</i>", "{b}" => "<b>", "{/b}" => "</b>",
                      "{url=" => "<a target="_blank" href="", "{/url}" => "</a>", "{color=" => "<span style="color: ",
                      "{/color}" => "</span>", "}" => "">",);
        $qreal = strtr($qreal, $qcng);
      }
    }
    if ($submit == "Preview") { print('<pre><p>' . $qreal . '</p></pre><div><img src="spacer.gif" alt="" class="spacer" /></div>'); }
    else if ($submit == "Submit") {
      print('<pre><p>' . $qreal . '</p></pre>');
      print('<form><p><input type="button" value="Submit Another Quote" onclick="location=''?page=add''" /></p></form>');
      $fp = fopen("test.txt", "a");
      fwrite($fp, "(%%:)" . $qreal);
      fclose($fp);
    } 
    if ($submit != "Submit") {
      print('<form name="addquote"><p><input type="button" value="Bold" onclick="format(''bold'');" />
             <input type="button" value="Italics" onclick="format(''italics'');" />
             <input type="button" value="URL" onclick="format(''url'');" />
             <input type="button" value="Color" onclick="format(''color'');" /></p>
             <p><textarea rows="12" cols="80" name="quote">' . $quote . '</textarea></p>
             <p><input type="hidden" name="page" value="add" />
             <input type="submit" name="submit" value="Preview" />');
      if ($submit != NULL) {
        print(' <input type="submit" name="submit" value="Submit" /></p></form>');
      }
    }
  } ?>
<!--misc stuff-->

Posted: Wed Nov 10, 2004 11:39 am
by Mini-Me
c'mon, no replies? I broke it down trying to figure out what to do, hoping it would be easier to look at:

Code: Select all

// ... ...
  else if ($page == "add") {
    if ($submit == "Preview" || $submit == "Submit") {
      ### validation ###
      if ($qtb1 != $qtb2 || $qti1 != $qti2 || $qtu1 != $qtu2 || $qtc1 != $qtc2 || $qtr1 != $qtr2) { // validation check
        ##### How can I make it change the $submit variable and reload the page with that? #####
      }
      else {
        ### format for preview/submission ###
      }
    }
    if ($submit == "Preview") { /* print preview */ }
    else if ($submit == "Submit") {
      ### print submission and submit it ###
    } 
    if ($submit != "Submit") {  ### \/ format buttons taken out ###
      print('<form name="addquote">
             <p><textarea rows="12" cols="80" name="quote">' . $quote . '</textarea></p>
             <p><input type="hidden" name="page" value="add" />
             <input type="submit" name="submit" value="Preview" />');
      if ($submit != NULL) {
        ### print submit button ###
      }
    }
  }
// ... ...

Posted: Wed Nov 10, 2004 2:11 pm
by rehfeld
to be honest the code is a bit confusing, as well as exactly what your problem is.

when you say "refresh", you mean you just spit a modified version of the form back at them?

i see 1 error that may/may not be casuing a problem for you:

Code: Select all

$submit  = (isset($_REQUEST['submit']))  ? $_REQUEST['submit']  : "";
...
      if ($submit != NULL) { // this will ALWAYS evaluate to true.
$submit can never be null, because you gave it 2 choices, either it can be the value in _REQUEST, or it can be an empty string.
this goes the same for all the times you did
if ($var != null)

you need to test it like this

if ($var != '') // if var is not an empty string


maybe that will fix your problem, but if not, please try and describe as specifically as possible what you know is working, and what is not.

Posted: Wed Nov 10, 2004 2:19 pm
by Mini-Me
Well, that's weird. Because it works. Whenever $submit = NULL, the submit button doesn't appear, and whenever it is anything else, it appears. O.o So how does it not work? I'll need to change that one anyway. If you get errors, I don't want the submit button to be there.
But yeah, I see how the top thing does that now. But for some reason it still works. O.o Anyway, I'll fix that part. ;)

Anyway, here's my entire problem in a nutshell: How can I make it so that if the validation doesn't check out, it goes to the Error page ($submit == "Errors";)?
For example, I have a NULL page, a Preview page, and a Submit page for the $submit variable. They are all sub-pages of $page = add, so to speak.
Whenever the user clicks preview or submit, I want it to validate that the code is correctly used (all tags closed, etc). If it checks out fine, proceed to preview/submit it. Else, go to an error page that says 'you have errors blah blah blah.'

Posted: Wed Nov 10, 2004 3:53 pm
by rehfeld
well if its working, i suppose its because your using != instead of !==
null evaluates to false, and so does ''

unless, you are passing the value of null into a form field? <input value="NULL">
if so, that would be why it is working.

generally, its a good habit to get into using !== or ===, as they make sure its exactly what your specifying. but thats all off the current subject,


anyway,
so your basically trying to emulate this board right?


does this outline what your tring to do?

Code: Select all

if (isSet($_POST['submit'])) { // if they submitted the form

    if (submit === 'preview') {
        // validate code
        // show error msg if needed
        // still show preview
        // give option to preview again, or submit
    }

    elseif (submit === 'submit') {
        // validate code
        if (code validates) {
            // add thier post
        } else {
            // error msg
            // dont add post
        }
    }

} else {
    // just show the form for them to fill out
}

Posted: Wed Nov 10, 2004 4:42 pm
by Mini-Me
"so your basically trying to emulate this board right?"
well, basically. But a simpler version. Just a quotes database.
I'll start with the outline and build it up. See if that works like I want.

Posted: Thu Nov 11, 2004 6:44 pm
by Mini-Me
alright, I've got it working like I want with this (not finished):

Code: Select all

<?php
  $page   = (isset($_REQUEST['page']))   ? $_REQUEST['page']   : "";
  $quote  = (isset($_REQUEST['quote']))  ? $_REQUEST['quote']  : "";
  $submit = (isset($_REQUEST['submit'])) ? $_REQUEST['submit'] : "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" />
<title>Emerald Sword Quote System</title>
<style type="text/css">
body {
  background-image: url('bg.png');
  font-size: 11px;
  font-family: Verdana, Tahoma, Arial, sans-serif;
  margin:0px 10px 0px 10px;
}
img  { vertical-align: middle; }
table, tr, td {
  font-size: 11px;
  font-family: Verdana, Tahoma, Arial, sans-serif;
}
a:link, a:visited, a:active {
  text-decoration: underline;
  color: #000000;
}
a:hover {
  color: #555555;
  text-decoration: underline;
}
h1 { margin-left: 10px; }
h3 {
  margin-top: 0px;
}
#logostrip {
  border: 1px solid #555555;
  background-color: #656565;
  background-image: url('tile_back.gif');
  padding: 0px;
  margin: 0px;
  margin-top: 10px;
}
.main {
  width: 100%;
  text-align: left;
  border: 1px solid #c0c0c0;
  background-color: #f0f0f0;
  margin: 10px 0px 10px 0px;
}
.tableborder {
  border: 1px solid #555555;
  background-color: #FFFFFF;
  padding: 0px;
  margin: 0px;
  width: 100%;
}
.maintitle {
  vertical-align: middle;
  font-weight: bold;
  color: #FFFFFF;
  padding: 8px 0px 8px 5px;
  background-image: url('tile_back.gif');
}
.titlemedium {
  font-weight: bold;
  color: #4F4F4F;
  height: 14px;
  margin: 0px;
  background-image: url('tile_sub.gif');
}
.section { background-color: #e2e2e2; }
.spacer { width: 200px; height: 1px; }
</style>
<script type="text/javascript">
<!--
function format(type) {
  if (type == 'bold') { var insert = '[b]text[/b]'; }
  else if (type == 'italics') { var insert = '[i]text[/i]'; }
  else if (type == 'url') { var insert = '[url=http://]text[/url]'; }
  else if (type == 'color') { var insert = '[color=#hex]text[/color]'; }
  document.addquote.quote.value += insert;
}
//-->
</script>
</head>
<body>
<div id='logostrip'>
<a href='http://s6.invisionfree.com/EmeraldSword/'><table cellspacing="0" cellpadding="0" width="100%">
<tr><td><img src='logo.gif' alt='' border='0' /></td>
<td align="right"><img src="sword.gif" alt="" border="0" /></td></tr></table></a>
</div>

<table class="main" cellpadding="5"><tr><td><a href="?page=all">List Quotes</a> &middot; <a href="?page=random">Random Quote</a> &middot; <a href="?page=add">Add Quote</a></td></tr></table>

<div class="tableborder">
<div class="maintitle" align="left"><img src="nav_m.gif" border="0" alt="" />&nbsp;<?php
if ($page === "random" || $page === "") { print('Random Quote'); }  ## all of these lines ##
else if ($page === "all") { print('Quote List'); }
else if ($page === "add") {
  if ($submit === "") { print ('Add a Quote'); }
  else if ($submit === "Preview") { print ('Preview'); }
  else if ($submit === "Submit") { print ('Quote Added'); }
}
?></div>
<table width="100%" border="0" cellspacing="1" cellpadding="4">
<tr><td class="titlemedium"></td></tr>
<tr><td class="section"><?php
  if ($page === "all") {
    $qblah = file_get_contents("test.txt");
    print('<pre>' . $qblah . '</pre>');
  }
  else if ($page === "random" || $page == "") {
    $qblah = file_get_contents("test.txt");
    $qlist = explode("\n<img src='spacer.gif' alt='' class='spacer' />\n", $qblah);
    $qlength = count($qlist) - 1;
    $qrand = rand(1,$qlength);
    print('<pre><p>' . $qlist[$qrand] . '</p></pre>');
  }
  else if ($page === "add") {
    if ($submit !== "") {
      if ($submit === 'Preview') {
        $qchk = array("" => "", """ => "", "'" => "");
        $quote = strtr($quote, $qchk);
        $qtb1 = substr_count($quote, "[b]");
        $qtb2 = substr_count($quote, "[/b]");
        $qti1 = substr_count($quote, "[i]");
        $qti2 = substr_count($quote, "[/i]");
        $qtu1 = substr_count($quote, "[url=");
        $qtu2 = substr_count($quote, "[/url]");
        $qtc1 = substr_count($quote, "[color=");
        $qtc2 = substr_count($quote, "[/color]");
        $qtr1 = substr_count($quote, "[");
        $qtr2 = substr_count($quote, "]");
        if ($qtb1 != $qtb2 || $qti1 != $qti2 || $qtu1 != $qtu2 || $qtc1 != $qtc2 || $qtr1 != $qtr2) {
          print("Errors have been found.");
        }
        else {
          $qreal = htmlspecialchars($quote);
          $qcng = array("[i]" => "<i>", "[/i]" => "</i>", "[b]" => "<b>", "[/b]" => "</b>",
                        "[url=" => "<a target="_blank" href="", "[/url]" => "</a>", "[color=" => "<span style="color: ",
                        "[/color]" => "</span>", "]" => "">",);
          $qreal = strtr($qreal, $qcng);
          print('<pre><p>' . $qreal . '</p></pre>');
        }
        print('<div><img src="spacer.gif" alt="" class="spacer" /></div>');
      }
      elseif ($submit === 'Submit') {
        $qchk = array("" => "", """ => "", "'" => "");
        $quote = strtr($quote, $qchk);
        $qtb1 = substr_count($quote, "[b]");
        $qtb2 = substr_count($quote, "[/b]");
        $qti1 = substr_count($quote, "[i]");
        $qti2 = substr_count($quote, "[/i]");
        $qtu1 = substr_count($quote, "[url=");
        $qtu2 = substr_count($quote, "[/url]");
        $qtc1 = substr_count($quote, "[color=");
        $qtc2 = substr_count($quote, "[/color]");
        $qtr1 = substr_count($quote, "[");
        $qtr2 = substr_count($quote, "]");
        if ($qtb1 != $qtb2 || $qti1 != $qti2 || $qtu1 != $qtu2 || $qtc1 != $qtc2 || $qtr1 != $qtr2) {
          print("Errors have been found. Please check that all tags have been closed.");
        }
        else {
          $qreal = htmlspecialchars($quote);
          $qcng = array("[i]" => "<i>", "[/i]" => "</i>", "[b]" => "<b>", "[/b]" => "</b>",
                        "[url=" => "<a target="_blank" href="", "[/url]" => "</a>", "[color=" => "<span style="color: ",
                        "[/color]" => "</span>", "]" => "">",);
          $qreal = strtr($qreal, $qcng);
          print('<pre><p>' . $qreal . '</p></pre><img src="spacer.gif" alt="" class="spacer" />');
          print('<form><p><input type="button" value="Submit Another Quote" onclick="location=''?page=add''" /></p></form>');
          $fp = fopen("test.txt", "a");
          fwrite($fp, "\n<img src='spacer.gif' alt='' class='spacer' />\n" . $qreal);
          fclose($fp);
        }
      }
    }
    if ($submit === "" || $submit === "Preview") { ?>
<form name="addquote"><p><input type="button" value="Bold" onclick="format('bold');" />
<input type="button" value="Italics" onclick="format('italics');" />
<input type="button" value="URL" onclick="format('url');" />
<input type="button" value="Color" onclick="format('color');" /></p>
<p><textarea rows="12" cols="80" name="quote"><?php echo $quote ?></textarea></p>
<p><input type="hidden" name="page" value="add" />
<input type="submit" name="submit" value="Preview" />
<?php if ($submit === 'Preview') { ?>
<input type="submit" name="submit" value="Submit" />
<?php } } } ?>
</td></table></div>
<div style="font-family: monospace; margin-bottom: 10px;">Copyright &copy; 2004 Mini-Me</div>
</body>
</html>
but take a look at where my comment is. I want that title to reflect what the page is. In other words, if it doesn't validate right, I want that title to be "Errors Found." Get the drift? ATM, it won't do that. Can you see how I could rearrange the code a bit to make that possible?

Posted: Fri Nov 12, 2004 3:40 am
by phpScott

Code: Select all

<?php
<title><?php if(isset($page)) echo $page; else echo "Emerald Sword Quote System";  ?></title>
?>
should get your $page variable in there.

Posted: Fri Nov 12, 2004 12:18 pm
by Mini-Me
eh.. yeah, I don't want to echo $page. I want to decide what to display in the page itself (not the title) based on both $page value, $submit value, and whether or not the text validated.
In other words, something like this:

Code: Select all

<div class="maintitle" align="left"><img src="nav_m.gif" border="0" alt="" />&nbsp;<?php
if ($page === "random" || $page === "") { print('Random Quote'); }
else if ($page === "all") { print('Quote List'); }
else if ($page === "add") {
  if (ERRORS FOUND) { print ('Errors Found'); } // this line
  else {
    if ($submit === "") { print ('Add a Quote'); }
    else if ($submit === "Preview") { print ('Preview'); }
    else if ($submit === "Submit") { print ('Quote Added'); }
  }
}
?></div>
The problem is, the error checking comes after the title div. I need to figure out how to check beforehand or something.

Posted: Fri Nov 12, 2004 1:52 pm
by rehfeld
store your output into variables, and do all the php before the html

if ($page === "random" || $page === "") { $out = 'random quote'; }

......
//process errors

<?php echo $out; ?>

Posted: Fri Nov 12, 2004 3:27 pm
by Mini-Me
doh. something in my mind didn't put together I could simply declare variables. bah. thanks.

Posted: Fri Nov 12, 2004 3:58 pm
by Mini-Me
alright, I have one last problem. when I use htmlspecialchars() or whatever, it converts the quotes and backslashes into esacpe sequences, which, when printed, still look like escape sequences. Hit preview a couple of times, and this:
the cat said "hello."
turns into this:
the cat said \\\\\"hello.\\\\\"
But trying to use strtr to translate them doesn't work--they just disappear. :/
So is there a way I could actually convert " to " and ' to &apos; ??