using html form with two </form> and submit buttons? - php

var el = document.getElementById('btn1');
el.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Add';
var form = document.getElementsByTagName('form');
form.submit();
});
var el2 = document.getElementById('btn2');
el2.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Print';
var form = document.getElementsByTagName('form');
form.submit();
});
<?php
if($_POST["hidElement"]=='Add')
echo "add";
else
echo "Print";
?>
<form action="" method="">
<input type="text" name="nasir" >
<input type="hidden" name="hidElement" id="hidElement" />
<input type="button" id="btn1" value="Submit & Add">
<input type="button" id="btn2" value="Submit & Print">
</form>
<?php
if(isset($_POST["yasir"]))
echo "Submit & Print";
else
echo "Submit & Add";
?>
<form action="" method="">
<input type="text" name="nasir" >
<input type="submit" value="Submit & Add">
</form>
<input type="hidden" name="yasir">
<input type="submit" value="Submit & Print">
</form>
There is no second starting for second form. I want both forms use same , but how PHP will recognize which form button has been pressed. I tried above but in both cases it submiting second yasir hidden input!!!
There is alot of inputs in first form.

please try this
<form action="" method="post">
<input type="text" name="nasir" >
<input type="submit" value="Submit & Add" name="first_btn">
<input type="hidden" name="yasir">
<input type="submit" value="Submit & Print" name="second_btn">
</form>
in php you can check by using this
<?php
if(isset($_POST['first_btn'])){
/*first form submit*/
}
if(isset($_POST['second_btn'])){
/*second form submit*/
}
?>

For every there should be an end also. Otherwise it will not work
try
<form action="" method="post">
<input type="text" name="nasir" >
<input type="submit" value="Submit & Add" name="btn1">
<input type="hidden" name="yasir">
<input type="submit" value="Submit & Print" name="btn2">
</form>

In that case it's better to use javascript to handle form post. You can add to buttons to same form for add and print and handle click on them using javascript. Something like below:
<form action="" method="" id="form">
<input type="text" name="nasir" >
<input type="hidden" name="hidElement" id="hidElement" />
<input type="button" id="btn1" value="Submit & Add">
<input type="button" id="btn2" value="Submit & Print">
</form>
In javascript
var el = document.getElementById('btn1');
el.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Add';
var form = document.getElementById('form');
form.submit();
});
var el2 = document.getElementById('btn2');
el2.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Print';
var form = document.getElementById('form');
form.submit();
});
In Php you can check based on hidElement value.

Related

Submit form on page load

I want this login form to submit automatically when the page loads. I am using IP address for registration so do not want a login button.
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="ip_address" /><br />
<input type="submit" name="submit" value="Login" />
</form>
You can do something like:
<form id="my_form" action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input id="ip_address" type="hidden" name="ip_address" /><br />
<input type="submit" name="submit" value="Login" />
</form>
<script>
$(document).ready(function(){
if($("#ip_address").val() != "")
{
$("#my_form").submit();
}
});
</script>
Note: you must add JQuery library in your web page.
In case you don't use jQuery:
<body onload="javascript:submitonload();">
<form id="my_form" action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="hidden" name="ip_address" value="<?=(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');?>" />
<br />
<input type="submit" name="submit" value="Login" />
</form>
<script type="text/javascript">
function submitonload() {
document.getElementById('my_form').submit();
}
</script>
</body>
Using JavaScript:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" id="my-form">
<input type="hidden" name="ip_address" /><br />
<input type="submit" name="submit" value="Login" />
</form>
<script>
//Once the page is loaded, submit the form
document.addEventListener("DOMContentLoaded", function(event) {
//The id of your form.
var idOfYourForm = "my-form";
document.getElementById(idOfYourForm).submit();
});
</script>
You just need to set the id attribute to your form and set the variable idOfYourForm, so the JavaScript will be able to submit your form.
This method use pure JavaScript, no jQuery or other library.
See DOMContentLoaded event.

Does a jquery event overwrite the submit action?

I have one form with a few submit buttons. I want to submit the form via POST to itself to process the filled out form fields... does the jquery override the submit?
$('#myButton').click('myAction') <!-- not actual code, just for the idea -->
<input type="button" type="submit" id="myButton" value="do something">
you can use something like this:
html form:
<form id="myform" method="post" name="form" action="">
<input type="text" name="name" value="">
<input type="text" name="other" value="">
<input type="submit" type="submit" name="myButton" value="do something">
</form>
js:
$('#myform').on('submit',function(e){
e.preventDefault();
var addnew='&addnew=1234';//additional data
var _data = $('#myform').serialize();
_data = _data+addnew;
console.log(_data);
});
sample jsfiddle

Hiding div when is submitted and send values to PHP

