10 replies
I have a script that inserts data once the submit button is pressed - once it's submitted the form actions itself basically I have an if (isset($_POST['submit'])) running and if it's not meaning form wasn't submitted it's ignored if it is then it runs the code and inserts the data

2 questions about this;

1) how do others feel about setting the form to action itself rather than an external fiel

2) problem I seem to be having is that the data isn't being cleared from the form so when I'm refreshing the same info is being entered into db over and over... I tried using exit() that don't work cause then the rest the screen is messed up - I tried dumping the varibles that also didn't work unless I'm doing it wrong

thanks
#php #question
  • Profile picture of the author solidsoul
    I got info on a PRG pattern but I can't figure out how this is used would I redirect in the IF statement? under the insert or?
    {{ DiscussionBoard.errors[6006877].message }}
  • Profile picture of the author Big Squid
    If you're refreshing from a submission, typically the browser will prompt you if you want to send the same data. If you say yes, than it's as though the $_POST['submit'] is set again.

    You may want to build a query to check to see if duplicate values are being submitted.

    I use self-referencing forms all the time.
    {{ DiscussionBoard.errors[6007033].message }}
  • Profile picture of the author aisdbuilder
    Hi Solidsoul,

    In answer to your questions:

    1) how do others feel about setting the form to action itself rather than an external field

    This is fine and a good practice to keep your file count down.

    2) problem I seem to be having is that the data isn't being cleared from the form so when I'm refreshing the same info is being entered into db over and over... I tried using exit() that don't work cause then the rest the screen is messed up - I tried dumping the varibles that also didn't work unless I'm doing it wrong

    After submitting the post, you can check if the db insert was successful by using the mysql_affected_rows() function with php. If the db insert was successful, you can clear the POST array. You can also give them a cookie that signals a db insert was already completed. Then only allow posts if that particular cookie doesn't exist (even on refreshing).

    I hope that helps.
    {{ DiscussionBoard.errors[6007691].message }}
  • Profile picture of the author Earnie Boyd
    It's going to depend on what your data is as to what you decide to do. If it is a post such as a forum comment then you can do like Warrior forum and limit the submissions per session to once every 30 seconds. If it is data such as name, address, etc then you can limit the input based on your database contents. If the data exists already give an appropriate message and ask the user to confirm the message whose action is to display the front page or some other appropriate page.
    Signature
    {{ DiscussionBoard.errors[6008291].message }}
  • Profile picture of the author solidsoul
    heres what I'am already doing and I have stopped the duplicate content HOWEVER on refresh I always now get the Sorry this is already in the DB message.. so it's always trying to post the data that I'm assuming is stuck in POST var

    PHP Code:
     = mysql_query("SELECT * FROM `clients` WHERE sin=");
                if (
    mysql_num_rows() >0 ) {
                    
        echo 
    "<div align='center'><font color='red'>That User Is Already Assocaited With An Account Please Search Our Listings Idiot.</font></div>";
                }
                    else {
    = 
    mysql_query("INSERT INTO `clients` (fname,lname,sin,phone1,phone2,street_address,city,prov,postal,email,home_status,home_long,home_payment,home_holder,bank_mortgage,home_value,home_owe,employer,e_address,e_phone,e_howlong,e_wageperhour,e_hoursperweek,e_status,insurance_company,i_phone,i_policy,stock_num,bank_approval) VALUES ('','','','','','','','','','','','','','','','','','','','','','','','','','','','','')");
                    }
         if () {
                    echo 
    "<div align='center'><font color='red'>Listing added Successfully, Thanks</font></div>";
                    echo 
    "<br />"; 
    {{ DiscussionBoard.errors[6008336].message }}
  • Profile picture of the author Big Squid
    I'm confused on what you mean by refresh. Are you saying that on the first load, or a refresh of the browser after you hit submit?
    {{ DiscussionBoard.errors[6011101].message }}
  • Profile picture of the author ussher
    2 questions about this;

    1) how do others feel about setting the form to action itself rather than an external fiel

    2) problem I seem to be having is that the data isn't being cleared from the form so when I'm refreshing the same info is being entered into db over and over... I tried using exit() that don't work cause then the rest the screen is messed up - I tried dumping the varibles that also didn't work unless I'm doing it wrong
    These are not 2 questions, its the same question asked in 2 different ways.

    The answer is "Redirect after a successful database insertion. You should always be doing this, its good design practice."
    Signature

    "Jamroom is a Profile Centric CMS system suitable as a development framework for building entire communities. Highly modular in concept. Suitable for enterprise level development teams or solo freelancers."

    - jamroom.net
    Download Jamroom free: Download
    {{ DiscussionBoard.errors[6011282].message }}
    • Profile picture of the author Earnie Boyd
      Originally Posted by ussher View Post

      These are not 2 questions, its the same question asked in 2 different ways.

      The answer is "Redirect after a successful database insertion. You should always be doing this, its good design practice."
      While you are correct, planning for data resubmitted must also be taken into account due to slow responsive client browsers. It may be that a client presses the submit button more than once or refreshes the browser before the redirect action takes effect on the client browser.
      Signature
      {{ DiscussionBoard.errors[6013583].message }}
  • Profile picture of the author mopsyd
    It sounds to me like you may be stumbling across an autofill issue with the browser itself. I had a similar problem when writing a login page not too long ago. You can use javascript to clear the form fields using an onload function.
    {{ DiscussionBoard.errors[6015656].message }}
  • Profile picture of the author drmadcow
    As a quick tip I'd change the line:

    if (mysql_num_rows() >0 ) {

    to

    if (mysql_num_rows()) {

    remember an if statement will fail with a 0 / false. That is also some sloppy coding you got going on there I'd recommend spending some time going through php.net and looking at mysql insert / query coding examples. I always use a pair of variables when doing a mysql query $my_query_variable_name_q and $my_query_variable_name_r so when I look at things like mysql_num_rows() I can see which result set the response reflects.
    {{ DiscussionBoard.errors[6015756].message }}

Trending Topics