Javascript/HTML Form field naming issue - php

Consider the following simple form:
<form method="GET" action="handle.php">
<input type="hidden" name="action" value="search">
</form>
Form submission is performed by Javascript (iui) in an ajax call. All fields are properly gathered from the form. Javascript then wants to send the ajax call to "form.action".
This is where my problem starts. The object form is of type HTMLFormElement. The action property of the form is supposed to be of type string and should contain "handle.php". After some hours of debugging, I noticed that form.action is now of type HTMLInputElement.
My question:
Is this proper Javascript behavior? I would have never though that defining a form field with the name of a form attribute, this would happen. In the mean time I solved the issue by naming my field differently.
Thanks in advance for any advice...
Found an easy way of displaying my problem. First the form with the problem:
<form action="test.php">
<input type="hidden" name="action" value="test">
<input type="button" onclick="alert(this.form.action);">
</form>
And the form that is proper:
<form action="test.php">
<input type="hidden" name="NOT_AN_ATTRIBUTE_NAME" value="test">
<input type="button" onclick="alert(this.form.action);">
</form>
In the first, the popup states "[object HTMLInputElement]", in the second: "http://localhost/test.php".

The issue you're seeing is because forms are special in JavaScript. All their fields are accessable as properties, so when you use this.form.action, it's getting the field action, not the HTML attribute action="test.php".
Try changing alert(this.form.action); to alert(this.form.getAttribute('action')) instead.

It seems as bug. Maybe there should be an array for 'action '
<form action="test.php">
<input type="hidden" name="action" value="test">
<input type="button" onclick="alert(this.form.action[0]);"> //the form action
<input type="button" onclick="alert(this.form.action[1]);"> // the text input
</form>

your this.form.action is still a object. instead of using alert put it in a console.log(this.form.action) and use firebug and firequery try to discover what events/properties your this.form.action has.
!you need to enable your console in firebug before you can use console.log

Related

Problems with FORM URL when switching to GET-method

I have a problem with creating a simple search form, used to search my database.
I started using the POST-method and everything worked fine, but switched to using GET-method instead to be able to bookmark my search results. But now I have trouble with the "action-url" when posting.
<form action="index.php?page=searchresult'" method="post">
This worked fine, I got my var's sent to the stated URL, but
<form action="index.php?page=searchresult" method="get">
Doesn't work. When I check my HTML code everything looks great, but I end up going to the URL
site.com/index.php?ALL THE GET-variables linedup here.
I am loosing the "?page=searchresult" part when using the GET-method?!
Am I missing something here, please help?!
Humble regards :)
Try changing this to
<form action="index.php?page=searchresult" method="get">
Put the page element as a hidden tag.
<form action="index.php" method="get">
<input type="hidden" name="page" value="searchresult"/>
<!-- Rest of the input elements -->
<input type="submit" />
</form>
This should fix your issue.
If you are using GET methood then you can pass page parameter in hidden field
like
<input type="hidden" name="page" value="searchresult">
GET sends data in the query string. If the URL in the action has a query string, it will be overwritten by the new one generated by the form.
Store your data in hidden inputs instead.
Well, this is interesting, but you can fix it using an hidden <input>, such as this:
<form action="index.php" method="get">
<input type="hidden" name="page" value="searchresult" />
<input type="submit" value="submit" />
</form>
Just compile the "value" of the hidden input with what you need to pass.
Tried it and it actually works.
Edit:
obviously, from php, use this:
<?php $page = ((isset($_GET['page'])?($_GET['page']):("nope")); ?>
Add a hidden page data in the form.
<form action="index.php" method="get">
<input type="hidden" name="page" value="searchresult" />
Simply because "page=searchresult" is a get data.
You have a quote too much, try removing it.
|
\|/
<form action="index.php?page=searchresult'" method="post">

Button functioning as link does not work with $_GET

I am using a button to function in the same way as a hyperlink:
<form action="intro.html"><input type="submit" value="CLICK HERE TO ENTER" ></form>
This works for static links, but does not work with PHP $_GET arguments.
<form action="wrong_choice.php?stage=0"><input type="submit" value="Wrong Choice!" ></form>
Clicking that will proceed to "wrong_choice.php" but not "wrong_choice.php?stage=0"
How can I fix that?
Thank you
Better to use:
<input type="button" value="Wrong Choice!" onClick="document.location.href('wrong_choice.php?stage=0');" />
If you do not want javascript, add method to form, delete parameter from action and add input with type hidden, which stands for parameter.
Action does not accept query string!
If you want to append data into the form which isn't part of the inputs filled by the user, add inside the <form>
<input type="hidden" name="stage" value="0" />
Action is what you want to do with the information in the form: you want to send the form in a email or send the information to another script to manage or comeback to same script.
If you want pass arguments in the form you should put them in form's fields like that:
<form action="wrong_choice.php>
<input type='hidden' value='0' name="stage">
<input type="submit" value="Wrong Choice!" >
</form>
Thanks

Why won't jQuery submit this form?

I am trying to submit a form with jQuery and I must be missing something small, because I can't get this to work, and from everything I see it should work fine.
What's wrong with this?
<table class="newrecord"><form id="editthis" action="page.php" method="post">
<tr><td class="left">Name:</td><td><input type="text" name="name" id="name" /></td></tr>
<tr><td class="left">Company:</td><td><input type="text" name="company" /></td></tr>
<tr><td class="left">Cancel</td><td><input type="button" name="submit" class="subbut" id="subthis" value="Update" /></td></tr>
</form></table>
And the javascript:
$("#subthis").click(function() {
$('#editthis').submit(); // An alert box works, so I know this is triggering
});
As mentioned in the code, an alert box works if I click the submit button, but when I use the jQuery submit function, nothing happens. What am I missing???
You don't need to use jQuery to submit a form. It's default behavior for a submit button to submit the form it belongs to.
Also, don't use a table for layout. The form elements themselves can layout just fine.
<form id="edit_this" action="page.php" method="POST">
<label>Name <input type="text" name="name"></label>
<label>Company <input type="text" name="company"></label>
Cancel
<button type="submit">Update</button>
</form>
Can be easily layout'd and will submit on its own.
If you need something to happen before the submission with jQuery, bind it to the form's onsubmit handler, rather than the actual click of the button.
The actual problem is the collision between the name you've given to the button and the reserved word in JavaScript. Don't use submit as the name.
I see two possible problems.
1) You have form tags inside table tags. While this probably isn't the root cause of your problem, it's not valid HTML.
2) You've used "submit" as the name of your submit button. This should be avoided because your object will collide with JavaScript reserved words. Use something other than "submit" like you've done with the id attribute.

