Basic If Statement question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dave79
Forum Newbie
Posts: 14
Joined: Wed Nov 10, 2004 10:59 am

Basic If Statement question

Post 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...
Last edited by dave79 on Fri Nov 19, 2004 7:28 am, edited 3 times in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
dave79
Forum Newbie
Posts: 14
Joined: Wed Nov 10, 2004 10:59 am

Post by dave79 »

Re: point 1 - thanks.

I want to keep my validation server side. Can anyone else help?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
dave79
Forum Newbie
Posts: 14
Joined: Wed Nov 10, 2004 10:59 am

Post 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 &#125;
if (isset($HTTP_POST_VARS&#1111;"submit"]) and ($checked))&#123;
$msg .= "Firstname:  ".$HTTP_POST_VARS&#1111;"firstname"]."\n";
$msg .= "Lastname:  ".$HTTP_POST_VARS&#1111;"lastname"]."\n";
$msg .= "Company:  ".$HTTP_POST_VARS&#1111;"company"]."\n";
$msg .= "website:  ".$HTTP_POST_VARS&#1111;"website"]."\n";	

mail("info@mywebsite.com","Client Portfolio request for references", $msg);
$location = "http://mywebsite.com/feedbacksent.html";
header ("Location:$location");
&#125;
else &#123;
header ("Location:client_portfolio_form_with_template.php#form");
&#125;

?>
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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"
dave79
Forum Newbie
Posts: 14
Joined: Wed Nov 10, 2004 10:59 am

Post 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?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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..
dave79
Forum Newbie
Posts: 14
Joined: Wed Nov 10, 2004 10:59 am

Post 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="&nbsp;";
// 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

Code: Select all

tags to post php code. [/color]
dave79
Forum Newbie
Posts: 14
Joined: Wed Nov 10, 2004 10:59 am

Post by dave79 »

Can anyone help at all - I'm sure the solution required is realtively straight forward?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

I don't know of any way to do it with out using PHP to write Javascript values.
dave79
Forum Newbie
Posts: 14
Joined: Wed Nov 10, 2004 10:59 am

Post 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.
dave79
Forum Newbie
Posts: 14
Joined: Wed Nov 10, 2004 10:59 am

Post 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.
Post Reply