Page 1 of 1

PHP problems!

PostPosted: 29. March 2007 18:35
by DocBoje
Hi,

First, I just want to say thanks for providing XAMPP!

I am having a problem getting PHP scripts to run if a page calls itself from a submit button. For example, I am writing a guestbook and once the user clicks submit (the form's action attribute is the page itself), PHP code is supposed to run to update the page with the new guest record. However, nothing happens when I click submit (other than all fields being erased). PHP code will run but only the first time the page is loaded. I have tested the program on my school's server and it works the way it should, but that doesn't really help when I am trying to do stuff locally.

I'd appreciate if someone could help me out here!

Thanks in advance!

DocBoje

PostPosted: 29. March 2007 19:53
by Wiedmann
Your code is not correct...

PostPosted: 29. March 2007 20:59
by DocBoje
Hmm...

Well I just wrote a test program to see if it would just run a print statement after submit is clicked and I also included a print statement to run when the page is loaded. As I suspected, the first one ran but the one that is supposed to run after submit is clicked does not.

After testing it locally, I uploaded the program to my school's server and it worked perfectly.

Here is the code:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" dir="ltr">

<head>
<title>Local PHP Test!</title>

<style type="text/css">

div {
  width: 100%;
  display: block;
  text-align: left;
  margin-left: auto;
  margin-right: auto;
}

</style>

</head>

<body>

<div>

<h3> Local PHP Test! :) </h3>

<br />
<br />

<form action = "test.php">

      Name:<br />
      <input type = "text"
             name = "txtName"
             size = "35"
             maxlength = "50" />
      <br />
      <input type = "submit"
             name = "go"
             value = "Submit!" />
      <br />
      <br />
      <hr />

<?

global $txtName, $timeStamp;

$timeStamp = date("F j, Y, g:i a");

print "This PHP code runs fine.  It occurs when the page loads. <br /><br />";

if (!empty($txtName)) {
    $output = "Hello there, <em>" . $txtName . "</em>!!" .
              "<br /><br />Message generated at: " . $timeStamp;
             
    print $output;

}

?>

</form>
</div>

</body>
</html>


I guess if there is a problem with the code, then my next question would be why does it run fine on my school's server but not on my local machine?

Thanks.

DocBoje

PostPosted: 29. March 2007 21:08
by Wiedmann
a) You should use "<?php".
b) Using the function global() in the global script context makes no sense.

and now:
c) You are using two variables ($txtName, $timeStamp) in the string concatenation (and if() statement), but you never set a value for these variables (they are not defined).

You should use a higher error_reporting lever, to find such a easy problem:
error_reporting(E_ALL);

BTW, allways read the PHP manual:
http://de.php.net/manual/en/language.va ... ternal.php

PostPosted: 29. March 2007 23:34
by DocBoje
Ah, I've figured it out. Apparently my school has the register_globals = on, so that's why my code was working on their server and not locally. Would have been nice to know that so I could learn this stuff the right way :)

I see now that the $txtName variable wasn't defined just by belonging to the form. I've rewritten the test program and it runs fine on my local machine.

Here is the code:

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" dir="ltr">

<head>
<title>Local PHP Test!</title>

<style type="text/css">

div {
  width: 25%;
  display: block;
  text-align: center;
  margin-left: auto;
  margin-right: auto;
}

.innerBlock {
  width: 100%;
  display: block;
  text-align: left;
  margin-left: auto;
  margin-right: auto;
}

</style>

</head>

<body>

<div>

<h3> Local PHP Test! :) </h3>

<br />
<br />

<div class = "innerBlock">
<form action = "test.php"
      method = "post">

      Name:<br />
      <input type = "text"
             name = "txtName"
             value = ""
             size = "35"
             maxlength = "50" />
      <br />
      <br />
      <input type = "submit"
             name = "go"
             value = "Submit!" />
      <br />
      <br />
      <hr />

<?php
error_reporting(E_ALL);

$timeStamp = date("F j, Y, g:i a");

import_request_variables("p", "form_");

if (!empty($form_txtName)) {
   $output = "Hello there, <em>" . $form_txtName . "</em>!!" .
             "<br /><br />Message generated at: " . $timeStamp;

    print $output;
}

?>

</form>
</div>
</div>

</body>
</html>


Thanks for your help, Wiedmann!