<?php
echo $_POST['textvalue'];
echo $_post['radiovalue'];
?>
<div id="hidethis">
<form method="POST" action="">
<label>Tekst Value</label>
<input type="text" name="textvalue">
<label>Radio Value</label>
<input type="radio" name="radiovalue" value="autogivevalue">
<input type="submit" id="submit" name="submit" value="submit">
</div>
http://jsfiddle.net/Bjk89/2/ here is it with the jQuery.
What i try to do is to hide the <div id="hidethis"> when it's clicking submit.
I know i can make another page where i can recieve the values without the <form> section, but i want to put both in one page, make the <div id="hidethis"> hidden after submit.
So i'll be able to get echo $_POST['textvalue']; and echo $_post['radiovalue']; as results
RESULT MUST BE LIKE
A Text // This is the value you input into Tekst Value
autogivevalue // This is the value from the radio button
----- INVISIBLE -----
<form is hidden because we set it in jQuery so>
</form>
Try this. No need to use jQuery here.
<?php
if($_POST) {
echo $_POST['textvalue'];
echo $_post['radiovalue'];
} else {
?>
<form method="POST" action="">
<label>Tekst Value</label>
<input type="text" name="textvalue">
<label>Radio Value</label>
<input type="radio" name="radiovalue" value="autogivevalue">
<input type="submit" id="submit" name="submit" value="submit">
</form>
<?php
}
?>
Try adding '#' in your jquery code. Your version does not have # next to submit. Also your form is missing a closing tag here and in your JSFiddle code.
Try this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#submit').click(function () {
$('form').submit();
$('#hidethis').hide();
});
});
</script>
<form method='post' id="hidethis" name='form'>
<input type="text" name="textvalue">
<input type="radio" name="radiovalue" value="1">
<input type="button" id="submit" name="submit" value="submit">
</form>

how to submit the form with a different actions

I'm trying to retrieve information from database with submit buttons , which have the same name . I'm giving value which is id of the row , so I could make a different actions, but I can not make up my mind how to do that .
When trying like this for example
<form name="form" action="index.php" method="post">
<input type="image" name="x" value="1">
<input type="image" name="x" value="2">
</form>
and php =>
<?php
if (isset($_POST['x'])){
echo $_POST['x'];
}
?>
in chrome it works fine , but in other browsers it doesn't , any ideas ? thanks
UPDATE
if ($r = $con->query("SELECT * FROM table")){
if ($row = $r->fetch_assoc()){
echo "<input type='image' name='submit' value='" . $row['id'] . "'>";
// and other data which I need to
}
$r->free();
}
I meant like this
HTML
<form id="form1" action="index.php" method="post">
<input type="button" name="x" value="1">
<input type="button" name="x" value="2">
<input type="text" name="action" id="action">
</form>​
​
JavaScript
var nodes = document.getElementById('form1').childNodes;
for(var i=0; i<nodes.length; i++) {
nodes[i].onclick = function(){
if(this.name == 'x'){
document.getElementById('action').value = this.value;
}
};
}
PHP
if(isset($_POST['action'])){
if($_POST['action'] == "1"){
...
}
if($_POST['action'] == "2"){
...
}
}
​http://jsfiddle.net/qLubz/
Just change type="text" to type="hidden" when ready to deploy.
Alternatively with no JavaScript:
<form action="index.php" method="post">
<button type="submit" name="action" value="delete">Delete</button>
</form>​
<form action="index.php" method="post">
<button type="submit" name="action" value="copy">Copy</button>
</form>​
<form action="index.php" method="post">
<button type="submit" name="action" value="edit">Edit</button>
</form>​

validate two forms on submit

I have two forms, first form contains the textbox and second form contains a button. I want to validate the textbox using the button (jquery or javascript) :
<form name="contactus" method="post">
<input name="txtFirstname" id="Firstname" type="text" class="field" Value="First name*" style="width:300px" />
</form>
<form name="frm1" action="sendemail.php" method="post">
<input id="send" type="image" src="images/sub.png" alt="Submit" style="float:right" />
</form>
how would i be able to do this when my textbox is not on 'frm1'
Here are examples of the two approaches I suggested in the comments above:
1) Using Javascript to alter the action attribute of the form depending on the button that was clicked:
<form id='the_form' action='' method='post'>
<input type='text' name='text_input' />
<input type='button' id='button_1' value='Button 1' />
<input type='button' id='button_2' value='Button 2' />
</form>
<script type='text/javascript'>
var theForm = document.getElementById('the_form');
$('#button_1').click(function() {
theForm.action = 'script1.php';
theForm.submit();
});
$('#button_2').click(function() {
theForm.action = 'script2.php';
theForm.submit();
});
</script>
2) Holding all the submit handler code in a single script (recommended):
HTML:
<form id='the_form' action='script.php' method='post'>
<input type='text' name='text_input' />
<input type='submit' name='submit_1' value='Submit' />
<input type='submit' name='submit_2' value='Submit' />
</form>
PHP:
<?php
if (isset($_POST['submit_1'])) {
echo 'You clicked button 1';
} else if (isset($_POST['submit_2'])) {
echo 'You clicked button 2';
}
Did you try this?
$('#send').click(function(){
var textVal = $('#Firstname').val();
//your validation code goes here
});

Categories