Does Chrome handle empty form fields different to other browsers? - php

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!

Related

Automating a .php command in Delphi via oleobject

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.

406 Not Acceptable error occurs in form when textarea contains raw html (input type checkbox)

406 Not Acceptable error occurs in form when textarea contains raw html (input type checkbox)
Hi guys/gals, very strange problem I'm having. Let me gt right into it:
I tested my site on Windows 7, Xampp 3.0.12 and using localhost everything works as it should...however, after uploading my site to godaddy cPanel, the 406 error began to occur, mentioned this in case it is relevant.
Problem:
I have a series of forms, 1 form to upload picture & text, 2nd form to use data from form 1 to fill in form 2 to get appropriate values (these two forms are on page1.php, upon submission of 2nd form that contains textarea fields that are holding html code to be sent to a new .php page (page2.php) the error displays.
After testing further to find the source of the error I find that the forms work properly if input type="" is not included in the textareas upon submission. If these input fields are included the error is thrown.
I have provided the code below that I think is relevant to solving this problem, but if there is something missing, let me know and I will add whatever is needed so we can get to a solution.
Form 1 looks like:
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data">
//contains input type text, file fields
<input name="submit" type="submit" value="">
</form>
Form 2 looks like:
<FORM NAME="product-submit-form" ACTION="http://www.mywebsite.com/product-live-demo.php" METHOD="POST" enctype="multipart/form-data">
//contains input type text, number, and includes textarea fields
//like this one below.
<div class="textarea-size">
<ul class="result2" contenteditable="true"></ul>
<textarea id="sizes" name="sizes" rows="8" cols="30" title="content">
</textarea>
</div>
<input type="submit" value="" name="submit-demo">
</form>
This is a sample of the code that is being inputed into the textarea fields:
<div class="checkbox-black"><input type="checkbox" id="black" name="Color:_Black" value="Yes"><label for="black" title="Black"></label></div>
The code above throws the 406 error, whereas if I simply delete <input type="checkbox" id="black" name="Color:_Black value="Yes"> then the error does not show.
This strikes me as odd because I have run a javascript that transforms the html code into raw text so it can be submitted in its raw text form properly to be rendered as html code in the next page. So, i just can't see why this particular piece of text is causing an error.
The javascript I have on page1.php with form 1 & 2 is here:
<script>
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
var el = $(this).closest('.box-checkbox-size').find('.rawHTML-code-insert2');
var title = el.html();
var id = el.data('id');
// If the checkbox is checked, add the item to the ul.
if($(this).is(':checked')){
var html = $("<li/>", {
title: title,
text: title,
id: id
});
$('ul.result2').append(html);
} else {
// if the checkbox is unchecked, remove the item from the ul.
$('li#' + id).remove();
}
});
});
</script>
On page2.php the form (form 3-demo) looks like this:
<form action="https://mysite.foxycart.com/cart" method="post">
// the foxy cart action value I added but the demo is not meant to be a
// working version, only a visual representation of what it would look like
<div class="box-checkbox-size">
<?php echo $_POST['sizes'] ?>
</div>
//other content
<input type="submit" value="" class="submit">
</form>
Attempted Solutions:
1.) I tried altering the accept charset="" values from utf-8 to ISO-8859-1 and vice versa on the forms but i gave up after trying about 6 different combinations with the forms I have. If this is the root of the error please let me know what values go for which form. thx
2) I also tried adding enctype="multipart/form-data" to form 3 but that seemed to have no effect.
3) I researched a little on godaddy and said .htaccess may be a cause but no detailed instructions were provided on how to remedy this type of problem.
This is all I have at the moment, if anyone can offer a solution it would be most helpful.
Thankyou
I encountered the same error on godaddy
basically change the name of input fields to body1..2..3
http://w2cschool.com/How/How-To-Manage-Not-Acceptable-406-error-On-Godday-Server-/1170
In regards to a site on your hosting account, the cause of the 406 error is usually due to a mod_security rule on the server. Mod_security is a security module in the Apache web server that is enabled by default on all hosting accounts. If a site, page, or function violates one of these rules, server may send the 406 Not Acceptable error.
You said that it worked fine on your localhost, that means you need to talk to godaddy, and see if they could disable specific ModSecurity rules.
Try this article: http://www.inmotionhosting.com/support/website/modsecurity/find-and-disable-specific-modsecurity-rules
use name="description" or name="message" in your name attribute ..
Hope This is solved your problem
Example
<textarea name="message" rows="8" cols="30" >

how to enable the submit button in real time depending on text validation?

I have a HTML form in list.php that submits the data from text box ("item" in below code) to check.php. This check.php validates the text entered to be not empty or white spaces only. After validation, it redirects to list.php for the entered text to be displayed. list.php is below. I want the "add" button to be enabled only when valid text is entered in the text box. I would like this feature to be done with php and probably not with javascript.
I can use "disabled=\"disabled\" in the form, but this does not work in real-time disabling depending on validation.
<form action="check.php" method="post">
<input name="item" type="text" size="25" autofocus="autofocus" />
<input type="submit" value="Add" id="add" />
</form>
You say:
I would like this feature to be done with php and probably not with javascript.
Unfortunately, if you want "real-time" then you're gonna need JavaScript. You'll need it to make AJAX calls to your PHP code to check for validation.
So either A) you don't validate in "real-time" at all, or B) You use JavaScript in one shape or another.
Let's say you opt for B), to use JavaScript, and presuming ALL you need to do is check for an empty string or whitespace, then you can do all of this client-side in JavaScript and not require a server call at all, also making it truly "real-time".
And so, here is my solution, using JavaScript (jQuery) without relying on server calls. This may not be suitable for your current implementation, but just in case it is, this might be helpful.
JSFiddle:
http://jsfiddle.net/VKfrw/1/
JavaScript:
function hasWhiteSpaceOrEmpty(s)
{
return s == "" || s.indexOf(' ') >= 0;
}
function validateInput()
{
var inputVal = $("#myInput").val();
if(hasWhiteSpaceOrEmpty(inputVal))
{
//This has whitespace or is empty, disable the button
$("#add").attr("disabled", "disabled");
}
else
{
//not empty or whitespace
$("#add").removeAttr("disabled");
}
}
$(document).ready(function() {
$("#myInput").keyup(validateInput);
});
HTML:
<!-- give this guy an ID -->
<input id="myInput" name="item" type="text" size="25" autofocus="autofocus" />
This implementation uses jQuery.
As mentioned, if you want this done in real time some javascript will be needed.
However I think this problem is actually more suited to javascript in general. PHP validation can be useful if you need to cross reference for data with data in your database.
eg. In a sign up form, checking a user is not already registered with the entered email address.
But in your case, depending on what you mean by "valid text" it is probably easier and better to use javascript.
There are some great jQuery plugins which make javascript validation really simple.
http://docs.jquery.com/Plugins/Validation/validate

$_POST Variable Detected in Chrome but not in Firefox

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?

Image buttons not working on internet explorer [duplicate]

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>

Categories