How can I submit a form using an image in php - 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.

Related

How to not refresh page's previous field input values after press submit

I have an input like this:
<input value="<?php echo $formdata['title'] ?>" type="text" name="title" id="Editbox2">
This is an edit page, I load database data into fields with echo, replace them, and hit submit to update them.
But when I hit submit it refreshes the old data onto browser's fields, how can I prevent this?
Submit your form using ajax request with jquery submit.
Use action="javascript:;" for the form tag
You need to handle the script with javascript, then prevent the default behaviour, which is refreshing the page. Here is an example:
*I haven't tested this, but from what I recall this is what I used to do. Let me know if it doesn't work, I'll give other suggestions.
<form>
<!-- elements inside -->
<input type="submit" id="submit-btn" value="Submit"/>
</form>
and in your javascript have the following:
<script>
$("#submit-btn").click(function(e){
e.preventDefault();
// handle form here with your JS
});
</script>

How to target input box that's not in html form - PHP?

I'm new to PHP, so my entire perspective of how I should build something like this might be wrong, so please give tips as needed!
Pretty much the app should push data to the db when submit is clicked. I handle that with a form in the html and this code in PHP (let's ignore injection hacking for now...)
if(isset($_POST['submit']))
{
$book = $_POST['book-num'];
...
The submit button is in a form like such
<div class="submission-field">
<form method="post"><input class="submit-report" type="submit" name="submit" value="Submit"/></form>
</div>
The problem I'm having is how to target the numerical input box (book-num) if it's not in a form? My input box and submission button aren't anywhere near eachother so I couldn't wrap it with the same <form> tag the submit button is in. How do I handle this?
use js can help you:
<form>
<input type="hidden" name="book-num" id="book-num">
</form>
...........
<input type="text" name="book-num-input" onchange="fillIn('book-num',this)">
<script>
function fillIn(input_id,input){
document.getElementById(input_id).value = input.value;
}
</script>

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.

PHP code regarding multiple submit button on single form

i have two submit button on my index page namely International and Domestic. i want that two different button to point to different pages namely int.php and dom.php when i click on the buttons. can you help me out. thank
while it is allowed only to define single action = "" for form element. but if i have to do that, i would do it this way.
<form action ="somepage.php" method="post">
<!--all your input elements-->
<input type="submit" name ="international" value="international"/>
<input type="submit" name ="domestic" value="domestic"/>
</form>
determine which button have been clicked and act accordingly.
if(isset($_POST['domestic']) {
//include dom.php
}
else if(isset($_POST['international']) {
//include int.php
}
and then you can include the necessary file.
or the other way is to go with AJAX/jQuery way
you can just use switch in php for differ or
use javascript
Do it with jquery! First, dont create submit buttons just create
<input type="button" />
Than give them an id like:
<input type="button" id="InternationalBTN" />
<input type="button" id="DomesticBTN" />
and with jquery bind the action
$(document).ready(function(){
$("#InternationalBTN").bind('click',function(){
$('#idOfYourForm').attr('action','setTheDestinationPageHere');
document.forms['nameOfYourForm'].submit();
});
});
That will not be possible since your form's action attribute can only point to one location at a time and both buttons are in the same form(but possible if you use AJAX).
If you wanted to use pure PHP (i.e. no Javascript involved), you'd have to write two different handlers for the different button clicks, like below:
if(isset($_POST["name_of_international_button"])){
//actions to perform for this --
}
if(isset($_POST["name_of_domestic_button"])){
//action to perform for this
}
In the actions part of each of the handlers, you could then do a redirect, with the URL containing the data to be processed either in the int.php or dom.php scripts
You can do it in this way:
In form tag please leave empty action action=""
2 buttons to send:
<input class="btnSend" type="submit" name="International" value="International" id="International"/>
<input class="btnSend" type="submit" name="Domestic" value="Domestic" id="Domestic"/>
and use ajax:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function(){
$('#International ').click(function() {
// send to file 1 using ajax
});
$('#Domestic').click(function() {
// send to file 2 using ajax
});
});
</script>
Here is how to send data using ajax:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Your form action would have to contain some sort of conditional statement to redirect users based on which submit button is clicked
<form action="<?php if(isset($_POST['international'])){echo 'international.php';}else{echo 'domestic.php';}?>" method="post">
<input type="text" name="field1" />
<input type="text" name="field2"/>
<input type="submit" value="international "name="international"/>
<input type="submit" value="domestic "name="domestic"/>
</form>
Or you could set up your conditionals on a page specified by the form actionand have them redirect based on which button was clicked,
Just put a form tag, and set the action to the page. Then the submit button will navigate to that page where the action tag is pointing to...
Easy as that :D

How to Submit two values with only one submit button?

Hey im making a website were you can send other users emails.
My problem is when I go and submit it doesn't submit both values?
my Form has a text box where the user wants to send the email and another is a text area which is the message.
and i don't want 2 submit buttons
Have a look at http://www.w3.org/TR/html401/interact/forms.html#h-17.1 on how to create forms.
Your inputs need unique name attributes.
Example:
<form>
<input type="text" name="recipient_address" />
<textarea name="message_body"></textarea>
<!-- no real need for a name on the submit button -->
<input type="submit" value="Send Message" />
</form>
any value that you want to send to the server make sure it is in the form tag. When the user clicks your "submit" button all of the data in the form is sent off along with the page request. You can have as many items as you want in the form.
<html>
<head>
/*... your code here
...*/
</head>
<body>
<form name='sampleForm' action='sample.htm' onsubmit='ValidatethisForm()' method="post" enctype="multipart/form-data">
<input type='text' name='formElem1'>
<textarea name='formElem2'></textarea>
<input type = 'submit' value='submit'>
</form>
</body>
</html>
In the validate function once u finish your validations use "return true" to proceed. You can submit multiple form elements as long as they have different name attributes and the are within your form tag.
By far the best I have found is here
Happy Coding
Check your input and add a unique name attribute.

Categories