RecursionIsSexy
RecursionIsSexy

Reputation: 31

PHP Problems With Web Page (Trying to Append & Process Form Input)

I've been working on a web page this entire weekend, and I'm just having some real issues with PHP. This is the first web page in which I've used PHP so it's all very fresh and vague right now.

I have my page showing correctly, but my problem now is that my PHP doesn't seem to want to call my processForm() function when I submit the page. In fact, when I click Submit, I get a 403 Forbidden error. I'm using WAMP, and I've tried to change the httpd.conf to Allow for All with no success.

Also, besides the fact that processForm() is apparently not happening, I want to take my form data (name, email, and the checkbox options) and put the info into a file. I want to append to the file each time someone visits the page. Also, I have to use an exclusive file lock. I have written a function for this, but apparently the file isn't even being created.

If you wouldn't mind looking over my processing function and its call, as well as my writeFile() functions, I would greatly appreciate it. This is all so new to me, and I really would like to figure this out. Thanks in advance for any help/knowledge you can pass on!

I am just showing you my index.php file as all my other is just formatting specific and Javascript validation, which I know is working.

<?PHP
require("header.php");

require("content.php");

if (isset($_POST['_submit_check']))  {
echo processForm();
}
else  {
echo displayForm();
}

// ------------------------------------------------
function processForm()  {    
if (isset($_POST['option']))  {
    $_POST['option'] = implode(', ', $_POST['option']);
}

echo "Your Name:  {$_POST['name']}<br />";
echo "Email Address:  {$_POST['email']}<br />";
echo "Area(s) of Study:  {$_POST['option']}<br />"; 
}

function displayForm()  {
$htmlblock = '
    <form name = "contact" method = "POST" action="<?php echo $_SERVER[PHP_SELF]?>">                
    <div class = "appearance">
    *All fields required.*<br /><br />
    Name: *  <input title="Name required!" type = "text" id = "name" name = "name"        required autofocus onchange = "validName();" size = 30>
    <br />
    Email: * <input title="Email address of form username@domain required!" type =   "email" placeholder = "[email protected]" id = "email" name = "email" 
    required onchange = "validAddress();" size = 30>
    <br /><br /><br />
    Areas of Study <br />
    Check the program(s) in which you are interested in. (*Check AT LEAST one.*) <br />
    <input type = "checkbox" id = "cs" name = "option[]">Computer Science<br />
    <input type = "checkbox" id = "cis" name = "option[]">Computer Information Systems<br     />
    <input type = "checkbox" id = "ddep" name = "option[]">Dual Degree Engineering    Program<br /><br /><br />
    <div class = "center">
        <input type="submit" value="Submit" onClick = "return validOption()" onSubmit =     "writeFile()" onSubmit = "processForm()">
        <input type="reset" value="Clear">
    </div>
    <input type = "hidden" name="_submit_check" value="1"/>
    </div>
    <br />
    <br />
    </form>'; 
    return $htmlblock;
}

function writeFile()  {
$file = 'PreviewDayInfo.txt';
$name = $_POST['name'];
$email = $_POST['email'];

if (isset($_POST['option']))  {
    $_POST['option'] = implode(', ', $_POST['option']);
    $options = $_POST['option'];
}

$write = $name + ", " + $email + ", " + options + "\n";
file_put_contents($file, $write, FILE_APPEND | LOCK_EX);
}


require("footer.php");
?>

Upvotes: 0

Views: 92

Answers (1)

TheStoryCoder
TheStoryCoder

Reputation: 3640

You have put PHP code inside a normal string. The part <?php echo $_SERVER[PHP_SELF]?> is not being processed but just returned to the browser as is. The first few lines of $htmlblock needs to be changed to:

$htmlblock = '<form name = "contact" method = "POST" action="'. $_SERVER[PHP_SELF] .'">                
    <div class = "appearance">

Also, you shouldn't have spaces before and after the equal-sign in your attributes. Eg. write like this: name="contact" instead of name = "contact".

Upvotes: 1

Related Questions