Submit form on page load - php

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.

Related

Have two forms in one page and set session

i have two file and two forms:
file 1:
<form name="name1" action="form2.php" method="post">
<input <? $_session['define'] = 'value1' ?> type="submit" ...>
</form>
<form name="name2" action="form2.php" method="post">
<input <? $_session['define'] = 'value2' ?> type="submit" ...>
</form>
As you see i have two forms and two different submit buttons but when i press each of them, the second value (the last) set to the $_session['define'] and in the second form i always have 'value2'.
You must post the data to PHP:
<form name="name1" action="form2.php" method="post">
<input name='v1' value='1' type="submit" />
</form>
<form name="name2" action="form2.php" method="post">
<input name='v2' value='2' type="submit" />
</form>
In form2.php:
<?php
session_start();
$_SESSION['value1'] = isset($_POST['v1'])?$_POST['v1']:0;
$_SESSION['value2'] = isset($_POST['v2'])?$_POST['v2']:0;
?>
Personally, I would make it one form and use JQuery/AJAX to POST the values.
<form id="selectForm" name="name1" action="form2.php" method="post">
<input id="v1" name='v1' value='1' type="submit" />
<input id="v2" name='v2' value='2' type="submit" />
</form>
<script>
// Requires JQuery
$("#selectForm input[id^='v']").click(function(e){
e.preventDefault();
$.post(form2.php, { $(this).attr("name"): $(this).attr("value") }, function(){ alert("Selection Saved.) });
});
</script>

Query DataBase With PHP + AJAX/JQUERY

liked to help me. I have a form, and when I write the ID of a user, displays a div with user data with that ID without refreshing the page. How can I do?
MY Form:
<form action="#" method="post" onsubmit="return validar(this)" id="form_aviso" class="form-validate">
<input type="hidden" name="form_send" value="send" />
<b><span name="user">ID Utilizador:</span></b>
<input type="text" name="iduser" class="iduser" id="iduser" />
<input type="submit" class="botao" align="right" value="Submeter" name="send" />
</form>
in jQuery:
var user_id = $("#id_of_that_field").val()
or:
$.post("<server>/get_user_info",{user_id:user_id},function(data){
$("#result_div").html(data)
})

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 an HTML form on loading the page?

How to submit this form without using submit button. I want to submit it in loading this form,
<form name="frm1" id="frm1" action="../somePage" method="post">
Please Waite...
<input type="hidden" name="uname" id="uname" value="<?php echo $uname;?>" />
<input type="hidden" name="price" id="price" value="<?php echo $price;?>" />
</form>
You don't need Jquery here!
The simplest solution here is (based on the answer from charles):
<html>
<body onload="document.frm1.submit()">
<form action="http://www.google.com" name="frm1">
<input type="hidden" name="q" value="Hello world" />
</form>
</body>
</html>
You can try also using below script
<html>
<head>
<script>
function load()
{
document.frm1.submit()
}
</script>
</head>
<body onload="load()">
<form action="http://www.google.com" id="frm1" name="frm1">
<input type="text" value="" />
</form>
</body>
</html>
Do this :
$(document).ready(function(){
$("#frm1").submit();
});
You can do it by using simple one line JavaScript code and also be careful that if JavaScript is turned off it will not work. The below code will do it's job if JavaScript is turned off.
Turn off JavaScript and run the code on you own file to know it's full function.(If you turn off JavaScript here, the below Code Snippet will not work)
.noscript-error {
color: red;
}
<body onload="document.getElementById('payment-form').submit();">
<div align="center">
<h1>
Please Waite... You Will be Redirected Shortly<br/>
Don't Refresh or Press Back
</h1>
</div>
<form method='post' action='acction.php' id='payment-form'>
<input type='hidden' name='field-name' value='field-value'>
<input type='hidden' name='field-name2' value='field-value2'>
<noscript>
<div align="center" class="noscript-error">Sorry, your browser does not support JavaScript!.
<br>Kindly submit it manually
<input type='submit' value='Submit Now' />
</div>
</noscript>
</form>
</body>
using javascript
<form id="frm1" action="file.php"></form>
<script>document.getElementById('frm1').submit();</script>
You missed the closing tag for the input fields, and you can choose any one of the events, ex: onload, onclick etc.
(a) Onload event:
<script type="text/javascript">
$(document).ready(function(){
$('#frm1').submit();
});
</script>
(b) Onclick Event:
<form name="frm1" id="frm1" action="../somePage" method="post">
Please Waite...
<input type="hidden" name="uname" id="uname" value=<?php echo $uname;?> />
<input type="hidden" name="price" id="price" value=<?php echo $price;?> />
<input type="text" name="submit" id="submit" value="submit">
</form>
<script type="text/javascript">
$('#submit').click(function(){
$('#frm1').submit();
});
</script>

html form - how to call different php pages based on the button selected

Problem: How to make an HTML Form call different php pages from the action based on what button is pushed?
The code below is the solution I have now, but I figure there must be a better way to do this then creating multiple forms on the page?
<html>
<body>
<form name="entry_form" action="entry_update_script.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="entry_id" value="">
<input type="hidden" name="entry_item_id" value="">
Truck/Railcar/Barge#:<input type="text" name="pro_number" value=""><br>
BOL #:<input type="text" name="bol" value=""><br>
<input type="submit" name="entry_submit" value="Add New Entry!">
</form>
<form name="entry_form_add" action="entry_view.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="entry_id" value="">
<input type="submit" name="submit" value="Add New Item!">
</form>
</body>
</html>
<html>
<body>
<script type="text/javascript">
function submitAction(act) {
document.sample.action = act;
document.sample.submit();
}
</script>
<form name ="sample" action="default.php">
<input type="button" value = "blah1" onClick="submitAction('phpPage1.php')">
<input type="button" value = "blah2" onClick="submitAction('phpPage2.php')">
</form>
</body>
</html>
you might choose the page to go from a dispatcher, it's an extensible and robust solution:
your form
<form action="dispatcher.php" method="POST">
<input type="radio" name="myOption" value="register" />
<input type="radio" name="myOption" value="login" />
</form>
dispatcher.php
$actions = array ('register', 'login');
// validate possible actions
if (in_array($_POST['myOption']), $actions)) {
include ($_POST['myOption'] . '.php');
}

Categories