-1

Below is my PHP code. I think I'm close but I don't understand why when I click the submit button on the form it isn't entering my if (isset($_POST['addFormula'])) statement in my PHP code to perform the insert.

alert('IM IN HERE!!!'); alert('successfully added '); alert('error while adding formula...');

Here is my html code

<table style="width:100%">                  
    <tr>
        <td>
            <form method="post">
                <h3 style="color:#a5a5a5; margin:5px 0 0 0">Name:</h3>
                <input type="text" name="name"><br>

                <h3 style="color:#a5a5a5; margin:5px 0 0 0">Formula:</h3>
                <input type="text" name="formula"><br>

                <h3 style="color:#a5a5a5; margin:5px 0 0 0">Category:</h3>
                <input class="radio-button" type="radio" id="math" name="category" value="Math" checked>
                Math
                <input class="radio-button" type="radio" name="category" value="Physics">
                Physics
                <h3 style="color:#a5a5a5; margin:5px 0 0 0">Description:</h3>
                <textarea class="description-textarea" name="description" ></textarea><br>  

                <tr>
                    <td class="cancel-button"> 
                        <button type="button" id="cancelAdd">Cancel</button>    
                    </td>
                    <td class="save-button"> 
                        <button type="submit" name="addFormula" id="add">Save</button>
                    </td>
                </tr>
            </form>
        </td>
    </tr>                   
</table>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
AMG
  • 117
  • 7
  • Can you share your form code? – Lucky Chingi Oct 19 '15 at 17:33
  • Just updated my code. – AMG Oct 19 '15 at 17:35
  • do you see the popup 'IM IN HERE!!!'? – Lucky Chingi Oct 19 '15 at 17:41
  • 1
    It might be a typo but did you close the php tags cause they are missing at the end ` – Antonio Smoljan Oct 19 '15 at 17:47
  • it never gets to the alert IM HERE!!! I tried to put that in to debug – AMG Oct 19 '15 at 17:47
  • Yes Antonio that was just a typo... so maybe my session isnt active? but it isnt logging out the user and redirecting to the index.php page? – AMG Oct 19 '15 at 17:48
  • 1
    Possible duplicate of [The 3 different equals](http://stackoverflow.com/questions/2063480/the-3-different-equals) – Jay Blanchard Oct 19 '15 at 17:50
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Oct 19 '15 at 17:52
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Oct 19 '15 at 17:52
  • @AMG just remove the if statement that checks for the user and try it out. – Antonio Smoljan Oct 19 '15 at 17:54
  • thanks for the suggestions, I am new to php and mysql... I fixed the comparison operator also – AMG Oct 19 '15 at 17:55
  • Does it work now that you have fixed it? – Jay Blanchard Oct 19 '15 at 17:56
  • Im having trouble testing right now because my connection on my school server wont let me access my cpanel... I will try to ftp in the new code and see – AMG Oct 19 '15 at 18:00
  • 3
    It is not necessary ***or a good idea*** to edit your question with the suggested fixes. Now the comments look ridiculous as the errors no longer exist! The underlying concept of SO is to provide Q&A's that others can use to correct similiar problems. I am going to revert your edits. – RiggsFolly Oct 19 '15 at 18:10
  • I only added an equals sign in my comparison statement to have the proper syntax since the question I asked really isnt based around that. – AMG Oct 19 '15 at 18:21
  • @AMG the alert will never work that way. *Rinse, lather, repeat: JavaScript is client-side, PHP is server-side.* – Jay Blanchard Oct 19 '15 at 19:17
  • Just tried testing without the if statement that checks session user and I still get nothing when the button is clicked. hmmmmm, – AMG Oct 19 '15 at 22:15
  • Thank you everyone for your advise, I fixed the (==) typo and it still wasn't getting into that section of code to fire my test alert. I moved all my code to the homepage where I know the session is good and everything works fine including the test alert and mysql insert. I believe the error existed because the session wasn't correct when i launched the new page that the form was on. – AMG Oct 21 '15 at 15:11

1 Answers1

-1

try this, I added the action element to the form to load the same php page. You can change it to a specific page if the PHP script is not on the same file. Example:

<form method="post" action='action.php'>

<form method="post" action=<?php echo $_SERVER['PHP_SELF'];?>>
Lucky Chingi
  • 2,248
  • 1
  • 10
  • 15