<?php
if(isset($_POST['submit'])){
// Here the code captures the inputs that entered.
Based on the inputs the script shows the associated information.
}
<form method="POST">
<input type="text" name="postc">
<input type="text" name="nr">
<button name="submit">Send</button>
</form>
How to check that both of the inputs are filled and if they are filled automaticly load the PHP code to get the data. (I'm a beginner)
I hope I explained it right!
Example page
http://bit.ly/1hx0EKS
I'd suggest something like below:
HTML
<form method="POST" name="myForm" action="page.php">
<input type="text" name="postc" onchange="submitForm()">
<input type="text" name="nr" onchange="submitForm()">
</form>
Javascript
this function is called anytime you fill an input of your form. It provides to get your input values and submit dynamically the form ONLY when you filled all your (in this case) inputs.
function submitForm() {
var postc = document.forms["myForm"]["postc"].value;
var nr = document.forms["myForm"]["nr"].value;
if (postc != "" && nr != "")
document.myForm.submit();
}
EDIT
This is a full working example
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Your title</title>
<script>
function submitForm() {
var postc = document.forms["myForm"]["postc"].value;
var nr = document.forms["myForm"]["nr"].value;
if (postc != "" && nr != "") {
document.myForm.submit();
}
}
</script>
</head>
<body>
<form method="POST" name="myForm" action="#">
<input type="text" name="postc" onchange="submitForm()">
<input type="text" name="nr" onchange="submitForm()">
</form>
<?php
if(isset($_POST['postc'])){
echo "postc value: " . $_POST['postc'] . '<br />';
echo "nr value: " . $_POST['nr'];
}
?>
</body>
</html>
hope this helps :-)
Related
I'm new to PHP so please bear with me.
I'm trying to decide the action based on the given input.
Given below is the code I've written for this simple task
<html>
<head>
<meta charset="UTF-8">
<title>SAMPLE</title>
</head>
<body>
<form method="POST" action="<?php
if(isset($_POST['submit_button']))
{
if($_POST['name_field'] === "USERA")
{
echo "output.php";
}
else
{
echo "wronguser.php";
}
}
?>">
Username : <input type="text" name="name_field" value="<?php
if(!isset($_POST['name_field']) || $_POST['name_field']=== "")
{
echo "Enter Something" ;
}
?>">
<input type="submit" name="submit_button" value="SUBMIT BUTTON !">
</form>
</body>
</html>
Here I want the form to redirect to "output.php" if the user is "USERA" or "wrongouput.php" for any other user name entered. Although it works as expected, I'm redirected to the right page only when I press the submit button the 2nd time.
Why is this ?
Also, reloading the page doesn't seem to bring back the original page with the text field containing the default "Enter Something" text. I have to run it all over again from the IDE.
Why is this ?
You can try a different approach
<?php
if(isset($_POST['submit_button']))
{
if($_POST['name_field'] === "USERA")
{
require_once("output.php");
}
else
{
require_once("wronguser.php");
}
}
else
{
?>
<html>
<head>
<meta charset="UTF-8">
<title>SAMPLE</title>
</head>
<body>
<form method="POST" action="">
Username : <input type="text" name="name_field" value="<?php
if(!isset($_POST['name_field']) || $_POST['name_field']=== "")
{
echo "Enter Something" ;
}
?>">
<input type="submit" name="submit_button" value="SUBMIT BUTTON !">
</form>
</body>
</html>
<?php
}
?>
Take your code out of the action, make the action for the form self so it fires on itself, then put your PHP above all the HTML at the top of the file. That'll stop the double click issue.
<?php
if(isset($_POST['submit_button']))
{
if($_POST['name_field'] === "USERA")
{
echo "output.php";
}
else
{
echo "wronguser.php";
}
}
?>">
Username : <input type="text" name="name_field" value="<?php
if(!isset($_POST['name_field']) || $_POST['name_field']=== "")
{
echo "Enter Something" ;
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>SAMPLE</title>
</head>
<body>
<form method='post' action="<?php $SERVER['PHPSELF']; ?>">
<input type="submit" name="submit_button" value="SUBMIT BUTTON !">
</form>
</body>
</html>
I have this code (it's an include), but the thing is that when I send the form the $_POST data is not being sent.
Im checking for $_POST data like this
if(isset($_POST['ft_upload']) && $_POST['ft_upload'] == 1){
//$usuario -> uploadFirstTime2($db);
echo "ok";
}
and the code for the form is
<div class="ft_userImage" style="background: url(<?php echo $usuario -> getProfileImage(); ?>);"></div>
<p class="ft_step2_continue"><?=$TEXT_BUTTONS['continue'];?></p>
<form action="" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<script>
$("p.ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
check.php
<?php
if(isset($_POST['ft_upload']) && $_POST['ft_upload'] == 1) {
echo "ok";
}
?>
index.html
<!DOCTYPE html>
<html>
<head>
<title>form</title>
</head>
<body>
<form action="check.php" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<button id="ft_step2_continue">SEND</button>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$("#ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
</body>
</html>
it works fine.
i think, you just forgot action="check.php" in your form tag.
Here check this:
<?php var_dump($_POST); ?>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script>
$("p.ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
</body>
</html>
Ok so i did this. Saved it in a php file. And then triggered the submit and it outputs:
array(1) { ["ft_upload"]=> string(1) "1" }
Your php code should be in the same file where the form html is written because your action attribute is empty.
You need to specify the action attribute.
<form action="check.php" method="POST" class="ft_step2_form_upload">
I think you want transfer a file to server??? if yes:
<form method="POST" action="this-file-name.php" enctype="multiform/form-data">
Your Photo:
<input type="file" name="filet" required="required">
<input type="submit" value="Send Photo">
</form>
<?php
if(isset($_FILES['filet'])) {
$dir = './my_dir_name/';
$file_name = $_FILES['filet']['name'];
$doit = #move_uploaded_file($_FILES['filet']['tmp_name'],$dir.$file_name);
if($doit) {
echo 'File '.$file_name.' uploaded';
}
}
?>
I cannot understand why, in the following code, the else clause is never performed. The form is displayed, but when I press the Submit button nothing happens. Can anyone help? Cheers.
<!doctype html>
<html>
<head>
<meta charset = "utf-8">
<title>Listing 12-3</title>
</head>
<body>
<h1>Query the Shop Database</h1>
<h3>Search for a Product</h3>
<p> Use a wildcard if necessary - % in front / behind text</p>
<?php
tryagain:
// Wait for submit
if (!$_POST['submit']) {
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p> Product Name: <input type ="text" name="product" /></p>
<p><input type="submit" name = "submit" value = "Submit" /></p>
</form>
<?php
}
else {
// Connect to the Shop database
Please change
if (!$_POST['submit']) {
to
if (!isset($_POST['submit'])) {
Hi all this is an easy question but right now i can't think about ..
I've a form where people can insert a personal code like "Abcdef301"
On submit i want to redirect them to url:
http://domain.tld/yoururls/Abcdef301
I've tried with some Post and Get (like the emails form but i've failed)
Any help?
<?php
if(isset($_POST['VAI'])) {
if(isset($_POST['text']) && $_POST['text'] != "") {
header("Location: http://domain.tld/yoururls/".$_POST['text']);
} else {
// handle error of no code in form field here if you want to
}
}
?>
<html>
<head>
<title>page title</title>
</head>
<div id="feedback-form">
<div class="success-block"></div>
<form action="" method="post">
<input name="text" placeholder="Inserire Codice Evento" required type="text">
<input type="submit" value="VAI" class="btn">
</form>
</div>
</html>
Should be what you're looking for.
I keep getting "Undefined index: latitude" and "Undefined index: longitude"
I was trying to make it work for hours, but i am not able to :( Would you please help me?
I need those two forms seperated each other.How can i link them together?
Thanks in advance :)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
<script>
var x=document.getElementById("log");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="GPS szolgáltatás nem müködik ezen a böngészőn, kérlek értesítsd a rendszergazdát!";}
}
function showPosition(position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementById("longitude").value = longitude;
document.getElementById("latitude").value = latitude;
}
</script>
<p id="log">
<form method="post">
<input type="hidden" name= "longitude" id="longitude">
<input type= "hidden" name ="latitude" id="latitude">
</form>
</p>
<br>
<br>
<form action="" method="post">
<input type="submit" name="ido" value="Click" /></td>
</form>
<?php
if(isset($_POST["ido"])) {
echo "<script>getLocation();</script>";
$latitude=$_POST["latitude"];
$longitude=$_POST["longitude"];
print_r($_POST);
}
?>
</html>
That is because your form does not contain the elements latitude and longitude , whereas the above form what you have defined has those fields , but they are never submitted.
<form action="" method="post">
<input type="hidden" name= "longitude" id="longitude"> <!-- I have added here-->
<input type= "hidden" name ="latitude" id="latitude"> <!-- I have added here-->
<input type="submit" name="ido" value="Click" /></td>
</form>
If you need the forms to be independent - then you should consider using a custom submit method and submit via ajax. That will give you the opportunity to parse the elements you need and then submit the data.