- This topic is empty.
-
AuthorPosts
-
January 20, 2014 at 3:29 pm #160843MBMParticipant
I have connected to my database to edit stored records and I’ve used a variable for the errors.
-
When I try to submit with empty fields it redirects and displays the changes have been saved message instead of displaying empty fields message
-
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.
January 27, 2014 at 12:29 am #161180thekeyinfosysParticipantif 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…
January 27, 2014 at 6:22 am #161198MBMParticipantYes 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.
January 27, 2014 at 7:48 am #161204thekeyinfosysParticipantcan you contact me here
January 27, 2014 at 8:51 am #161216__ParticipantWhen 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");
January 27, 2014 at 10:49 am #161236MBMParticipantI 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.
January 27, 2014 at 6:31 pm #161248MBMParticipantI’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.
January 27, 2014 at 7:07 pm #161249__ParticipantDo 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.
January 27, 2014 at 7:21 pm #161250MBMParticipantWhen 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?!
January 27, 2014 at 7:40 pm #161251__ParticipantAll 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.
January 27, 2014 at 7:51 pm #161252__ParticipantWhen 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.
January 27, 2014 at 7:53 pm #161253MBMParticipantTsk. 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.
January 27, 2014 at 10:56 pm #161258__ParticipantThe 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.
-
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.