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';
}
}
?>
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>
My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>
I am learning PHP and in the process of making a search engine. The following code will not echo the users input, in the search field, on the results page.
The search page:
<!DOCTYPE html>
<html>
<head>
<title>Search Engine</title>
<style type="text/css">
body {
background:#F0FFFF;
margin:-80px;
}
form {
margin:25%;
}
</style>
</head>
<body>
<form action="result.php" method="post">
<input type="text" name="user_query" size="80" placeholder="Enter Search Here" />
<input type="submit" name="search" value="Search Now" />
</body>
</html>
The results page:
<!DOCTYPE html>
<html>
<head>
<title>Results</title>
<style type="text/css">
.results {
margin:5%;
}
</style>
</head>
<body bgcolor="#F0FFFF">
<form action="result.php" method="get">
<span><b>Enter Query Here:</b></span>
<input type="text" name="user_keyword" size="80" />
<input type="submit" name="result" value="Search Now" />
</form>
<?php
if(isset($_GET['search'])) {
$get_value = $_GET['user_query'];
echo "<div class='results'>
$get_value;
</div>";
}
?>
</body>
</html>
Can someone tell me why the user's input is not being echoed when the search is run?
If you using method=post
In form tag you must receive you data as $_POST
Also method=get use $_GET
Edit this lines in result page to this
<?php
if(isset($_POST['search'])){
$get_value = $_POST['user_query'];
echo "<div class='results'>{$get_value}</div>";
}
?>
well the problem lies in your form tag you should have used method="get" if you want to get some data from database and if you are using method="post" you should have to use $_POST['search'] so your form tag shuld have to be like this
<form action="result.php" method="get">
and if you are going to use method="post" anyway than your $_GET['search'] should have to be $_POST['search']
but i will suggest you to use method="get" for searching query from database
Can anyone please help me on this:
I am trying to add a classname/ change the css of div using jquery; after I click submit on the form. Means, after clicking submit and when the page reloads this classname/ changes should stick to the target element. I am using php to get the values of form submitted. (no ajax, but if necessary I can modify my code). I tried to do this by jquery but when the page reloads the changes are gone !
Thanks.
update: Here is the sample code(so.php itself) I am working on. after form is submitted and page is reloaded; say I want to add a class to input that is clicked and manage the style in CSS:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<div id="container">
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>
update2: I tried the logic provided by #NoPyGod and it worked. But here is the thing. I am trying to add a class to the button that is selected as well as update the text in the div with id "hello" with the value of input that is clicked or in active state once the form is submitted and page is reloaded. This is the one I ended up but once form is submitted
all the inputs are having the same class name as well the text in div is also not updated.
<html>
<head>
<?php if (isset($_GET['Load'])): ?>
<script>
$(document).ready(function() {
$('#hello').text('Hello');
});
</script>
<?php endif; ?>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Google" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Rediff" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Yahoo" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Sify" />
</form>
<div id="container" >
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
if (isset($_GET['Load1'])) {
$value = $_GET['Load1'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
<div id="hello"></div>
</body>
</html>
When the page is submitted, set a variable like so
$was_submitted = true;
Then in your HTML do something like this
<div id="myid" class="<?php if ($was_submitted) { echo "some_class"; } ?>">
Or if you absolutely insist on using jQuery to change it, include this in your html --
<?php if ($was_submitted): ?>
<script>
$(document).ready(function() {
$("#myid").addClass("some_class");
});
</script>
<?php endif; ?>
Updated:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<!-- I changed this line below -->
<div id="container" class=<?php if (isset($_GET['Load'])) { echo "some_class"; } ?>>
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>
i am doing a simple html form for two languages(English & Hindi) so i show the link for both language for ex English / <a href="#">Hindi after that i need to display the form with corresponding language what user clicked one of the hyperlink. so here how do i find out which language link is clicked?
English / <a href="#">Hindi
<? if($_SESSION['language']=='English'){ ?>
<form action="" method="post" name="englishform">
......
</form>
<? } else { ?>
<form action="" method="post" name="hindiform">
......
</form>
<? } ?>
You can not change PHP code with java script. As PHP code is run on server. Sent to client and then a java script can be run on the result. So you have two options:
Either send both forms. Use css to hide both. An onclick show the corresponding form.
Or the AJAX soltion. Then the user clicks on the link. Use java script to fetch the form from the server (an other URL) and show in in the page.
You could add a hidden field to both forms, containing the selected language.
<form action="" method="post" name="englishform">
<input type="hidden" name="language" value="en" />
...
</form>
<form action="" method="post" name="hindiform">
<input type="hidden" name="language" value="hi" />
...
</form>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.js"></script>
<script type="text/javascript">
$(function() {
$("#showEnglishForm").click(function()
{
$("#hindiFormDiv").hide();
$("#englishFormDiv").show();
});
$("#showHindiForm").click(function()
{
$("#hindiFormDiv").show();
$("#englishFormDiv").hide();
});
});
</script>
</head>
<body>
show English form
show Hindi form
<div id="englishFormDiv" style="display:none;">
<h3>English form</h3>
<form action="" method="post" name="englishForm">
...
</form>
</div>
<div id="hindiFormDiv" style="display:none;">
<h3>Hindi form</h3>
<form action="" method="post" name="hindiForm">
...
</form>
</div>
</body>
</html>
Well, assuming you just want to hide or show a different form based on the hyperlink:
<a class="switcher" rel="englishform" href="#"> English </a> / <a class="switcher" rel="hindiform" href="#">Hindi</a>
<form action="" method="post" id="englishform" style="display:none">
......
</form>
<form action="" method="post" id="hindiform" style="display:none">
......
</form>
<script>
$("a.switcher").click(function(e){
var name = $(this).attr("rel");
$("#" + name).show();
e.preventDefault();
});
</script>
I don't know javascript well enough, but if you get jQuery you can do the following:
English / Hindi
<form action="" method="post" data-lang="en">
...
</form>
<form action="" method="post" data-lang="hi">
...
</form>
jQuery: (needs to be specified more on your actual page or it will target all links and forms)
$('a').click(function(e){
e.preventDefault();
$('form').hide();
$('form[data-lang="'+$(this).attr('href')+'"]').show();
});
Or without jQuery:
<? if(!$_GET['lang'] || ($_GET['lang'] != "en" && $_GET['lang'] != "hi")){ ?>
English / Hindi
<? }elseif($_GET['lang'] == "en"){ ?>
<form action="" method="post">
...
</form>
<? }elseif($_GET['lang'] == "hi"){ ?>
<form action="" method="post" data-lang="hi">
...
</form>
<?
}
?>