javascript submit() not working - php

Here is the form that is inside index.php -- I'm trying to programmatically submit it using a click on an image that's nested inside an 'a' anchor:
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250"
id="artifactName">Enter an artifact name here...
</input>
<a href="javascript:document.landingForm.submit()">
<img src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</a>
</form>
Here is my php code:
<?php
if(isset( $_POST['imageFormSubmitter']))
{
// this fx just echos a javascript 'alert()'
showAlertBox("The index page form 'landingForm' was just submitted.");
}
else
{
var_dump($_GET);
var_dump($_POST);
}
?>
When I type something in the edit box on the form, then click on the image 'bronzeAgeCaveDrawing.jpg' the dump of the GET and POST arrays says:
array
Empty
array
Empty
And for that matter I'm not even sure the above PHP code is getting called for the
<a href="javascript:document.landingForm.submit()">
case at all because the above php code executes regardless of whether the form submits -- the GET and POST
dumps occur when index.php loads due to the 'else' clause above that I added because it didn't appear that the 'if' clause was happening.
If the above php code IS in fact executing by the submit() call, the GET and POST are still empty arrays.
I studied examples of using an anchor 'a' with an image combined with a call to submit() in a few posts on SO and I don't see any reason why the POST or GET array is empty. And due to my form method="post" I'm 100% expecting for the submit() to POST the form, not submit with a GET.
Been staring at this awhile -- am I missing something?
UPDATE: Here is the functioning code -- thanks for your help folks!
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250" name="itemName_name"
id="artifactName">Enter an artifact name here...
</input>
<a href="javascript: document.landingForm.submit()">
<img src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</a>
</form>
if(isset( $_POST['itemName_name']))
{
showAlertBox("The index page form 'landingForm' was just submitted.");
var_dump($_POST);
}
Output of the dump of POST array NOW shows:
array
'itemName_name' => string 'fffffffffff' (length=11)
TAKEAWAYS:
1) the syntax "document.landingForm.submit()" is 100% fine.
2) the 'name' attribute of my <img> tag does NOT get posted (not sure why,
but it's not the worst thing happening for me right now so I'll deal with it)
3) giving the text <input> field a 'name' attribute made its way fine to the POST array.
4) the call to submit() does in fact pay attention to the method="post" in my
form declaration.
I'm running with it, thanks all. There is more than one equally acceptable answer for me, so I'll +1 y'all
then have to choose one. Thanks again.

You're not setting the name attribute on your <input> tag, and the <img> tag is not the right way to submit with an image.
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250" name="artifactName"
id="artifactName" />Enter an artifact name here...
<input type="image" src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</form>
Source: HTML Dog tag reference
Note also that the <input> tag in HTML5 does not / cannot have children.

Related

GET parameter won't pass with html button formaction

