When a form has multiple image inputs and the server side uses their names and/or values to distinguish which one was clicked, it works perfectly in FireFox. However, people often write the whole thing before finding out that HTML specifies that nothing has to be sent, and thus some browsers are not sending it.
It's not about sending any random object, but sending a pair as input_name=input_value. The best worst-case scenario example here would be what I've encountered: A list of elements all in one form and all accompanied by buttons with name="delete" value="<item_id>"
What can I do to fix this problem?
Per the HTML spec, clicking on an IMAGE input will return the parameters:
name.x=x-value and name.y=y-value where "name" is the value of the name attribute
with x-value and y-value corresponding to the click position.
Sure, the server code to deal with this will be a little annoying, but you could just check all the query parameter keys with a regular expression:
/^(.*)\.[xy]$/
to search for the IMAGE input keys to determine which IMAGE was clicked.
I tried with this sample:
<form action="#" method="GET">
<input type="text" name="t" value="Text here"><br>
<input type="image" name="a" value="1" src="http://sstatic.net/so/img/logo.png"><br>
<input type="image" name="b" value="2" src="http://www.gravatar.com/avatar/c541838c5795886fd1b264330b305a1d?s=32&d=identicon&r=PG"><br>
</form>
And I get the following urls:
FF 3.6: x.html?t=Text+here&b.x=19&b.y=17&b=2#
IE 8: x.html?t=Text+here&b.x=22&b.y=18
IE 7: x.html?t=Text+here&a.x=185&a.y=51
Opera 10: x.html?t=Text+here&a.x=107&a.y=53#
Chrome: x.html?t=Text+here&b.x=20&b.y=17&b=2#
So it seems that all the browsers are sending something image related, even if it isn't the image name directly. Since you need to scan for all the image names that you expect to see you can just scan for imagename.x instead. This seems to be how the spec indicates it should work.
The problem was half solved up to now: like here
But it didn't allow to get the value!
The correct answer is:
$('input[type=image]')
.unbind('mousedown')
.mousedown(function(){
$(this).after('<input type="hidden" name="'+$(this).attr('name')+'" value="'+$(this).attr('value')+'" />');
});
This code creates a hidden duplicate of the input when user starts clicking it. The unbind('mousedown') is to secure it happens once even if You put the code in multiple places in a weird application and it might be called more than once.
I recommend putting it in $(document).ready();
I think I am/was having a similar problem. I wanted to click on an thumbnail and have it enlarged on a different page. I was trying to do this with PHP alone but I finally had to use the tag with the . Worked great for FF3 and safari but the INPUT IMAGE values did not post for IE9 or FF9.
My work around was to put each image in its own form and then also use a hidden input to send the needed data.
<table>
<tr>
<td>
<form method="post" class="form_photo">
<input type="image" name="img_photo" value="does nothing in IE9 or FF9" />
<input type="hidden" name="photo" value="nameoftheimage.jpg" />
</form>
<form method="post" class="form_photo">
<input ...>
<input ...>
</form>
<form> ...
</td>
</tr>
Then I discovered the forms displayed vertical, making it very odd. CSS to the rescue.
.form_photo { display:inline; }
seems to have solved the vertical problem. Now the user can click on the thumbnail and the value now passes in all the browsers I have access to testing.
Using the type="image" is problematic because the ability to pass a value is disabled for some stupid lack of reason. Anyways & although it's not as customizable & thus as pretty, you can still use you images so long as they are part of a type="button".
<button type="submit" name="someName" value="someValue"><img src="someImage.png" alt="SomeAlternateText"></button>
Related
Currently I am using a hidden form (for reasons decided upon by others, not myself) to act as a link so it can post data with itself. This "link" always opens in the same window, and the general tricks I have tried don't let it open in a new tab instead. This is the current code, any tips?
<tr><td>
<form method="post" action="myPage.php" class="inline">
<input type="hidden" name="submit_parassmHidden" value="extra_submit_value">
<button type="submit" name=".$dataIAmPassingToNextPage." value="submit_value" class="link-button">"
.$linkTitle."</button></form>
</td>";
^Took out the escape slashes to be easier to read, its in PHP, its echo so it shows as regular HTML.
Things like target="_blank" dont seem to be working for me or I am putting them in the wrong place. Anyone have ideas on how I could do this?
EDIT: Since people are saying I can't be doing both, this is what I am doing. It LOOKS like a normal link, there is no form, it IS HIDDEN.
"Ok you're saying 2 different things here. The form is either hidden or you can see the submit link. Both can't be true" <- From comments. Clearly it is true, the form is hidden, and you can see the submit link. However I want these "Links" (which are really buttons in disguise) to open in the new tab.
Unfortunately you have not another option.
target="_blank" or button with formtarget="_blank" in HTML5 (like #CD001 comment) is the solution.
_blank Opens the linked document in a new window or tab depending on the user's browser configuration.
But I saw a little trick here: https://stackoverflow.com/a/15551850/2951051
someone who say to use target="_tab" even if not exist (I dont know if it really work)
<tr><td>
<form method="post" action="http://stackoverflow.com" class="inline" target="_blank">
<input type="hidden" name="input_name" value="extra_submit_value">
<button type="submit" name="form_name" value="submit_value" class="link-button">test</button></form>
</td>
I'm relatively new to Delphi, so if the title doesn't match the contents of the question, my apologies. I'm looking to automate a process on a site that submits a form. I believe I've isolated the code from the site which handles this (values edited for account security) which is:
<form action="action.php" method="post">
<p class="center">
<input type="hidden" name="StringOne" value="StringOneB" />
<input type="hidden" name="StringTwo" value="StringTwoB" />
<input type="hidden" name="StringThree" value="StringThreeB" />
<input type="image" src="img/imageone.jpg" />
</p>
</form>
It is the only form on the page. The form is usually submitted via clicking imageone.jpg. The values of the three inputs are generated dynamically. How would I automate submission of this form? I planned to use the internet explorer OleObject. My code currently looks like
procedure TForm1.Button1Click(Sender: TObject);
var
IE: variant;
begin
IE:= CreateOleObject('internetExplorer.Application');
IE.visible := true;
IE.navigate('http://thesite.com');
I have attempted to use the following (each line is a separate attempt):
IE.Document.GetElementByID('StringOne').setAttribute('value', 'StringOneA', 0);
//above won't work because input has no ID
IE.Document.GetElementByName('StringOne').setAttribute('value', 'StringOneA', 0);
//does not appear to be understood
IE.OleObject.Document.forms[0].submit();
IE.OleObject.Document.forms[1].submit();
//does not appear to recognise the form
I've tried countless other lines of code, but none have been fruitful. Can this be done and if so, how? If any code could be explained, it would be helpful, as I am not well versed in OleObject automation. I can ham-handedly get the values from the HTML source, the main focus of the question is simply submitting the form assuming I have the values. Anything else is a bonus. If anything needs to be clarified, just say.
I am using 2 images in a form to sort out query results from the database. The form is submitted using the POST method. When i click on the first image, the query results have to be sorted in ascending order, and when i click on the second, the results have to be sorted in the descending order.
This is the code for the form:
<form name="" action="" method="post">
<input type="hidden" name="typep" value="price" />
<input type="image" name="sort" value="asc" src="images/asc-ar.png" />
<input type="image" name="sort" value="desc" src="images/dsc-ar.png" />
</form>
Now this is the code for checking if the $_REQUEST['sort'] variable is set and therefore whether sorting is required or not.
if ($_REQUEST['sort'] != "")
{
$sort = $_REQUEST['sort'];
$typep = $_REQUEST['typep'];
//query to be executed depending on values of $sort and $typep
}
Firefox does detect the $_REQUEST['typep'] variable but not the $_REQUEST['sort'] one.
This works perfectly in Chrome though. When i test the site in Firefox, it doesn't detect the $_REQUEST['sort'] variable and therefore the if condition evaluates to false and the search results don't get sorted.
Apparently at some point in the HTML5 standard development somebody decided that the actual value of input type="image" is unimportant, and they decided to just not require browsers to submit it in any way: link to specification.
Unfortunately, it seems not only Firefox sticks strictly to the specification. A comment to a bug submitted to the Firefox developers states that this behavior is also observed in Opera and Internet Explorer.
So, you can test if the image has been clicked by inspecting the .x/.y coordinates submitted with the form, but you cannot determine which of the two images has been clicked, because you don't reliably receive its value across different browsers, and browsers that still pass the value will likely also follow the spec and drop it at some point in the future. You will need to name the two buttons differently.
Actually Firefox will only send the coordinates of the click. If you want to use input type="image" you can use:
<input type="submit" name="sort" value="desc"><img src="..." /></input>
and
<input type="submit" name="sort" value="asc"><img src="..." /></input>
or you can use css to input type="submit". input type="image" is equivalent to giving background image to button, so use that instead.
If you have multiple input controls with same name in form , they will appears as array in PHP not as single value.
What does
var_dump($_POST)
or
var_dump($_REQUEST)
show?
how can i pass parameters from an html page(map.html) to a php(createxml.php) without having to open the php page? Im incorporating googlemaps in html page(map.html) so i want the users to enter data on a form on the html page which will be sent to php(createxml.php) which in turn will connect to mySQL DB and create an xml format of the response the html page uses this xml output to create positions on the map since it contains longitude and latitude.
I have used the following code in the heading of the php page(createxml), but it shows the contents of php file for a brief moment redirecting me to map.html
Thanks for your time, i can post all the code if needed.
<meta http-equiv="refresh" content="0;url=http://localhost/map.html/">
It's quite simple with AJAX, using jQuery you don't have to know much about it :)
So simply import the latest jQuery Library.
Then you have your form:
<form id="my_form">
<input type="text" name="param1" />
<input type="text" name="param2" />
<input type="hidden" name="action" value="do_stuff" />
<input type="submit" value="Submit" />
</form>
and somewhere beneath that, you just paste this tiny javascript-function, which handles the submit of the form:
<script>
$('#my_form').submit(function(){
var post_params = $('#my_form').serialize();
$('#waiting').show();
$.post('the_page_you_are_on.php', post_params, function(data) {
$('#waiting').hide();
return false;
})
});
</script>
(The element (div, p...) with the id "waiting" could e.g. contain one of those fancy ajax loading images, but is not neccessary! :) If you want one to be shown, find one via google, set it as the background image of the #waiting-element and set its display to none (CSS)).
The function itself just calls the page you're on and then you've got the form variables in your post-array, so the top of your page could look something like this:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'do_stuff' :
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
//do some DB-stuff etc.
break;
}
}
?>
I hope that helps!
It's a terrible idea, but because you don't want to use AJAX you could put the PHP in a frame and reload just that portion. Again, awful idea, but the closest you're going to get without using AJAX.
On a useful note though, AJAX is literally just one function in javascript. It's not hard at all to learn.
If you are just trying to pass parameters to a PHP page from the web browser, there are other ways to do it beyond 'Ajax'. Take a look at this page and view the source code (be sure to view the source of the included javascript file: http://hazlo.co/showlist.php?s=chrome&i=4e289d078b0f76b750000627&n=TODO
It uses an extremely basic method of changing the src of an image element, but passes information to the web server (PHP page) in the querystring of the image request. In this example I actually care about the results, which are represented as an image, but it sounds like you are just trying to pass data to the server, so you can return a 1 pixel image if you like. BTW, don't be fooled by the URL that is being used, a server rule is telling apache to process a specific PHP file when check it,GIF is requested.
You should play with it and use firebug or chrome's built in debugger to watch the requests that are being sent to the server.
You can't get any results from a PHP-script if you don't request it and process the output. If you dont't want to leave the current page, you have to use AJAX!
"but it shows the contents of php file for a brief moment" The reason is, that your browser first needs to load the entire page, then start the META-redirect. You don't need a redirect to load data from the server, but if you really want to, you should HTTP-headers for redirect.
Ok guys after hours of headache i finally found the solution! Basically i called my xmlproduce.php from inside my map.html, lemme explain so maybe will help others:
maps.html contained a googlmap API Javascript function which called my createxml.php called second.php
GDownloadUrl("second.php", function(data) )
what i did was i tweaked this call to second.php by adding some parameters in the URL like:
GDownloadUrl("second.php?strt="+ysdate+"/"+msdate+"/"+dsdate+"&end="+yedate+" /"+medate+"/"+dedate+"&id="+ide, function(data)
which is sending the parameters needed by second.php, so after that i made a small html form which called the script of googlemap api on the same file(map.html) to pass the parameters from the form to the GDownloadUrl i mentioned above, the code of the form is :
form method="get" action="">
IMEI: <input type="text" id="id" name="id" size="25" /> <br />
Start Date: <input type="text" id="ysdate" name="ysdate" size="4" value="2000" /> <input type="text" id="msdate" name="ysdate" size="1" /> <input type="text" id="dsdate" name="dsdate" size="1" /> <br/>
End Date: <input type="text" id="yedate" name="yedate" size="4" /> <input type="text" id="medate" name="ysdate" size="1" /> <input type="text" id="dedate" name="dedate" size="1" /> <br/>
<input type="button" value="submit" onClick="load()" />
</form>
afterwards i put extra constraints on the form for the values allowed.
Thanks everybody for the help, and you can always ask if somebody needs some clarification.
According to a PHP script of mine, Google Chrome is sending an empty (e.g. no value="") input field with a form post. This is reflected in $_POST superglobal in PHP. However, other browsers are not exhibiting this same behavior.
Furthermore, the <input> in question is inside of a <div style="display: none;"/> (that is verbatim in the code). It seems as if other browsers don't send this along.
How/where do I find documentation or proof that Chrome handles this differently?
Update
It turns out that the div has a css style applied to it which applies visibility: hidden; - I'm not sure if this alters the answers below or not.
See here for actual code:
From the form-handler:
$check = stripslashes($form['E-mail']);
if (!empty($check)) {
$problem = false;
$errors[] = '0|wrong spamcheck code!';
}
From the form:
<td><div style="visibility:hidden; display:none"> <input type="text" name="E-mail" ></div></td>
I can confirm that both Firefox and Safari exhibit this behaviour too.
<?php
var_dump($_POST);
?>
<form method="post">
<input type="hidden" name="hiddenField" />
<input type="submit" />
</form>
The results of this are:
array(1) {
["hiddenField"]=>
string(0) ""
}
Also tested inside a hidden div, and with value="", result was the same. Should also point out, I don't think closing a div with /> is actually allowed according to standards, seems like bad practice to me regardless (since you state it is verbatim).
Edit: Out of my own curiosity, I have now also tried it without type="hidden" (type attribute removed completely, simple <div style="display:none"><input name="hiddenField" /></div>) and placing it inside a div with display: none. Results remained consistant in Firefox and Safari.
I have found the solution - it turns out that in Chrome, it populates this form field with your auto-complete email address. A var-dump of the results of the form submission confirmed.
Thanks so much to those who looked into this - it led me down the path to solution. Votes up for all!