Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Weird Form Formatting & Redirect Problem

  • This topic is empty.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #160843
    MBM
    Participant

    I have connected to my database to edit stored records and I’ve used a variable for the errors.

    1. When I try to submit with empty fields it redirects and displays the changes have been saved message instead of displaying empty fields message

    2. The formatting is a mess until I click submit or put a dummy echo statement in after data is fetched from the database, once the page redirects (to the same page) everything displays fine.

    I’m sure there is something wrong with the structure of my loop and I am putting something in the wrong place?

    This is what the form looks like before submitting data, with the formatting a mess :

    http://mbmitservices.co.uk/pre_submit.png

    And once the form has been submitted everything displays as I styled it :

    http://mbmitservices.co.uk/post_submit.png

    I have removed the css but it didn’t resolve the issue.

    https://gist.github.com/gyprosetti/3e816b47cb5d52008658

    #161180
    thekeyinfosys
    Participant

    if you are using php it won’t redirect if there is header already passing before the redirection code.. contact me if you want it to be fixed…

    #161198
    MBM
    Participant

    Yes I want it fixed. I have made changes to the code. If I try to submit data with any fields empty they default to the previous value which is better than saving empty records but still no error message.

    https://gist.github.com/gyprosetti/3e816b47cb5d52008658

    #161204
    thekeyinfosys
    Participant

    can you contact me here

    #161216
    __
    Participant

    When I try to submit with empty fields it redirects and displays the changes have been saved message instead of displaying empty fields message [emphasis added]

    if you are using php it won’t redirect if there is header already passing before the redirection code.. contact me if you want it to be fixed…

    @thekeyinfosys, I think you misread the original post – he doesn’t say anything about his page not redirecting (in fact, he mentions twice that it does redirect). His question was about formatting and getting his error message to display.

    @MBM, is this the error message you’re talking about?

    $message = "Please enter data in all fields" ;
    header("Location: edit.php?message=$message");
    

    It’s possible that your script is continuing to execute after you set the Location header, and the message and header are being overwritten a little further down the page. You should always exit; immediately after you redirect.

    Let me know if this solves the problem.

    As for the formatting issue, you’d need to share the code where the message is actually displayed.

    ~~~~~~~~~~~~~~~~~~~~~~~

    Two other suggestions:

    1) Instead of

    $forename = $_POST['forename'];
    //  etc..
    

    check if the POST field exists, and assign a default value if not. This will avoid errors in the case where the page is visited without a form submission.

    $forename = isset( $_POST['forename'] )?
        $_POST['forename']:
        null;
    

    2) Location headers require a fully qualified URL.

    header("Location: edit.php?message=$message");
    

    …will work in most browsers, most of the time. But it should be in the form

    header("Location: http://example.com/edit.php?message=$message");
    
    #161236
    MBM
    Participant

    I have an exit after the changes have been saved into the table. I will try your suggestions and let you know how I get on.

    #161248
    MBM
    Participant

    I’ve made this change :

    $forename = isset( $_POST['forename'] )?$_POST['forename']:null;

    Do I then need to change this?

    if (empty($forename) || empty($surname) || empty($username) || empty ($password) || empty ($email))
    {
    $message = "Please enter data in all fields" ;
    header("Location: edit.php?message=$message");
    }
    else
    {

    As I’m still having the empty field defaulting to the stored value on submit despite using isset.

    I’ve fixed the formatting issue.

    gist updated.

    #161249
    __
    Participant

    Do I then need to change this?

    if (empty($forename) …

    No.

    I have an exit after the changes have been saved into the table.

    When there is an error, you’re not saving any changes, are you?

    I’m still having the empty field defaulting to the stored value on submit despite using isset.

    This is because you still haven’t exited after setting the location header. Your script is continuing to run instead. When you do refresh, you query the database again and get the stored values.

    #161250
    MBM
    Participant

    When there is an error, you’re not saving any changes, are you?

    It shouldn’t but it does. I have an if statement which checks for errors followed by an else statement which writes the data into the table.

    This is because you still haven’t exited after setting the location header. Your script is continuing to run instead. When you get down to the html portion of the page, your variables ($forename, $surname`, etc.) still have their values from POST.

    The exit is after the data has been submitted?!?!!

    The purpose of the page is to allow users to edit data. The HTML displays the fields that have been retrieved from the database. It works apart from the empty field error not displaying so it must be largely correct?!

    #161251
    __
    Participant

    All right; start to finish, here’s what I see your script doing:

    <?php
    if( /* form was submitted */ ){
    
        // get form data from $_POST
    
        if( /* any fields are empty */ ){
    
            // make error message
            // set Location header
    
            // RIGHT HERE, there should be:
            exit;
            // otherwise, the script will keep running.
            // your message, and location header, 
            //    will be overwritten a few lines down.
        }
        else{
            // update the database
        }
    
        // make success message
        // (this OVERWRITES your error message above)
    
        // set Location header
        // (this OVERWRITES your location header above)
    
        // exit;
        // script stops here, everything is sent to the user,
        //    who is redirected with the "success" message.
    
        // wouldn't it make more sense to do all this 
        //    inside your else{} block,
        //    once you know the database 
        //    was updated successfully?
    }
    // regardless of whether the form 
    //    was submitted or not:
    
    // query the database
    if( /* query was successful */ ){
    
        // set variables from query result
    }
    ?>
    
    <!-- display your html form.
        also, note you are trying to use the variables 
        from your query above,
        even though you don't know if they 
        were created successfully or not. -->
    

    See what I mean about your error message never being seen? If you submit the form, you will always see the success message.

    #161252
    __
    Participant

    When you get down to the html portion of the page, your variables ($forename, $surname`, etc.) still have their values from POST.

    The exit is after the data has been submitted?!?!!

    Sorry: you replied to that before I caught my mistake and corrected myself. Please see my post above for a better explanation.

    #161253
    MBM
    Participant

    Tsk. I didn’t think it was possible to have more than one exit statement. It’s working 100% now. Thank you. What a bloody headache.

    also, note you are trying to use the variables
    from your query above,
    even though you don't know if they
    were created successfully or not.

    The data has already been written into the table from another page, this page retrieves and edits that data e.g.

    $id=429;

    Pulls data relating to record #429. I want to use a session but I’m not sure how to approach that so for testing purposes wanted to make sure I could read and write the data before moving on.

    #161258
    __
    Participant

    The data has already been written into the table from another page

    Yes, no doubt. But if your query (the SELECT on this page) fails, then those variables won’t be defined. You never check if the query is successful, so you’d get errors.

    It’s working 100% now. Thank you.

    Great, glad to hear it.

Viewing 13 posts - 1 through 13 (of 13 total)
  • The forum ‘Back End’ is closed to new topics and replies.