Armando
Armando

Reputation: 11

how to submit a CFform and change its action attribute after the new page load?

I am trying to have cfform where users can input and search for some result on a different website which requires log in to obtain access to the desire results. The other website will open in a pop up or new page with the user credentials already logged in and will display the results the user previously input in the cfform or another cfform or form I am not ensure how it should be done. Basically I would like to provide a quick way to search results in a different site from my site while skipping the log in progress for users of the site that contains the results.

So far I have a working cfform that opens www.somewebsite.com with the user already logged in that looks like this

<cfform action="https://www.somewebsite.com/login.php" method="post" id="ud_form">
     <cfinput type="hidden" name="email" value="#email#" />   
     <cfinput type="hidden" name="password" value="#password#" />
     <cfinput type="hidden" name="source" value="#source#"  />
</cfform>

<script type="text/javascript">
     document.getElementById('ud_form').submit();
</script>

The problem I am having is that I haven't find a way to change the action attribute after the login process completes. For example, once the log in happens the cfform action attribute should be set to new url + the input values of the cfform (https://www.somewebsite.com/cp.php?addr="+form_address+"&unit="+form_unit) and then be submitted. I have done a ton of research and could not find a solution for this.

I have tried to include cfinput tags inside the above cfform and use a javascript function that will trigger the action attribute change after the form is submitted. But pressing the submit button fail to trigger any action without report any script error in the console. The code I used is shown below.

<cfform action="https://www.somewebsite.com/login.php" method="post" id="ud_form" onSuccess="show_results();">
        <cfinput type="hidden" name="email" value="#email#" />
        <cfinput type="hidden" name="password" value="#password#" />  
        <cfinput type="hidden" name="source" value="#source#"  />
        Address: <input id="address" type="Text" name="address" placeholder="Enter an Address" size=40><br/><br/>
        Unit #: <input id="unit" type="Text" name="unit" placeholder="Enter Unit" size=40><br/><br/>
        <input id="submit" type="button" value="Search">       
</cfform>



<script type="text/javascript">

       $("#submit").click(function(){                                                    

              $("#ud_form").submit();
       });

       function show_results()
       {
              var form_address =$("#address").val();
              form_address = form_address.split(' ').join('+');
              var form_unit = $("#unit").val();
              form_unit =form_unit.split(' ').join('+');
              var url = "https://www.somewebsite.com/cp.php?addr="+form_address+"&unit="+form_unit;                                               
              $('#ud_form').attr('action',url);                                                                          
              $("#ud_form").submit();
       }

Any help or suggestions are greatly appreciated!

Upvotes: 1

Views: 468

Answers (1)

Paul Sturm
Paul Sturm

Reputation: 2138

Once you submit that HTML form, the browser is going to move away from your code in this case - it's going to start interacting with somewebsite.com

I think what you'll want to do is submit the form to yourself and take the form fields and do a CFHTTP with them to www.somewebsite.com, and then using the results returned populate a page/dialog/pop-up with the data you just fetched.

Otherwise, you're just sending the user off to that secondary site and navigating them away from yours.

Upvotes: 1

Related Questions