form element link acts as a button - php

I have a HTML form that contains many links (around 17). I want, when a link is clicked, the form is submitted using the same ID in the link.
<form id="form" action="some page.php">
<a href='?value=0'>value 1</a><br />
<a href='?value=1'>value 2</a><br />
<a href='?value=2'>value 3</a><br />
<a href='?value=3'>value 4</a><br />
<a href='?value=4'>value 5</a><br />
***********************
**** CLIPPED ****
***********************
<a href='?value=16'>value 17</a><br />
</form>
When a link is pressed, it send me to the same page index.php?value=some number. I want each link to have a certain value/id which when clicked, the form is submitted and sent to some page.php.
Is this possible to be done?

You must use a name on your form:
<form id="form" name="myform" action="some page.php">
Add a hidden field:
<input type="hidden" name="myvalue">
Then use javascript on your link:
value 17
or
value 17
And finally, write a javascript:
<script language="javascript">
function submit(value) {
document.myform.myvalue.value = value;
document.myform.submit();
}
</script>
If your form contains only one field, you can also use a simple link with value:
value 17

You can simply point the link to somepage.php?value=1.
Edit: Sorry, I misread! Here is the correct answer: use JavaScript to set a hidden variable and submit the form:
<input type="hidden" name="value" />
<a onclick="this.form.value=1; this.form.submit();">value 1</a>
<a onclick="this.form.value=2; this.form.submit();">value 2</a>

You can use href="some page.php?value=" etc., except that a naked space is not allowed in a URL. But this means just a link—not form submission. If you want form submission, use input type=submit or button type=submit elements. The easiest way might be to input type=submit with a value parameter. However, your real problem (which cannot be guessed from the description) might have a completely different solution.

This looks like a link but IS a button. You won't get the benefit of visited link colors but I believe this is what you are looking for.
http://jsfiddle.net/KtZPW/3/

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 insert form information into html button with php

Here's what I'm looking to accomplish. When a user creates a profile they fill out some information with a form. After they submit the form the information is displayed on their profile. One of the form fields is going to be a button that other users can click to do an action. To display the button this is the PHP I currently have:
add_action('kleo_bp_after_profile_name', 'my_profile_button');
function my_profile_button()
{
echo 'Talk';
}
I need to input the form information into the href="#" spot. Does anyone know a way around this?
Thanks.
It sounds like you want to simply submit a form that a user fills out. If that is the case, you can't use a link, but you need to use a button:
<form action="submitpage.php" method="post">
Name: <input type="text" />
<input type="submit" value="Some Text" />
</form>
or
<form action="submitpage.php" method="post">
Name: <input type="text" />
<button type="submit" class="success button radius show-for-small">Some Text</button>
</form>
Sure, if you have captured that information with a POST variable, named 'redirect' for example, you could use it to generate a button. The problem is that I don't understand very well what you mean with be put into href="#" spot, because buttons don't have that property, so I write you the code to use it on a redirection which is done at clicking the button:
<input type="button" value="submit" onclick="location.href='<?php echo $_POST["redirect"];?>';">
If you want to use information in a link, which actually have href property use this:
<a id="link" href="<?php echo $_POST['redirect'];?>">Text of the link</a>

How can I submit a form using an image in php

I want to submit a form to the database and I want to use a sprite image instead of regular submit buttons..
Here is the images I'm using
<div class="cancel">
</div>
<div class="save_and_new">
</div>
<div class="save_and_quit">
</div>
if(isset(......)){
}
I have no idea what to put in the isset function ...
Do i need to set names to the images? or what?
You could just use
<input type="image src="/your/button/image/here.gif" />
instead of the images nested inside anchors.
The only problem would be that you can't directly sense which button exactly was pressed because <input type="image" /> does not post a value. If you really need multiple post buttons that also post a value:
<button name="button" value="action1"><img src="/your/image/here.gif" alt="action 1" /></button>
<button name="button" value="action2"><img src="/your/image/here.gif" alt="action 2" /></button>
You can do it in Jquery. Try this,
$("#save_and_new_btn").click(function() {
$("#form").submit();
});
#form is id of form
Generally, a form is submitted when the user presses a submit button. However, sometimes, you may need to submit the form programmatically using JavaScript.
JavaScript provides the form object that contains the submit() method. Use the ‘id’ of the form to get the form object.
For example, if the name of your form is ‘myform’, the JavaScript code for the submit call is:
document.forms["myform"].submit();
But, how to identify a form? Give an id attribute in the form tag
<form id='myform' action='formmail.pl'>
Here is the code to submit a form when a hyperlink is clicked:
<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
Search
</form>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>
Source: How to Submit a Form Using JavaScript
You need to use javascript.
Find a form which has to be submitted. Then add actions to each elements. Whene they're clicked you are submitting (or canceling) form.

how to focus on a particular section in another html or php page while clicking a button?

I want to use a search option to find the contents in another page.
My Html page:
<form action="search.php" method="post">
<input name="search" type="text"/>
<input type="image" src="images/search-btn.jpg" alt="search-btn" />
</form>
I have 5 topics in another page, say 'products.html'. Topic headings are Film, Music, etc.
So if a keyword, like the headings or some predefined keywords in each topic, is typed and search button is clicked I want to redirect to the products.php page.
The main thing is that i want to get the focus on that particular topic
Now I'm redirecting simply like this:
<?php
$val=$_POST['search'];
if($val=='Music'||$val=='singer')
{
header('Location:http://localhost/products.html');
}
?>
page1.html
<a name='music'></a><!-- This is anchor -->
<a href='#music'>Go to music on same page</a>
page2.html
<a href='page1.html#music'>Go to music on another page</a>
SERVER SIDE:
<?php
if($val=='Music'||$val=='singer'){
header("Location:http://localhost/products#$val.html");
}
?>
You can style your keywords anchors as you wish:
<a name='music'><b>music<b></a>
<span id='music'>music</span>
Within your form post to a page that can deal with the result, save it and then push the user on.
<form action="process.php" method="post" name="search">
This response page will redirect to something like
header("Location: /products.html#music");
Depending on how the form was completed.
Then on your products page add ids that tie this up, for example:
<h2 id="music">Music</h2>
The user will be redirected and the browser will jump to the corresponding anchor or id.
You can also try this technique by using a hidden field, but for the first page JavaScript will work for this
<input name="target" type="hidden" value=""/>
<input name="search" type="text" onkeyup="your-function-to-set-value-on-hidden-field"/>
Set the value if typed keyword is matched with your list and on the second page receive the hidden field value in PHP variable and do the stuff.

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