I am trying to pass a GET parameter through a button but I can't figure out what I am doing wrong. The parameter is set, as it shows up fine in the header, but it isn't being added to the edit.php url. The button is directing me to edit.php, just without the GET parameter added. I am pretty new to this stuff and this is my first time using links that aren't through anchor tags, so I am clearly missing something here. Any advice is greatly appreciated.
<h1 class="headerWithButton">Claim #<?echo($_GET['claim_id'])?>
<form>
<button type="submit" formaction="index.php" class="backButton">Back</button>
<?echo('<button type="submit" formaction="edit.php?claim_id='.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
</h1>
When you submit a form using the GET method, any existing query string in the action will be replaced by a new one generated by the name and value of the successful controls associated with that form.
In your case, the only successful control is the submit button, which doesn't have a name or a value.
You could get the effect you desire by moving the data to those attributes:
<h1 class="headerWithButton">Claim #<?php echo htmlspecialchars($_GET['claim_id']); ?>
<form>
<button formaction="index.php" class="backButton">Back</button>
<button formaction="edit.php" name="claim_id" value="<?php echo htmlspecialchars($_GET['claim_id']); ?>" class="editButton">Edit</button>
</form>
</h1>
Important security note: inserting data from the URL directly into a page makes you highly vulnerable to XSS attacks. You need to take precautions against that. The most basic of those is using htmlspecialchars.
Note, however, that it isn't really appropriate to use a form here. Your form buttons are not submitting any data the user has entered, nor performing any kind of action. The affordances offered by buttons are misleading here.
You can, and should, use regular links instead.
<form method="get" action="edit.php">
<?echo('<button type="submit" formaction="edit.php?claim_id='.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
instead of using the form you can just use a straightforward link
k with the anchor tag
edit
or you can specify the methos of get on the form with a hidden for input to place the link get parameter
If you have to use formaction, you must specify name and value of element:
<h1 class="headerWithButton">Claim #<? echo($_GET['claim_id'])?>
<form>
<button type="submit" formaction="index.php" class="backButton">Back</button>
<?php echo('<button type="submit" formaction="/edit.php" name="claim_id" value="'.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
here it is better to place buttons in different blocks. But personally, in this case, I use a hyperlink
<form method="get" action=""></form>
<?echo('Edit
I find the easiest way to pass values with a button is to just wrap a form around the button.
$id = $_GET['claim_id'];
echo <<<EOT
<form action="index.php" method="get">
<button>Back</button>
</form>
<form action="edit.php" method="get">
<button name="claim_id" value="$id">Edit</button>
</form>
EOT;

How to get a form name in php script?

For my php file, I need to grab the unique form name.
The php file is executed when a user clicks the submit button. However, there are multiple submit button each with the same id, but they all have unique names. I need the name when they click on the submit button.
you dont want elements in html with the same id - bad practice in general. Your page will likely load normally but an html validator will notice it as an error.
html validator: http://validator.w3.org/
without seeing your code, its difficult to give you a definitive answer. if you have miltuple forms you can use hidden inputs. e.g.
<input type="hidden" name="form_name" />
Otherwise you can use javascript to put data in the form when the button is clicked. example javascript using jquery
html:
<form id="formid" >
<button type="button" id="someid" onclick="submitForm('btn1')" />
<button type="button" id="someid" onclick="submitForm('btn2')" />
<input type="hidden" id="btnsubmitid" value="" />
</form>
js:
function submitForm(btnID){
$("#btnsubmitid").val(btnID);
$("#formid").submit();
}
1 way is to put a hidden input inside of your form.
<input type="hidden" name="formName" value="[name of form]" />
then in your php, you can get it using
$form-name = $_POST['formName'];
pretty sure there are other ways, but this came to mind first.

Form submit not working with twitter bootstrap (PHP $_POST)

I just started using twitter bootstrap, and I got stuck trying to get my form submit button to submit (PHP $_POST). Does anyone know why its not working?
No clue really..
I've been doing this previously and its been working until bootstrap:
<?php
if (isset($_POST["login"]) && $_POST["login"]=="login") {
echo "login here";
}
?>
<form action="http://mysite.com" method="post">
<input type="hidden" id="login" name="login" value="login" />
<button class="btn success" type="submit">Login</button>
</form>
The page seems to load but the PHP does not. Is the POST information being blocked?
My form input elements did not have an id or name (I didn't include them in my example).
Not an HTML expert, but I've always used <input type="submit" /> and not <button type="submit" />, not sure that button is a functional form element.
Also, your button element is closed twice - first with /> in the opening tag, and then again with a closing tag for no reason.
Maybe you are wrong with the action="http://mysite.com"? That attribute is for specifing the form data receiver, so in your case I think it's the page itself. So, if your page is named mypage.php, use action="mypage.php".
I had this same issue as well, turns out I was missing the name="submit". Gotta have that so the $_POST has a key in the assoc array!
it is adviseable that every element has a name thus (name="elementName") this makes it easy to reference them in your php scipt; also ensure that a form action and method are set.

Upload Progress bar within form data

I try to create an upload progress bar with PHP and jQuery. However, I have a problem when I bring it to the form data. The code is similar like this:
<form method="post" action="upload.php" enctype="multipart/form-data" id="upload-form" target="upload-frame">
Suburb:<input type="text" name="txtSuburb" id="txtSuburb">
Picture:
<input type="hidden" id="uid" name="UPLOAD_IDENTIFIER" value="<?php echo $uid; ?>">
<input type="file" name="file">
<input type="button" name="submit" value="Upload!">
<iframe id="upload-frame" name="upload-frame">
</iframe>
<input type="submit" name="DataSubmit" value="Submit Data"/>
</form>
As you can see, I got 2 submit buttons. If I keep the form like this then the form can't submit data to server. It just submits the file to iFrame. If I change the action and target of the form then the upload progress function will not work.
Could anyone please help me to find the solution for this?
I want the user can click on upload button to upload their file. Then they can take the rest to fill the form. When everything is done, they can click on another submit data button to submit their data (included the file) to the server.
Make sure that you have only one input element of type submit within your form.
If you want the first button to trigger some Javascript, use a regular input element or even a styled link and attach a Javascript event to it's onclick event, then prevent it's default behavior, e.g. by returning false.
Like this only the second button will actually submit your form which should do what you're describing.
In general I'd second #Treffynnon's suggestion to use a existing library for this purpose. These hacks have a tendency to get pretty nasty, especially when it comes to crossbrowser compatibility.

isset code not being executed PHP

I was wondering if my code below is even correct, I've been having numerous errors with this, but am not sure if the problem really exists here. The code is below:
The user will click 'Exit Group'.
<p class="logout"><a id="exit" name="logout" href="#">Exit Group</a></p>
The code that should be execute when 'Exit Group' is clicked is below:
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
However, the code I am trying to execute when the user clicks 'Exit Group' is not even being executed. There is nothing wrong with the code within the braces, as numerous people have checked it. But I was wondering if my problem may lie in the code above? Thank you.
If you click the link, nothing happens because the URL only contains the fragment identifier #. Not even a GET request will be issued.
You use this kind of link normally to jump to an element inside the page (e.g. Top to jump to an element with ID top). This is completely handled in the browser.
And if you only put the fragment identifier there, just nothing will happen. This is very often used if the link should execute some JavaScript and should actually not link to something else.
You are testing the $_POST array at the server side. But this array only contains elements, if you initiate a POST request by a form. That means you need to create a form with a submit button, e.g.:
<form action="" method="POST">
<input type="submit" name="logout" value="Exit Group" />
</form>
Here comes the name attribute into play, which will be the key in the $_POST array. But assigning this on a normal link will have no effect.
You could do it also with the link, but with a GET request this way:
<a id="exit" href="?logout=1">Exit Group</a>
<!-- ^-- parameter must be part of the URL, name has no effect -->
and
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
Note that you have to pass a parameter logout it here.
It seems you have mixed up GET and POST requests. If you have a form, the name s of the form elements will be transmitted as parameters to the server. That means given this form:
<form method="POST">
<input type="text" name="foo" value="" />
<input type="text" name="bar" value="" />
<input type="submit" name="send" value="Send" />
</form>
if the user clicks on the submit button, the $_POST array at the server side will have the keys:
$_POST['foo']
$_POST['bar']
$_POST['send']
This does not work with links though. A click on a link will create a normal GET request, and here, the parameters must be part of the URL, appended after a question mark ? and separated by an ampersand &:
Link
will result in
$_GET['foo']
$_GET['bar']
$_GET['andMore']
You probably should read about the HTTP protocol.
a isnt a form control. it needs to be an input or select if it's within a form.
For manual linking, do href="/page?logout"
You're using a regular hyperlink, no form will get posted. you need a submit button of some kind in a form with method="post" to do that. regular links just result in GET requests and nothing will ever be posted that way.
edit: added simple example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Form test</title>
</head>
<body>
<?if ($_SERVER['REQUEST_METHOD'] == 'POST'):?>
<pre><? print_r($_POST)?></pre>
<?endif;?>
<? // $_SERVER['REQUEST_URI'] holds the current URL, so we know that ?>
<? // we'll end up back in this file when the form is submitted. ?>
<form method="post" action="<?= $_SERVER['REQUEST_URI']; ?>">
<input type="text" name="textbox"
value="<?= isset($_POST['textbox'])?$_POST['textbox']:'Type something' ?>" />
<input type="submit" name="submitbutton" value="Submit" />
</form>
</body>
</html>
$_POST will only be filled if you use a form with method=post.
Yes. A POST and a GET are two different things ;)
if(isset($_GET['logout']))
This <a id="exit" name="logout" href="#"> should be <a id="exit" href="?logoff=true#">.
Then logoff will be in the $_GET array.

Categories