2

SO I am trying to build a very simple (semi) emailing system simply for practice and learning.

SO I have my nav bar with users personal info loading on the body (right side). Inside the nav bar there are clickable links to allow user to compose, read messages etc.

The links are appended with a PHP parameter as follows.

<a href="body.php?action=compose">

In the image below it is just a page load so for debugging purposes I use get not set, since no action has been taken (link clicked)

enter image description here

Now when user wants to compose an email and clicks compose, the following happens:

MY Logic

I get the action user wants to take via parameter appended to url in this case compose, so up comes a form allowing user to compose / sent a message to another user, which looks as follows:

enter image description here

I achieve this by the following code:

if($_SERVER['REQUEST_METHOD'] == 'GET'){
                if(isset($_GET['action'])){
                 //get which action user wants to take in this example compose email
                    $action = $_GET['action'];
                    if($action == 'compose'){
                        ?>
                        <form name="composeMsg" method="post" id="composeMsg" action="">
                        <input type="text" name="receipnt" placeholder="Receipnt" id="from">
                        <div class="break"></div>
                        <input type="text" name="subject" placeholder="Subject" id="subject">
                       <div class="break"></div>
                        <textarea placeholder="Message" name="msgBody" style="width: 350px; height: 200px">
                        </textarea>
                        <div class="break"></div>
                            <button type="submit" name="sendMsg" class="btn btn-danger">SEND</button>
                        </form>
                <?php
                        }
                       //submit button has been clicked
                        if(isset($_POST['sendMsg'])){
                         //execute script to send msg
                        $check = $msgClass->composeMsg('14', $_POST['receipnt'], $_POST['subject'], $_POST['msgBody'], date('Y-m-d') );
                        if($check ==  true){
                            echo '<h1>MSG SENT!</h1>';
                        }
                        elseif($check == false){
                            echo '<h1>OOPS...SOMETHING WENT WRONG!</h1>';
                        }
                    }//isset msgSent

My problem

All I get when sending message is a reloaded screen. No error, No Warnings and no script execution so I really dont know where to start my debugging.

I have tried to create a test page with just the email form and my composeMsg() method, which worked and inserted data into db. But when I implement it into my main page, when submit is clicked page just reloads without any action (as mentioned).

I have tried setting the form method to $_GET instead of $_POST but still same result

Any help appreciated!

Additional INfo

My method for send message looks as follows which is correct:

 public function composeMsg($userID, $toUser, $subject, $msg, $senton ){
        $db = DB::getInstance();
        $sql = "INSERT INTO messages (userID, fromID, subject, body, senton) values (:userID, :toUser, :subject, :msg, :senton )";
        $stmnt = $db->prepare($sql);

        $stmnt->bindValue(':userID', $userID);
        $stmnt->bindValue(':toUser', $toUser);
        $stmnt->bindValue(':subject', $subject);
        $stmnt->bindValue(':msg', $msg);
        $stmnt->bindValue(':senton', $senton);

        $stmnt->execute();

        if($stmnt->rowCount() > 0){
            return true;
        }
        else{
            return false;
        }
    }
Timothy Coetzee
  • 5,626
  • 9
  • 34
  • 97
  • How are you using the composeMsg() method? You're also using a class for this. Reurns will often fail silently because they halt execution. There isn't enough code. – Funk Forty Niner Jul 23 '17 at 10:45
  • Seeing new comments now, error reporting should have thrown you an undefined index error so your system may not be setup to catch and display by default. http://php.net/manual/en/function.error-reporting.php do that, and what does it show you now using your original code? – Funk Forty Niner Jul 23 '17 at 10:48
  • I made a typo in my first comment: *"Reurns"* which should have read as *"Returns"*. So, about error reporting? Again; that should have thrown you an undefined index. – Funk Forty Niner Jul 23 '17 at 10:58

2 Answers2

2

Submitting your form takes you to your page without any GET Requests.

<form name="composeMsg" method="post" id="composeMsg" action="">

And since you wrap your POST Data submission with this:

if($_SERVER['REQUEST_METHOD'] == 'GET'){

It will never get there.

Nisan Bahar
  • 73
  • 1
  • 1
  • 8
2

Issue is when you will click on submit on form POST method will be generated so your condition if($_SERVER['REQUEST_METHOD'] == 'GET'){ not executed. Do following change in your code

if($_SERVER['REQUEST_METHOD'] == 'GET'){
    if(isset($_GET['action'])){
     //get which action user wants to take in this example compose email
        $action = $_GET['action'];
        if($action == 'compose'){
            ?>
            <form name="composeMsg" method="post" id="composeMsg" action="">
            <input type="text" name="receipnt" placeholder="Receipnt" id="from">
            <div class="break"></div>
            <input type="text" name="subject" placeholder="Subject" id="subject">
           <div class="break"></div>
            <textarea placeholder="Message" name="msgBody" style="width: 350px; height: 200px">
            </textarea>
            <div class="break"></div>
                <button type="submit" name="sendMsg" class="btn btn-danger">SEND</button>
            </form>
    <?php
        }
    }
}
//submit button has been clicked
if(isset($_POST['sendMsg'])){
 //execute script to send msg
    $check = $msgClass->composeMsg('14', $_POST['receipnt'], $_POST['subject'], $_POST['msgBody'], date('Y-m-d') );
    if($check ==  true){
        echo '<h1>MSG SENT!</h1>';
    }
    elseif($check == false){
        echo '<h1>OOPS...SOMETHING WENT WRONG!</h1>';
    }
}//isset msgSent
B. Desai
  • 16,414
  • 5
  • 26
  • 47