Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: rcane on May 06, 2022, 03:51:11 PM

Title: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: rcane on May 06, 2022, 03:51:11 PM
One the fields on a form is just a number.
Before it inserts that data to the table I'd like to test it for duplicates--and only proceed if there is no duplicate.

I can't seem to find the best place to tuck that without interfering with the page.

I even tried doing a query when the page loads to build an array so the check can be done without an additional query.

any ideas where it can be done on this same page?
Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: Tyrsson on May 06, 2022, 04:03:02 PM
Maybe something like:(sudo code)

$proceed = true (result of query, no duplicate)

if($proceed && isset($_POST)) { do something } elseif($proceed === false) { we have a duplicate }
Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: Tyrsson on May 06, 2022, 04:08:53 PM
As a side note. If this question springs from your other topic regarding file uploads. It would be best to use a unique hash so that you will not have to worry about duplicate entries. Generally you would not want to use an auto incremented identifier to name an upload or to base its identifier on because it will introduce a known or discoverable identifier for the file which causes an attack vector based on filename and known path.
Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: rcane on May 06, 2022, 05:09:35 PM
Quote from: Joey Smithâ„¢ on May 06, 2022, 04:08:53 PMAs a side note. If this question springs from your other topic regarding file uploads. It would be best to use a unique hash so that you will not have to worry about duplicate entries. Generally you would not want to use an auto incremented identifier to name an upload or to base its identifier on because it will introduce a known or discoverable identifier for the file which causes an attack vector based on filename and known path.

I see where you're going. 

Yeah it's related, but there are no auto-incremental.  I'm using user-defined "file_number" to order them.  Because, they're court documents and sometimes you get 3 on the same date come out.  So, you have to pick which is "first".  Easier to just type your own numbers. Plus I'm the only person with access to the form.

I found a script that checks your form for duplicate entries on the fly.  I was looking to see if I could  hijack that to compare to the compare to the array of "file_numbers" I pulled down from the table. 

In a perfect world, if you're entering a "file_number" on the form, it would holler and let you know that number has been taken---all without hitting submit and going through the $_POST process.

The list is long, and although I have it pointing out the most recent number used (so you know the next logical # to assign your document) sometimes a record needs removed which frees up a number. 

This was what I found and will play around with.

function gfg_check_duplicates() {
     var myarray = [];
     for (i = 0; i < 4; i++) {
      document.getElementById("status" + i).innerHTML = "";
      myarray[i] =
      document.getElementById("gfg_field" + i).value;
         }
     for (i = 0; i < 4; i++) {
      var flag = false;
        for (j = 0; j < 4; j++) {
          if (i == j || myarray[i] == "" || myarray[j] == "")
             continue;
          if (myarray[i] == myarray[j]) {
             flag = true;
           document.getElementById("status" + i).innerHTML +=
             "<br>Its identical to the phone number " + (j + 1);
                        }
                    }
          if (flag == false)
            document.getElementById("status" + i).innerHTML = "";
                }
            }
        </script>
    </head>
    <body>
        <h4>Phone no. 1</h4>
        <input id="gfg_field0"
               oninput="gfg_check_duplicates()" />
        <div id="status0"></div>
        <h4>Phone no. 2</h4>
        <input id="gfg_field1"
               oninput="gfg_check_duplicates()" />
        <div id="status1"></div>
        <h4>Phone no. 3</h4>
        <input id="gfg_field2"
               oninput="gfg_check_duplicates()" />
        <div id="status2"></div>
        <h4>Phone no. 4</h4>
        <input id="gfg_field3"
               oninput="gfg_check_duplicates()" />
        <div id="status3"></div>
        <br />
    </body>
</html>
Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: Tyrsson on May 06, 2022, 09:24:25 PM
Just a thought. If they are court docs, why not use the case number? Those are unique are they not?
Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: rcane on May 07, 2022, 03:36:06 PM
Ha, you'd think.  But I've seen that they don't organize them well (depending on how I get my hands on them) with regard to when they come out.  I've got them organized in order of "the back and forth" which is a child-level of "date of filing". 

Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: SpacePhoenix on May 08, 2022, 05:37:17 AM
Shouldn't you be doing the isset before running the query? No point in running the query if a file number hasn't been supplied. Is the file number going to be alphanumeric or numeric?
Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: Tyrsson on May 12, 2022, 09:21:40 PM
Well, if it were me. In this particular situation. I would probably have 2 fieldsets with different ID's in that particular form. One that was ajaxed and only dealt with sorting if the file identifier I wanted to use was currently in use. Since original poster is going to be the only user of this functionality then he doesn't really have to worry to much with server performance etc (too much), and could probably work up something so that it does it in real time as you type (seems like I remember there being a jquery plugin that did that). Which would allow the logic to be very straight forward once the form is posted.
Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: Dhayzon on May 12, 2022, 11:07:01 PM
find value on database if return empty proceed  ... there is no other method


Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: Tyrsson on May 13, 2022, 10:33:42 PM
Quote from: Dhayzon on May 12, 2022, 11:07:01 PMfind value on database if return empty proceed  ... there is no other method
Do you even take the time to read what you reply too? I seriously doubt it.
Title: Re: Can you interrupt 'isset($_POST)' with a conditional before it proceeds?
Post by: rcane on May 20, 2022, 03:16:06 PM
Ok, got it squared away.

Yeah, I ran a query that grabs all the file numbers and dumps it into an array.

I then added a script with some event listeners like onblur to make sure what is entered as the next "file number" doesn't already exist.  Just a little flag and some red in the field to let me know I shouldn't have typed that.

Thanks to all the suggestions.