Post-variables only passed, when action="" is empty

In a php-script i have a form, method is post, action-attribute is empty, which is working so far. but when i add a value into the action-atribute, like so:
action="index.php?id=9&get-id=5"
the whole post-array is empty after submitting.
Someone has any idea what this could be about?
Thanx in advance, Jayden
edit: here is an Example:
$form = '<form name="form1" method="post" enctype="multipart/form-data" action="index.php?id=9&get-id=5">
<input type="text" name="name1" value="">
<input name="submit" type="submit" value="submit">
</form>';
The form is displayed in a tab in a js-tabmenu, which opens also by get-parameters, in each tab is a form and after submitting the get-param is needed to display the right tab with the right form.
try to use $_REQUEST
which is collection of $_GET and $_POST
You should not use both GET and POST in a request.
You must only use post, therefore the two variables 'id' and 'get-id' should be in the form (use hidden fields)
edit:
try changing your code to:
<form name="form1" method="post" enctype="multipart/form-data"
action="index.php?id=9&get-id=5">
<input type="hidden" name="id" value="9">
<input type="hidden" name="get-id" value="5">
<input type="text" name="name1" value="">
<input name="submit" type="submit" value="submit">
</form>
then if you :
print_r($_POST);
at the top of the index.php page you should be able to see what is going on.
Also - just to check are there any redirects in your code, ie does index.php then redirect somewhere else as that would cause the $_POST to get lost
If you're trying to access id or get-id from your script: Those were appended to the url, even if you submit that form via post. Therefore you will find their values in $_GET, as usual. Only the values of <input> fields (and textarea etc., simply: all form elements) are in $_POST.

Getting correct URL when using GET

When clicking a button, I would like my URL to become this:
users.php?action=search&formvar1=value&formvar2=value&...
This is what I've tried:
<form id="search" action="users.php?action=search" method="get">
But this doesn't seem to work (it doesn't add the action=search part). Is there any way to do this? I know it works when using POST instead of GET, so why wouldn't it here?
Thank you
You can do that similarly to a POST form. Simply include the default attribute as hidden form field:
<form id="search" action="users.php" method="get">
<input type="hidden" name="action" value="search">
This way it will be added as parameter to the URL like all other variables.
The browser is building the query string from scratch.
Instead, you can add an <input type="hidden" name="action" value="search" />.

Categories