Page 1 of 1

String Variable Not Printing

Posted: Sat Nov 10, 2007 6:45 pm
by The Merg
I have some PHP code that needs to output HTML code. I have two functions that will be outputting the same HTML, so I figured I'd put it in a variable and just reference the variable within the functions. This does not seem to be working out for me. I have tried declaring the $htmltop and $htmlbottom variables as global to no avail as well. I'm not well versed in PHP, so I'd appreciate it if someone could help me out. The function does work as the resulting page does display the output from show_error_list().

Code: Select all

<?php

session_start();

$errors = array();

$htmltop = <<<TopOfPage
<html>
<head>
<title>Error</title>
</head>
<body>
<p>There were problems with your submission. Please go
 back to the previous page and correct the following errors:
</p>
TopOfPage;

$htmlbottom = <<<BottomOfPage
</body>
</html>
BottomOfPage;

function show_errors($errors) {
    print $htmltop;
    show_error_list($errors);
    print $htmlbottom;
}
If I use the print command with the html coded into it, such as:

Code: Select all

function show_errors($errors) {
    print("<html>\n<head><title>Error</title></head>\n
	   <body>\n<p>There were problems with your submission. Please go
	   back to the previous page and correct the following errors:
	   </p>\n");
    show_error_list($errors);
    print("</body>\n</html>");
}
This will result in the page being created correctly with the HTML code desired.

TIA,
Merg

Posted: Sat Nov 10, 2007 7:10 pm
by Christopher

Code: Select all

function show_errors($htmltop, $errors, $htmlbottom) {
    echo $htmltop;
    show_error_list($errors);
    echo $htmlbottom;
}
Although there are better page building schemes.

Posted: Sat Nov 10, 2007 11:09 pm
by Bogey
Try this... this should work... it worked for me :)

Code: Select all

<?php

session_start();

$errors = array();
$htmltop = '<html><head><title>Error</title></head><body><p>There were problems with your submission. Please go back to the previous page and correct the following errors:</p>';

$htmlbottom = '</body></html>';


function show_errors($errors) {
    print $htmltop;
    show_error_list($errors);
    print $htmlbottom;
}
In the variables that you set up... you needed to enclose the codes with ' and ' why? Because you are storing something into a variable rather than setting a variable to something. Like if you had a variable to set to true it would look like...

Code: Select all

$error = true;
If you echo that...

Code: Select all

$error = true;
echo $error;
The following is what you would get...
1
why? Because True = 1 and false = *nothing*. So if error is equal to 'false' you would get the following
But if you want a variable to represent something... such as 'Hello World"

Code: Select all

$somvar = 'Hello World';
And if you echo that you would get "Hello World". But if you put HTML codes in between the apostrophes, then the browser obviously renders it and shows the HTML renders so you would see a table if you save a table into a variable...

Posted: Sun Nov 11, 2007 8:21 pm
by The Merg
arborint wrote:

Code: Select all

function show_errors($htmltop, $errors, $htmlbottom) {
    echo $htmltop;
    show_error_list($errors);
    echo $htmlbottom;
}
Although there are better page building schemes.
That did the trick! Why wouldn't the $htmltop and $htmlbottom variables be available to the function if they were declared as global variables? Shouldn't that preclude me from having to add them as arguments to the function?
bogey wrote: Try this... this should work... it worked for me

Code: Select all

<?php 

session_start(); 

$errors = array(); 
$htmltop = '<html><head><title>Error</title></head><body><p>There were problems with your submission. Please go back to the previous page and correct the following errors:</p>'; 

$htmlbottom = '</body></html>'; 


function show_errors($errors) { 
    print $htmltop; 
    show_error_list($errors); 
    print $htmlbottom; 
}
In the variables that you set up... you needed to enclose the codes with ' and ' why? Because you are storing something into a variable rather than setting a variable to something.
With using the single quotes though, you need to keep the string all on one line though, correct? My example shows very limited HTML compared to what I acutally use. That's why I used the included (<<<) method. You can see what I mean by looking at the actual page (http://www.themerg.net/contact.php). Just click on submit without entering anything and you'll see the error page being generated. All the HTML info on the error page is included in those two variables.

Thanks all for your help.

- Merg

Posted: Sun Nov 11, 2007 8:44 pm
by Christopher
The Merg wrote:That did the trick! Why wouldn't the $htmltop and $htmlbottom variables be available to the function if they were declared as global variables? Shouldn't that preclude me from having to add them as arguments to the function?
No within a function you can only see local variables. You could declare those variables as global within the function, but that is a bad practice. Passing them is the proper thing to do. Or, implement a page building class to hold everything -- even better.
The Merg wrote:With using the single quotes though, you need to keep the string all on one line though, correct? My example shows very limited HTML compared to what I acutally use. That's why I used the included (<<<) method. You can see what I mean by looking at the actual page (http://www.themerg.net/contact.php). Just click on submit without entering anything and you'll see the error page being generated. All the HTML info on the error page is included in those two variables.
No, strings can span several lines.

For your example, I would recommend that the form submits to itself rather than a different script. That way you don't need to have two pages that display the form. On success, redirect to another page. That deals the form being re-submitted if they hit refresh.

