Hi

A simple problem which at the moment is defeating me.

I wish to create a form with a button that states 'Return to Log In after uploading'. The form has the action 'log_in.php' and correctly returns to that page. The method used is "POST" with a submit type button.

When this button is used I want a record in a database to be updated with a marker that the client has returned to log in after uploading. This uses a MYSql query funtion.

An alternative button on a separate form (but same page) states 'Return without uploading' and does not place the marker in the database.

The problem. Using "if(isset($_POST['submit'])) {" stops the update but allows the form to execute the new page action.

Separating the two pieces of coding and leaving out the "if(isset($_POST['submit'])) {" automatically updates the database when the page opens, not on the button.

Making the form action="", allows the update on the button, but does not move to the new page (obviously as there is no URL).

So there is a conflict and, despite intensive research on the net, I cannot find a way of combining the two actions with one button.

Does anyone have any suggestions how I can achieve this?

Many thanks.

Tony

    Not getting to see any of your code makes it somewhat like guessing the color of your underwear from a phone call but from your description, it sounds like your solution might be solved by simply passing a hidden form element to tell your script which form got processed

    In your upload form:

    <input type='hidden' name='doTheUploadBoogaloo'>

    On the processing side of things:

    if(ISSET($_POST['doTheUploadBoogaloo'])){
        /* Form with the upload feature got processed */
    }
      tonyrhills wrote:

      An alternative button on a separate form (but same page) states 'Return without uploading' and does not place the marker in the database.

      It seems to me that this alternative button with a second form is a glorified link, i.e., you could have replaced it with a link that is styled to look like a button. If so, you really only have one form proper, therefore schwim's suggestion of a hidden field is unnecessary.

      tonyrhills wrote:

      I wish to create a form with a button that states 'Return to Log In after uploading'. The form has the action 'log_in.php' and correctly returns to that page. The method used is "POST" with a submit type button.

      When this button is used I want a record in a database to be updated with a marker that the client has returned to log in after uploading. This uses a MYSql query funtion.

      log_in.php sounds like the wrong place to process this form. Rather, set action="", then process the upload and record the "marker" on form submission. If the form processing is successful, do a location [man]header[/man] redirect to log_in.php.

        Hi

        Thank you for your replies. Clearly, in my attempt to keep my post as brief as possible, I have not explained what I am trying to do, so here is a slightly longer version with coding.

        Our service is to use employee data provided by individual companies to estimate their liabilities under forthcoming pension legislation. To achieve this, when a company visits our site they register and create a unique id.

        To get the data to us, they download from the site an Excel template and complete it with their employee data. This template name has their id in it to prevent others accessing it.

        At this point, their unique record in a MySql database has a marker to say they have downloaded the template. So far, so good.

        Using this marker, when they return to the Log In page, they are given different instructions and can then upload the template with the data using the page in question called, naturally, &#8216;employee_data_upload.php&#8217;.

        I am using phpFileUploader for uploading and this works well. When the upload is complete, the user receives an alert telling them it has been completed successfully. This is generated by a javascript function:

        <script type='text/javascript'>
        
        function CuteWebUI_AjaxUploader_OnTaskComplete(task)
        {			
        alert(task.FileName + " has been successfully uploaded. You should now return using the button below and proceed to your Initial Report.");
        }

        </script>

        Now comes the bit I am struggling with. I wish to place a marker on the database that upload has taken place using the following code:

        // Connect to the database

        $dbc = mysqli_connect('dbxxxxxx.db.1and1.com', 'dboxxxxxx', xxxxxxx', 'dbxxxxxxxx'); (I have replaced the actual database details with xs for security.)

        / check connection /
        if (mysqli_connect_error()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();}

        $query = "UPDATE CBCcompanies SET xlsx_upload='1' WHERE id = $id";

            mysqli_query($dbc, $query); }

        This works fine on its own on the page but, of course, fires when the page is opened, whether or not upload takes place. If the user decides not to upload for the moment and returns using a button to the Log In page, the system now thinks uploading has taken place and will provide the wrong information.

        Ideally this would be in the above javascript function, but I cannot get it to work in this script.

        So I thought I would provide two additional buttons for the user &#8211; one allowing them to proceed after upload (to the &#8216;calculation.php&#8217; page) and one to return to the Log In page without uploading. I am using forms because whichever direction the user chooses, their unique data needs to go with them as I do not want them to go through the log in procedure with their password a second time for the same session, so the coding is:

        <form id="form1" name="form1" method="post" action="calculation.php">
        <input type="hidden" name="excel" id="excel" value="<?php echo '' . $excel . ''?>" />
        <input type="hidden" name="id" id="id" value="<?php echo '' . $id . ''?>" />
        <input type="hidden" name="log_in_company_name" id="log_in_company_name" value="<?php echo '' . $log_in_company_name . ''?>" />

        <div align="center">

        <input type="submit" name="Continue" value="Continue having uploaded your CBC Template" id="Continue" />

        </form>

        <form id="form2" name="form2" method="post" action="log_in.php">

        <input type="hidden" name="excel" id="excel" value="<?php echo '' . $excel . ''?>" />
        <input type="hidden" name="id" id="id" value="<?php echo '' . $id . ''?>" />
        <input type="hidden" name="log_in_company_name" id="log_in_company_name" value="<?php echo '' . $log_in_company_name . ''?>" />

        <div align="center">

        <input type="submit" name="Return" value="No - I do not wish to upload - return to Log In page" id="Return" />

        </form>

        If I place the MySql query in Form 1 or outside it and use

        if(isset($_POST['Continue'])) {

        the query does not take place. If I change the form action to:

        <form id="form2" name="form2" method="post" action=" ">

        the query does take place, but the user is not sent to the next page. So there seems to be a conflict between the MySql query and embedding or linking the action to a form.

        Is there any coding which can achieve my objectives?

        Thanks for help.

        Tony

          tonyrhills wrote:

          Ideally this would be in the above javascript function

          I think that ideally, it would be in the phpFileUploader configuration, if such a thing is possible, i.e., some kind of trigger PHP function to be called when the upload has completed. If this is not possible, then as you say, editing CuteWebUI_AjaxUploader_OnTaskComplete to make an out of band call to a PHP script that would update the database with the uploaded marker would be the next best thing.

          tonyrhills wrote:

          If I change the form action to:

          <form id="form2" name="form2" method="post" action=" ">

          the query does take place, but the user is not sent to the next page. So there seems to be a conflict between the MySql query and embedding or linking the action to a form.

          Is there any coding which can achieve my objectives?

          I have already given you a way: send a location header to perform the redirection to the next page.

            Hi there Tony,

            Can't you just have the part of the fileuploader that deploys the js alert also set a variable to let your form know that you've got an upload?

            JS stuff
            $uploaded = '1';

            in the return page

            if(ISSET($uploaded)){
                /* Present the form for those who have uploaded */
            }

            And I don't mean this as a derogatory comment, but your flow through the process seems very convoluted and hard to follow. With all the different forms, buttons and pages for people doing the same thing but not always including the same data, maybe just a single form with a space to attach a file and on the form processing side if the file_attachment form element is blank, they didn't upload a file and if they browsed and attached, the form knows to deal with the file upload.

            I often find myself doing the very same thing when I'm not sure how to solve a problem. I keep attaching bits that I know how to do and it ends up looking like a patchwork quilt. I'm only mentioning this because this may be a very good chance for you to streamline this whole process.

              schwim wrote:

              Can't you just have the part of the fileuploader that deploys the js alert also set a variable to let your form know that you've got an upload?

              You cannot use Javascript to set a PHP variable like that, since by the time that Javascript code gets executed, the PHP interpreter has long done its job. What you can do with Javascript would be equivalent to say, having a hidden form field, i.e., to change the POST or GET payload to add data, but in this case this does not seem appropriate, hence my suggestion of an out of band invocation of a PHP script.

                I meant the part of the php script that echoes the JavaScript also handle setting up the form elements that are desired.

                  schwim;11046165 wrote:

                  I meant the part of the php script that echoes the JavaScript also handle setting up the form elements that are desired.

                  DW about this, some of us got it 😉

                  Of course you can have PHP output the JS in a view, or just via straight echo'ing, but it is probably best to only do this in one place and only set up variables in a JS object. i.e.

                  window.app = window.app || { "settings": {} };
                  window.app.settings = window.app.settings || {};
                  // now set settings values, you can of course further namespace to group, and add structure
                  window.app.settings.username = '<?php echo Auth::getUserName(); ?>';
                  window.app.settings.department = '<?php echo Auth::getUserDept(); ?>';
                  

                  If you need a button click or event in JS to talk to PHP just set up an appropriate request using XMLHttpRequest, jQuery.ajax / jQuery.post, or similar. This can send data to PHP, generally anything sent to PHP externally should be verified appropriately for security and functionality.

                  If it all gets confusing, just break it down into steps, simplify things already working as needed i.e. render page could be one step, and visualize what happens at each step. Convoluted code is where you do something really clever and forget to break apart the steps and write huge 50-2000 line functions which are a pig to debug, scale and modify... I once worked on a project where some indian and chinese coders over seven years had literally copied and pasted some working code, and changed some variables rather than write a function with arguments... now that gets convoluted fast!

                  Edit: Apologies to laserlight, no offence was mean

                    Again, many thanks for the replies to my problem.

                    Schwim &#8211; I appreciate your comments. I am self taught on PHP (mainly from one book) and have used similar phrases of coding where I know they work, building up a page phrase by phrase and, as you suggest, this can end up with complex lengthy coding. I have often thought that a professional would do it in a simpler manner, but I have no one to guide me.

                    For this current site, I have also learnt two new derivatives of PHP &#8211; PHPExcel and PHPFileUploader. So it has been a steep learning curve!

                    I have decided on the simpler route of updating the database that an upload has taken place by processing this on the next page (calculation.php) which is where the user goes if they confirm upload has taken place. Access to this page can also come from an earlier page in the sequence, but the MySql query will simply overwrite the database with the same information.

                    If a user decides not to upload after all, pressing the return button takes them back to log in and the system does not regard an upload as having taken place. The information provided now reflects this correctly.

                    I hope this works! Of course, a user can always upload the data and then press the button to return to log in, in which case the user will have to go through the uploading procedure again to progress. But then there is only so far you can go to guide users correctly.

                    Thanks again.

                    Tony

                      Write a Reply...