POST method only working with empty action - forms

I am trying to send a form via post-method, and with the action
action="<?php echo $_SERVER['PHP_SELF'];?>"
but it is only working if i leave the action blank. This is still reloading the page, just as PHP_SELF. But could this lead to any problems?
Btw, when I use get-method, the action can be PHP_SELF.

The problem is $_SERVER['PHP_SELF'] returns the name of script. You probably mean $_SERVER['REQUEST_URI']. It is working with actoin because most of browsers will submit it to the same page when there is no action attribute.

Related

Dynamically loading content through Groovy server page upon form submit

I have a Groovy project (vanilla; no Grails) with an index.gsp that takes form input from the user and sends it in a POST request to a Groovy script. The form is set up like this:
<form action="somewhere" method="POST" enctype="multipart/form-data">
// some other inputs
<input type="submit"/>
</form>
Is there any way (ideally not using Javascript) to dynamically load content on the same page after the user submits? Redirecting to another GSP might also work. Just something simple, like a string containing whatever the user typed. It seems like Grails has plenty of options, but unfortunately I can't use it.
As you mentioned, Grails is capable of doing what you need without any complex code. Since you can't use it, you will have to use JQuery(Javascript) to make an AJAX call. AJAX is the he only way that I know to achive that.
Just make an AJAX call to your groovy script. JQuery.ajax has a success function to be called if the request succeeds. You can use it to update a hidden dive after the form. This success function has the data returned from the server as an argument, that data could be the string containing whatever the user typed. In that case just add the data to the hidden div and then make that div visible.
function onSucceed(data) {
$('#hiddenDivToUpdate').text(data);
$('#hiddenDivToUpdate').show();
}
You can learn about JQuery.ajax() in this link AJAX

IE form action URL issue

Recently i am started tuning our products to IE compatability. Now i am facing a weird problem in IE alone.
My form url is something like this https://x.com/formurl/dynamicvalue
and my form element is
<form action="" method='post'>
...
</form>
some values the dynamicvalue holds are ( Alphanumeric characters )
plan
plan2
1234443
544
Except IE every other browsers sending the actions to https://x.com/formurl/dynamicvalue
IE form action is sending to https://x.com/formurl
I don't know why this is happening, I can replace the document.URL to post the Form back to solve the problem. Still, i want to what's the reason for IE to remove that dynamicvalue
I am testing in IE-9
Kindly someone teach me.
Thanks in advance.
I have also discovered this bug in Internet Explorer 11 when reading the action attribute of a form with action set to the empty string.
<form action="" method="post"></form>
If one reads the form.action attribute in javascript and the current URL does not contain a trailing slash, the last part of the URL will be removed. I.e., if the location is example.com/xxx/yyy, form.action=="example.com/xxx, while if location is example.com/xxx/yyy/, form.action=="example.com/xxx/yyy/.
However, if the form is posted by clicking a submit button, it is posted to the correct URL, i.e., example.com/xxx/yyy or example.com/xxx/yyy/.
I overcame this by using jQuery's attr function to check if action="" in the HTML and then I use location.href instead.
if($(form).attr('action') === '') return location.href else return $(form).attr('action')
(Why would someone do this? I am intercepting the form submit and using ajax to send the data. To do this, I need to know where the form wants to submit)

redirect to custom php file after custom contact form is submitted in Magento

I made a CMS page to include a form I created but want the form action to send to /template/contacts/report.phtml
Currently I have this in my form but I know its not right.
<form action="report.phtml" id="contactForm" method="post">
Thanks for helping!
Magento has built in magento standard contact form. You can access it from browser www.yourdomain.com/contacts
That's managed by the controller in app/code/core/Mage/Contacts/controllers/indexController.php and the function called
public function indexAction()
and the action on the form is in http://www.yourdomain.com/contacts/index/post/ which is managed by the same controller and a function called
public function postAction()
If your aim to have completely custom form (by adding extra fields), the Magento way, you need to create an extension and have your own controllers that you can access it from browser.
or
the hacking way, you can create new function called customAction() with the same code with postAction(). you can copy app/code/core/Mage/Contacts/controllers/indexController.php and create new file in app/code/local/Mage/Contacts/controllers/indexController.php
and copy the whole
public function postAction(){
until the close of the function.
}
And paste it and rename the function customAction(). And if you want customise redirection. you can change the code inside your customAction()
Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
$this->_redirect('*/*/');
to
Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
$this->_redirect('http://yourdomain/redirect_url');
And the action on your form you can pointed to http://www.yourdomain.com/contacts/index/custom/
Hope that's help.

How would you add a step in the AccountController of Magento?

I need to add a tracking code to a Magento template, pretty simple, but I need it on the confirmation page when a user register, but when it does, he's redirected to the index page, as any user who log in.
I think to add a step by override the account controller because there no such configuration (or I wasn't able to find it), but even if it's just a method override of confirmAction to handle the redirect, it doesn't look like the best way, because this redirect is called several time depending on several things and add session message.
Any ideas?
The only real way I know that you could do this would be to hook into the controller_action_postdispatch_customer_account_createPost event. If you don't know how to do that, check out this Wiki page.
In your observer, do something like this:
// Store a simple boolean that flags this user as just having registered
Mage::getSingleton('core/session')->setUserJustRegistered(true);
Then, in any footer template, do something like this:
<?php if (Mage::getSingleton('core/session')->getUserJustRegistered()): ?>
<!-- INSERT TRACKING CODE HERE -->
<?php Mage::getSingleton('core/session')->setUserJustRegistered(null); ?>
<?php endif; ?>
Note that we reset our session variable back to null so that your tracking code doesn't end up firing on every page.

How to clear the form when the user doesn't provide valid username/password

I designed a form as follows:
User Name: _______________
Password: _______________
Login
I also use jQuery Form Plugin to submit the form to the server side.
It will return if the server script finds some errors. The data returned by server is in JSON format. I would like to know how I can reset the user name + password when I know the username/password is invalid in a decent way.
In other words, I can manually use jQuery to empty the username/password field if the returned result indicates a failure. In fact, I am looking for a decent way built in Form Plugin or sth else that can do this part me for automatically. The only thing I have to do is to set a flag so that if the submission is failed, then the form will be resetted.
Thank you
You cam simply do:
$('#form_id').reset();
I don't think you need a plugin for such simple task. You simply call above code based on the response.
Run this.form.reset() when a form button (e.g. Reset) is being pressed.
e.g.
<form>
...
<input type="button" value="Reset!" onclick="this.form.reset();">
</form>