2

I have created a form in HTML and a processing script in PHP. When I hit the send button, nothing happens.

My HTML Form:

            <form method="post" action="../core/code/php/notes.php" id="sendNote">
            <input type="hidden" name="sender" value="user">
            <ol>
            <li><label for="sendto">Send to:</label></li>
            <li>
                <select name="sendto">
                    <option value="admin">admin</option>
                    <option value="bnguyen654">bnguyen654</option>
               </select>  
            </li>
            <li><label for="message">Message:</label></li>
            <li><textarea name="message" style="font-family:'Charter BT';"></textarea></li>
            <li><input type="button" value="Send" name="send"></li>
            </ol>             
            </form>

My PHP Code:

<?php 
$to = $_REQUEST['sendto']; 
$from = $_REQUEST['sender'] ; 
$message = $_REQUEST['message'];

if($message == '') {echo '<script type="text/javascript"> alert("You have not entered a message, please go back and try again"); history.go(-1)</script>';} 
else { 
$file = "../../../Admin/%to/notes.txt";
$fh = fopen($myFile, 'a') or die("Can't find file");
$stringData = "%message ~%from";
$send = fwrite($fh, $stringData);
fclose($fh); 

if($send)
    {echo '<script type="text/javascript"> alert("Message sent successfully"); window.location={"../../../Admin"};</script>';} 
else {
{echo "We encountered an error sending your message, please try again."; } 
}
}

?>

The send button does nothing. No success message or error. This is really frustrating. I do tend to overlook things thought so...

  • This is on a server
  • The links are correct

  • I know it's a lot to go through

  • Any help would be appreciated

Thanks, ~BN

2 Answers 2

3

There are two errors that I can see:

<input type="hidden" name="sender" value="login">
                                         ^ Needs quotes around the value

and

<input type="submit" value="Send" name="send">
              ^ Change this to Submit from send.

In HTML there is no form input with a type of "send", the submit type however submits the form when pressed.

Edit (As per comment below):

If you aren't getting into the part of the script that is echo'ing out some javascript, it is because you aren't writing to the file. I have checked the echo statement, and it works perfectly well. This means that either you aren't able to write to the file - fwrite() returns a false if it hasn't written to the file. Also, if it cannot open it properly - ie, fopen() failed, then it will also return a false.

Looking at your code some more, you name the file you want to update $file but then refer to it as $myFile in the fopen() command.

$file = "../../../Admin/%to/notes.txt";
$fh = fopen($myFile, 'a') or die("Can't find file");

You should probably be using the same variable eh?

$myFile = "../../../Admin/%to/notes.txt";
$fh = fopen($myFile, 'a') or die("Can't find file");

Edit 2: To test a connection after an fopen() you can do the following:

$fh = fopen($myFile, 'a') or die("Can't find file");
if($fh)
{ echo "Connection to file is okay";}
else { echo "File couldn't be opened it seems.";}

It is basically the same thing you are doing with your $send variable right to the end of your published code.

Sign up to request clarification or add additional context in comments.

10 Comments

It's successful but for some reason the alert box doesn't pop up.
@BrandonNguyen Made an edit to the answer, while I am happy to help, be sure to carefully step through your code - you should be able to find mismatched variables yourself - though I do get that sometimes we simply miss them.
@BrandonNguyen Okay, can you pass some extra information? Is it writing to the file? Can you test to see whether $fh returns true after the fwrite()? Need some extra information, rather than "doesn't work" mate :)
Um.. I'm sort of a beginner so how would you do that?
@BrandonNguyen I made another edit. Can you have a play with that one and try to track down where the error is happening?
|
1

You have the wrong input type. The button input type is used for an actionless button that you can attach javascript to. The submit type is for submitting a form.

2 Comments

Is your script tag with the alert call actually getting echoed?
Did you call var_dump($send) to see exactly what is happening as a result of your fwrite call?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.