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

Related

HTML how can I preprocess text when clicking submit

I have a form that is using get as its method. The structure looks something like this:
<form class="form-download" method="get" id="download" action="dest.php">
<h1 class="form-download-heading">Process</h1>
<input type="text" name="destid" id="destid" size="40" placeholder="Input" />
<input class="btn btn-primary" type="submit" name="type" id="type" value="Download" />
</form>
The problem is that I want the contents of the field destid to be processed when I click the submit button but BEFORE anything else, i.e. before it gets into the URL bar.
I have seen many examples that simply do not do that while using the GET method, so I would like to know how can I fix this problem.
You can include an onsubmit event in the form, like this:
<form class="form-download" method="get" id="download" onsubmit="myFunction()" action="dest.php">
The onsubmit event will fire before the action.
That way, when you click the submit button, you can process the contents of the field destid before anything else.

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.

How to post form variables with javascript, just like a type="submit" button

I'm a noobie programmer and I wonder how to properly submit a form with javascript.
I made some test code to show you what I mean:
<?php
if (isset($_POST['message']))
{
echo $_POST['message'];
}
?>
<script>
function formsubmit()
{
document.getElementById('form').submit();
}
</script>
<form id="form" name="form" action="" method="post">
<input id="message" name="message" value="hello world">
<input id="submit" name="submit" type="submit">
</form>
Click me<br/>
<input type="submit" onClick="formsubmit()" value="Click me">
When you push the "submit" button inside the tags - the php code will echo "hello world".
When submitting the form with JS the values won't post to the page. What am I doing wrong?
Thanks in advance.
I've searched the whole afternoon for a solution, but cause of my lack of knowledge about programming I failed to find it.
Believe it or not, but the main problem lies here:
<input id="submit" name="submit" type="submit">
If a form contains an input element with name (or id) of submit it will mask the .submit() method of the form element, because .submit will point to the button instead of the method. Just change it to this:
<input name="go" type="submit">
See also: Notes for form.submit()
The smaller problem is here:
Click me<br/>
An empty anchor will just request the same page again before calling formsubmit(). Just add href="#".
The problem here is that the id and name of the input element on your form is called submit.
This will mask the submit function for the form. Change the name and id and you will be able to use javascript to submit the form.
try setting the href of the to '#'. I would guess what is happening is that by clicking on the link, it submits the form and immediately changes the url to the same page you are on cancelling the form submit before it has a chance to go.

How to make an image-button run a PHP script?

What's the best practice to create an image button that sends a value and runs a php script (that executes a mySQL query) when clicked. The button has to be an image and not a default submit type of button. I've been googling this for a few days and I still can't find a sutable answer. I could use GET and make a few image buttons (images with links that contain values) on the page that redirect to itself which then I can collect with
if (isset($_GET['variable']))
but I don't really want the user to see the values. I tried creating a form which has only one button in it that when clicked will reload the page and I could capture and use the value with
if (isset($_POST['submit_value'])) {$var = $_POST['submit_value']; }
but I can't seem to make this work, at least not when the button is an image. So if anyone knows a decent way to do this, please share. It doesn't have to be AJAX e.g. page reload is perfectly fine. I'm guessing that I need JavaScript to do this but I don't really know JavaScript so a working example would be nice.
SELF-ANSWER
Thank you for all of your answers. I found that the simplest working way to go with is to create a form with an input type of image that makes the submit and an input type of hidden that carries that value.
<form action="some_page.php" method="POST">
<input type="hidden" name="variable" value="50" />
<input type="image" src="image.png" name="submit" />
</form>
And on the PHP side I use this to catch the value.
if (isset($_POST['variable'])) { $var = $_POST['variable']; }
This is the most suitable solution for my problem. Thank you all again for your speedy responses.
Image buttons are pretty much a mess! :(
I would suggest using CSS to put background-image to ordinary <input type="submit">. This way value will always be visible (eg. sent in request) when user submits the form.
For example:
.myImageSubmitButton{
width: 100px;
height: 22px;
background: url(images/submit.png) no-repeat;
border: none;
/** other CSS **/
}
the bad thing here is that you must set width and height according to image used...
if it must be a <button> you can redirect the form to another script like this:
<form action="somescript.php" method="POST" name="myform">
<input type="submit" name="submit" value="normal submit">
<button name="foo" type="button" value="bar"
onclick="document.myform.action = 'someotherscript.php';
document.myform.submit()">
<img src="someimage.png">
</button>
</form>
or change a hidden field and post the form to the same page like this:
<form action="somescript.php" method="POST" name="myform">
<input type="submit" name="submit" value="normal submit">
<input type="hidden" name="action" id="hidden_action" value="normal_action">
<button name="foo" type="button" value="bar"
onclick="document.getElementById('hidden_action').value = 'special_action';
document.myform.submit()">
<img src="someimage.png">
</button>
</form>
Just a note: if the user wants to, they CAN retrieve the values, for example with Firebug. This cannot be changed.
Also, HTML buttons can be images. See this.
Or use XMLhttprequest on an image wih onclick. There are many tutorials for XMLHTTPRequest. For example this.
You can make a POST form and use the image as a submit button without javascript:
<input type="image" src="myimage.gif" name="submit">
invoke a submit using onclick event on the image
<img src="image.jpg" onclick="document.formname.submit();" />
make submit button with image like that
<input type="submit" style="background-image:url(image); border:none;
width:10px;height:10px; color:transparent;" value=" " name="submit_value"/>
I think the only two ways of doing this are with gets (like you've stated) or with a form where the image button is an input with type submit.
I'm pretty sure you can change the styling of a submit button so that it has a background image, if not then ignore my ignorance.

form element link acts as a button

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/

Categories