Posted: Sun Nov 11, 2007 9:38 pm
by The Merg
arborint wrote:For your example, I would recommend that the form submits to itself rather than a different script. That way you don't need to have two pages that display the form. On success, redirect to another page. That deals the form being re-submitted if they hit refresh.
I would rather do that, but not quite sure how. I'm using the contact form called Simple Contact Form (scform.php by James Seymour LinxNet.com) and modified it to fit into my site. As far I understand your suggestion, the action command for the form, which is in contact.php, would be to call contact.php (itself). But, my PHP coding abilities are kinda limited at that point (I'm a VB6 guy). Would I then have the whole script that I currently have in contact-errors.php, my processing page, at the beginning of contact.php and then just have a if...then statement to skip that code if nothing is in the form variables?

Kinda like:

Code: Select all

If form fields are not blank then
     PHP form processing code...

Else
     Display blank form

End If
Right now I have the processing page as contact-errors.php and if everything goes okay it moves to contact-thankyou.php. If there are errors, it just stays on contact-errors.php, lists the errors there (not the form) and then redirects back to contact.php. Any help is greatly appreciated.

Thanks,
Merg

Posted: Sun Nov 11, 2007 9:45 pm
by Bogey
Heres how you can do it...

Code: Select all

---Your HTML here---
---the following is the process of the form---
<?php
  if(!empty($_POST['submit']))
  {
//Put the process of the form here
  }
?>
---your form here with the action to itself---
--the rest of your site
I think it is clear on what you do.

Posted: Sun Nov 11, 2007 10:14 pm
by feyd
Looking for the submit button can create problems for browser that do not submit the button data if it wasn't used to submit the form. Look for a field (or all the fields, preferably) from the form.

Posted: Mon Nov 12, 2007 1:28 am
by s.dot
I use a hidden input field specifying the action of the form, and check for that on submission.

Code: Select all

<input type="hidden" name="action" value="postBlog" />

Code: Select all

<?php

if (!empty($_POST['action']) && ($_POST['action'] == 'postBlog'))
{
    //process the form
}

Posted: Mon Nov 12, 2007 7:53 am
by The Merg
Okay. Working well now (http://www.themerg.net/contact2.php), kinda. However, when the error list comes up, it just lists the errors. In order to get back to the form, I still have to hit the back button. Should I look at it auto-refreshing back to the form and then populating the form fields back in with what the user entered or should the form just display below the error list?

Also, if the form is filled out correctly and submitted, I get the

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started...
error. This is because I've already outputted info before this code has been executed, correct? How would I work around that? In the past, I made sure that the PHP code was the first thing in the page, but if I have the PHP code within the HTML I'm going to get this error, right?

Thanks again!

- Merg

Posted: Mon Nov 12, 2007 12:15 pm
by feyd
The Merg wrote:error. This is because I've already outputted info before this code has been executed, correct? How would I work around that? In the past, I made sure that the PHP code was the first thing in the page, but if I have the PHP code within the HTML I'm going to get this error, right?
Correct. Place all non-display logic before any output to help ensure this type of error does not happen. Use headers_sent() as well if possible.

Posted: Tue Nov 13, 2007 6:46 pm
by The Merg
Okay, I appreciate all the help, but I hope you guys can bear with me a bit longer. I am not really a PHP coder (mostly code in VB6), so I am not quite sure on the exact implementation although my coding background is floating ideas around in my head as to how this should work. My understanding is that the page would look something like this:

Code: Select all

<php form processing>
Form processing code that does not display anything and creation of headers for e-mail for contact form
</php>

<html>
<body>

<form>
Form fields
</form>

<php error display>
Code that displays output if error is found from form entry
</php>

</body>
</html>
My biggest issue is trying to break apart the current form processing code and being able to pass the values of variables that are created in <php form processing> to <php error display>.

Any help would be appreciated. Even a simple version of the way the code should be would be a help as I am pretty good at adapting current code to my use.

Thanks,
Merg

Posted: Tue Nov 13, 2007 7:08 pm
by feyd
The Merg wrote:Okay, I appreciate all the help, but I hope you guys can bear with me a bit longer. I am not really a PHP coder (mostly code in VB6), so I am not quite sure on the exact implementation although my coding background is floating ideas around in my head as to how this should work. My understanding is that the page would look something like this:

Code: Select all

<php form processing>
Form processing code that does not display anything and creation of headers for e-mail for contact form
</php>

<html>
<body>

<form>
Form fields
</form>

<php error display>
Code that displays output if error is found from form entry
</php>

</body>
</html>
My biggest issue is trying to break apart the current form processing code and being able to pass the values of variables that are created in <php form processing> to <php error display>.

Any help would be appreciated. Even a simple version of the way the code should be would be a help as I am pretty good at adapting current code to my use.

Thanks,
Merg
Sort of. All logic not pertaining to things outside the realm of simplicity like echo $foo, or a loop thereof, should be handled before output has started. This would include the collection of data into a convenient vessel needed to display any errors that may have occurred. Your pseudo-example above would indicate there is validation logic mixed into the output sections.