embed html form to php code - php

I have a form on on html outside of php...
<form method="post" action="">
<input type="text" name="user"/></br>
<input type="submit" value="submit" name="login"/>
</form>
then call submit button from php and do this
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply"/>
</form>
this;
if(isset($_POST["apply"]))
{ print "it works";}
}
Alright, so the problem is that, "it works" won't print from the second form thats inside the php. it just takes me back to where i came from. Perhaps it's a dumb question, please help though! thanks

The problem is that by the time you're checking if(isset($_POST["apply"])) the login condition becomes invalid because everything is inside the if(isset($_POST["login"])).
Try taking the if(isset($_POST["apply"])) outside the login IF.

Your "apply" code exists only INSIDE the login test code. When you submit that second form, there will be NO login form field, because you didn't include an input/textarea of that name in the second form. So the second form submits, there's no login, and the entire inner code never gets executed. You probably want:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="" name="apply">
<input type="hidden" name="login" value="foo" /> <!-- add this line -->
etc...

I'm not sure to understand what you wanna do with this code but you obviously missed some details :
_You did not set the "action" field in your form tag, so I don't understant how you would like the PHP file to get called ?
_Your code if(isset($_POST['login'])) has no sense, you are testing the existence of a value sent by a validation button, you'd rather whrite isset($_POST['user'])
Hoping to have helped you

Your variables are declared in 2 forms, so there will be 2 calls (completely independant) to your php.
So you could have a second submit button inside your second form:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply" value="Second"/>
</form>
this;
}
if(isset($_POST["apply"]))
{ print "it works";}

Related

Confused about creating a "login" form

So what I want to do it kind of like a login form, but rather than it being individual users, it's more of a password locked page.
Here's sort of what I have for php
<?php
$user = $_POST['user'];
if($user == "placeholder")
{
include("randomfile.html");
}
else
{
if(isset($_POST))
{?>
<form id="login" method="POST">
User <input type="text" name="user" id="userID"></input><br/>
<input type="submit" name="submit" value="Go"></input>
</form>
<?}
}
?>
and it's basically doing what I want it to do, but if you were to go back (like use the go back button in the browser) it doesn't get rid of that submitted text (in this case, it would be "placeholder").
Any suggestions of any other way to do this, maybe easier or more basic because I just started with php, and is it possible so that if you enter "placeholder" and submit it, then go back, it doesn't have the User field already filled out with what you previously submitted?
<form id="login" method="POST" autocomplete="off">
That work for all the form, I think is the easiest. Ref: form:autocomplete

window.print function call after form validating form in php

I have a problem, i want to print a a div after form validating and then submitting. Issue is, when call print.window function in submit button, it display print window without validating form fields.
input type="submit" value="Submit" class="button" name="submit" onclick="window.print()">
while the div which to be print is in
if($_POST['submit'])
{
echo"print div here";
}
Please help.
Thanks
If you want to print something after form validating and then submitting, you are supposed to do it in client side.
You can do something like this:
<form name="myForm" action="action.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
Then, validate form like this and print.
<script>
function validateForm() {
// validate your form here
window.print();
}
</script>
Hope it helps.

How can I write the data entered in a form to a text file on button click?

I'll preface this with I'm not a coder nor aspiring to become one.
I just want to play around with something simple.
Please don't feel bad about spoon-feeding me here haha.
All I want is when I hit a my submit button the text entered in the text field is saved to a file called log.text
I want it to overwrite each time.
Once data has been written I want it to redirect to another page.
Tried this but it doesn't create the file nor write to it even if I create it manually. The redirect also doesn't work because I'm an idiot.
Any help guys? :(
<?php
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
?>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
It is because the action element of the form is empty.
It should be \n
<form action="action.php(or any other php file that is handling the form)" method="post" name="form">
This guide offered me the solution I was after.
Thanks anyway guys!
http://www.howtoplaza.com/save-web-form-data-text-file
Because you dint checked whether the form is submitted or not. if submited create log. code given below
<?php
if(isset($_REQUEST['submit']))// if to check whether submit name is passed or not
{
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
}
?>
<html>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
</html>

Cannot echo the values from a simple php form

First time i try to create a simple form using the POST method.Problem is when i click the button nothing gets echoed.
here is my insert.php file :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
EDIT: I tried the GET method and it works...Any ideas why that happened? Server configurations?
NEW EDIT: So it turns out i switched method to GET and it worked.Then i switched back to POST (like the code i posted on top) and it works...I have no clue why this happened.Any suggests?
The code you have posted is perfectly valid and should work.
I'm going to guess that you do not have PHP enabled, or it is not working.
<?php ... ?> looks to the browser like a long, malformed HTML tag, and therefore ignores it, making the effect invisible.
Try right-clicking the page and selecting View Source. If you see your PHP there, then the server is indeed not processing it.
The most likely reason for this is probably the same problem I had with my very first bit of PHP code: you're trying to "run" it directly in your browser. This won't work. You need to upload it to a server (or install a server on your computer and call it from there)
Use !empty($_POST['newitem'] instead:
if(!empty($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
empty()
Try the following:
if($_POST) {
if(!empty($_POST['newitem'])) {
$itemnew = $_POSΤ['newitem'];
echo $itemnew;
// or leave it as is: echo $itemnew = $_POSΤ['newitem'];
}
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
The if($_POST) will make sure the code is only executed on a post. The empty() function will also check if it isset() but also checks if it is empty or not.
Try this :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
$_SERVER['PHP_SELF']; is pre-defined variable in php.It allows the user to stay on same page after submitting the form.

Creating a iframe search page for my site

It is very difficult for me to put in words my query. But I will try.
I have a site xyz.com which has search facility for listed products. The search page url is generated like this :www.wyz.com/search/search_term
I want to create a iframe page in a third party site with a search facility which can directly communicated with my site xyz.com.
I have tried to create a search box with a submit button. I want to append the search query in as a variable to my form action url string.
So the search string should look like this :www.wyz.com/search/my_string_variable
The code I have written is:
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
}
?>
<?php
$result=$url.$r1
?>
<html><body>
<form action="<?php echo $result; ?>" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
==================================================================
But output what I get, is only "http://www.xyz.com/search/". It removes my variable from the url. I am not able to find what is the reason? I have also tried to print result via to check the actual output and it shows that it has added the value at the end of url. But when I want to achieve the same thing via form action it does not work. please help?
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
$result=$url.$r1;
header("location:$result");
}
?>
<html><body>
<form action="" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
Please try the above code. I have made some modifications. The main reason your code is not working is whenever you press the submit button it is going to the the url "http://www.xyz.com/search/" directly .The if condition is never executed. In the above mentioned code it will work properly
action="" - you are submitting to the wrong url. Here is alternate version -
<?php $url='http://www.xyz.com/search/';
if (isset($_POST['submit'])) {
$r1=$_POST['num1']; header("Location: ".$r1); // 302 redirection
}
?>
<html><body> <form target="_SELF" method="post"> Num1:<input name="num1" type="text" /><br /> <input type="submit" name="submit" /> </form> </body></html>

Categories