Welcome Guest, Not a member yet? Register   Sign In
Invoke Form submit automatically from within controller
#1

[eluser]tkaw220[/eluser]
Hi,

I have a case where I need to invoke form action from Controller.

Said I have a page for customer to review their order summary, when they click on the "Place Order" button, the browser post some hidden information to a function in my controller which tell model to insert information into database, if data injection completed successfully then the Controller invoke the form action automatically and post those posted hidden information from last form to internet payment gateway.

Any example how I could achieve this?

Billion thanks.
#2

[eluser]Akinzekeel[/eluser]
If I understood you correctly, you could do it like this:
- Post your stuff to the controller via AJAX (jQuery javascript Framework offers easy handling of this)
- When the AJAX request is complete (i.e. when your data was inserted) run a javascript function which submits the form

Here's an example with jQuery. It will show the order summary and when your customer clicks "Place order" it will trigger your controller function which does some database insert. After that request is completed it will submit the actual <form> to a different controller function.

Code:
<script>
function doInsert() {
$.post("http://example.com/controller/function/", {myData1: "I am ready!", myData2: "some more data!"}, function(a,b,c) {
// this will execute AFTER your controller has finished execution and has delivered some output
document.myFormName.submit(); // submit the form
});
}
</script>

<p>Your order summery here...</p>

&lt;button onclick="doInsert()">Place order!</button>
&lt;form method="post" action="http://example.com/controller/myPaymentFunction" name="myFormName"&gt;
&lt;input type="hidden" name="submitdata" value="some data for 2nd POST"&gt;
&lt;/form&gt;
#3

[eluser]tkaw220[/eluser]
Hi sHiRoKKo1337,

Thank you for the sharing. I had found another way to accomplish this by using cURL (but your server must support openSSL). There is a simple but powerful CI library in Wiki for this task. Due to the need to post sensitive information to payment gateway for processing, I prefer to have all the hidden information pass to gateway in backend, where customer won't see it even if they view my page from source.




Theme © iAndrew 2016 - Forum software by © MyBB