Page 1 of 1
Basic If Statement question
Posted: Wed Nov 10, 2004 11:10 am
by dave79
NOTE: I have re-submitted this problem in a new topic to include new advice / update code. For the latest, please refer to:
viewtopic.php?p=143976#143976
------------------------------------------------------------------------------
Hello all,
I am trying to create a form that uses php to validate it. NOTE: I want to avoid client-side languages. The validation I'm using works fine.
If the form is filled in correctly, feedbacksent.html loads. If the form is filled in incorrectly, I want the page to refresh showing errors (which it does) and then the browser to jump down to where the form is located (which is wear my problem lies...)
Please now scroll down to my posting on Saturday 13th, 7pm for my code and more details...
Posted: Wed Nov 10, 2004 12:54 pm
by josh
1- You shold specify the full URL $location="
http://yoursite........
2-You can scroll down with javascript.. it was something like document.scoll() or something look it up on google
Posted: Thu Nov 11, 2004 3:57 am
by dave79
Re: point 1 - thanks.
I want to keep my validation server side. Can anyone else help?
Posted: Thu Nov 11, 2004 4:08 am
by CoderGoblin
If you have ID="XYZ" (must be unique) for each element you can use "Location:script.php#id" to jump to that point. NOTE: This is not always centered in page but users can always see it.
Posted: Thu Nov 11, 2004 5:55 pm
by dave79
I've tried using your solution but the code below does not work. Can anyone spot a problem? It works up to the line:
header ("Location:$location");
Code: Select all
<?php }
if (isset($HTTP_POST_VARSї"submit"]) and ($checked)){
$msg .= "Firstname: ".$HTTP_POST_VARSї"firstname"]."\n";
$msg .= "Lastname: ".$HTTP_POST_VARSї"lastname"]."\n";
$msg .= "Company: ".$HTTP_POST_VARSї"company"]."\n";
$msg .= "website: ".$HTTP_POST_VARSї"website"]."\n";
mail("info@mywebsite.com","Client Portfolio request for references", $msg);
$location = "http://mywebsite.com/feedbacksent.html";
header ("Location:$location");
}
else {
header ("Location:client_portfolio_form_with_template.php#form");
}
?>
Posted: Fri Nov 12, 2004 3:55 am
by rehfeld
when you say "doesnt work", what doesnt work? is there an error?
if you trying to say the browser is not redirected
maybe try adding a space after the colon
"Location: $location"
Posted: Fri Nov 12, 2004 9:20 am
by dave79
Thanks for the response. I've tried your suggestion but that doesn't solve it. I've just found out I've been getting the following message (which was hidden behind some graphics):
Warning: Cannot modify header information - headers already sent by (output started at /mywebsite.com/user/htdocs/testarea/client_portfolio_form_with_template.php:59) in /home/fhlinux196/n/mywebsite.com/user/htdocs/testarea/client_portfolio_form_with_template.php on line 623
Note: my php file follows the format:
1. Defining rules/validation
2. HTML
3. Code shown in this posting
Is there anything I can use in replace of 'header'? The code works if 'header' is only used once, but not if used twice. Is there anything I can use instead?
Posted: Fri Nov 12, 2004 1:48 pm
by rehfeld
headers must be sent before any output to the browser
<html>
<?php
header()// will fail
even a single space, tab, or new line being output before the header will fail it.
Posted: Fri Nov 12, 2004 9:11 pm
by neophyte
Posted: Fri Nov 12, 2004 9:18 pm
by josh
Use output buffering, OB is god hehe
Just put
ob_start();
as the first line of your code then ob_end_flush(); to send the output... ob_end_clean(); gets rid of the buffer... What this does is hold all outputs untill the entire script finishes, that way you can output the headers without worrying..
Posted: Sat Nov 13, 2004 1:00 pm
by dave79
Thanks to all who have contributed. Still having problems though. My script below has ob_start in it now. (see below for problems):
Code: Select all
<?php
ob_start();
function check_len(&$check, $field, $max, &$err_field, $err="", $min,
// etc, more statements here that determine validation (which work well)
if (empty($HTTP_POST_VARS["firstname"])) $HTTP_POST_VARS["firstname"]="";if (empty($err_firstname)) $err_firstname=" ";
// More statements here, 1 for each field in the form
$checked = true; if (isset($HTTP_POST_VARS["submit"])){
check_len($checked, $HTTP_POST_VARS["firstname"],50,$err_firstname,"Your first name cannot exceed 50 characters",1,"You must fill in your first name");
// More statements here, 1 for each field in the form
}
if ( empty($HTTP_POST_VARS["submit"]) or (!$checked) ){
<!-- my HTML goes here. Including my form with id="form" -->
<?php }
if (isset($HTTP_POST_VARS["submit"]) and ($checked)){
$msg .= "Firstname: ".$HTTP_POST_VARS["firstname"]."\n";
// More statements here, 1 for each field in the form
mail("info@mywebsite.com","Email subject goes here", $msg);
$location = "http://www.mywebsite/feedbacksent.html";
header ("Location:$location");
}
else {
$failvalidation = "http://www.mywebsite/this_script.php#form";
header ("Location:$failvalidation");
}
ob_end_flush();
?>
The script works fine if I remove the following lines. With them, the script doesn't even load (i.e. no error message!). However, without them, the browser doesn't jump down the form:
Code: Select all
else {
$failvalidation = "http://www.mywebsite/this_script.php#form";
header ("Location:$failvalidation");
}
I know the concept *can* work because I've tested making the browser jump to the form when the form is filled in corrently (i.e. currently on passing form validation, the feedbacksent.html is loaded - I've tested the concept by replacing this file with #form and the browser does jump to the form).
It would be great to sort this out. When it is sorted, i'll post the full script. You can test the script/form (without the second "else {..."), here:
http://www.testarea.north-square.com/fo ... mplate.php
...feel free to fill it in/fail validation etc.
Weirdan | Dave, please use
Posted: Mon Nov 15, 2004 3:02 pm
by dave79
Can anyone help at all - I'm sure the solution required is realtively straight forward?
Posted: Mon Nov 15, 2004 3:12 pm
by neophyte
I don't know of any way to do it with out using PHP to write Javascript values.
Posted: Mon Nov 15, 2004 4:40 pm
by dave79
I know it can be done using php, because I've created this example. Here, instead of a correctly completed form taking the user to the "formsent.html", it jumps users back up to the form. I suggest you test this by filling in the form (don't press submit yet), and then scrolling so that the "request references" button is right at the top of the screen and then pressing it.
http://www.testarea.north-square.com/fo ... d_test.php
This proves that my problem relates to my IF statement, rather than anything else. I would be most grateful if someone could read through the logic and tell me how I can make the screen jump up to the form, when a user fails validation.
Posted: Mon Nov 22, 2004 3:41 am
by dave79
[Problem has now been solved]
I needed:
<form id="form" action="<? echo $GLOBALS["PHP_SELF"] ?>#form" method="post">
to make the view jump to where the form is.