0

UPDATE: I added the action "index.php" to the form and it worked fine. Noob mistake but working now.

I've been wrestling with this for a few days and I must be missing something simple because I had it working before.

I have the following (simplified) code to upload a file and save some data, but when I submit the form, the page just reloads. This is a snippet of a bigger chunk of code, but everything else works just fine. I suspect it's something to do with the POST. Can anyone see where I'm going wrong?

$node = 'pubs';

// if url contains ?action=add&node=pubs
if ( $_GET['action'] == 'add' && $_GET['node'] == $node ) {

    echo '<h2>Add New</h2>
    <form action="" method="POST" enctype="multipart/form-data">

    <label for="file">File</label>
    <input type="file" name="file" />

    // some other form fields

    <input type="submit" name="add_new" value="Add It" />
    <input type="hidden" name="node" value="'.$node.'" />
    </form>';

// if submit button was pressed for this node (pubs)
} elseif ( isset($_POST['add_new']) && $_POST['node'] == $node ) {

    echo 'Success!';

    // some file and mysqli functions

}
robotsmeller
  • 345
  • 2
  • 4
  • 13
  • I suspect there's an issue in some other part of your HTML that you aren't displaying here. I see nothing wrong with your code, it should work. A bit nitpicky but I'd set your form action explicitly. – Cfreak Sep 26 '14 at 13:59
  • Odd how every other piece of the code works flawlessly (using GET), as soon as I add POST for the upload, it stops playing nice. – robotsmeller Sep 26 '14 at 14:00
  • This is because you have not specified the url for the form. That means you have `$_GET` variables `action` and `node` in the form url, so the script never gets in the second option. So, for either `GET` or `POST`, it will go into the first option. – machineaddict Sep 26 '14 at 14:01
  • Try adding the url for the action part. Maybe working with both $_GET[] and $_POST[] is messing it up. more info [here](http://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a) – tfrascaroli Sep 26 '14 at 14:02
  • @machineaddict, you go it. It was indeed the missing action. Thanks! – robotsmeller Sep 26 '14 at 14:03
  • @Damien Overeem: I wouldn't recommend the $_REQUEST variable for beginners, as it overwrites data and is ambiguous. – machineaddict Sep 26 '14 at 14:05
  • .. @machineaddict And that is exactly why I didn't .. Read better buddy :) That comment was a response on someone's comment to use _REQUEST. I'll delete my comment though, since the original comment seems to have been removed. – Damien Overeem Sep 28 '14 at 13:52

0 Answers0