Cannot echo the values from a simple php form - php

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.

Related

How to exclude/remove an input after the php code is executed.?

So i have an post input where i submit data something simple.
<form method="post" action="result.php">
<input type="url" name="url" class="form-control" placeholder="http://example.com/">
<input type="submit" name="submit" />
</form>
After the html code is going to be executed a php code which echo success or something like this that doesn't matter.
But i have a problem when i include('submit.php') it's going to show also the input and i don't want this.
How i can do that to don't show the input on result.php?
If you want it to be user-specific, you can try to use cookies or sessions like this:
index.php
<?php
session_start();
?>
<?php if(!isset($_SESSION['show_button']) && !$_SESSION['show_button'] ){ ?>
<!-- Button logic here... -->
<?php } ?>
result.php
// If the url has been entered, it returns a false from empty()
$_SESSION['show_button'] = empty($_POST['url']);

embed html form to php code

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";}

PHP Working with Buttons and Functions

I'm having difficulties on the executing a php function with a button
I've been browsing through the net (and ofcourse here at stackoverflow) for a solution to this problem. However, still no luck.
This problem might have been asked many times in this site, but none of them seems to work to me (or am i just that poorly literate with programming).
What i am trying to achieve is to set a session variable once the button is clicked, and print it in the second page as it redirect.
here's what my first php page looks like:
<?php
session_start();
if(isset($_POST['submit']))
{
$_SESSION['testing']= "hello world";
}
?>
<form method="post" action="update.php">
<input type="submit" id="submit" name="sumbit" value="Submit" >
</form>
and here's the update.php:
<?php
session_start();
echo $_SESSION['testing'];
?>
It may look theres nothing wrong with it, but the script does not execute anything inside the "if(isset(..." statement.
I am using WAMP as a server and my PHP version is 5.3.13
PS: I am aware that PHP is a server-side programming, and what i'm trying to do is something similar to client-side scripting.
The problem is, i do not know how to work with javascripting and what they call it "Ajax" scripting.
Is there any way this could be fix? Is it possible to do this with PHP alone without using javascript or ajax?
Hi simply empty action like <form method="post" action=""> on form tag and redirect after session setting.like header("location:update.php");.Hope solve your problem.Your first page code will be
<?php
session_start();
if(!empty($_POST))
{
$_SESSION['testing']= "hello world";
header("location:update.php");
}
?>
<form method="post" action="">
<input type="submit" id="submit" name="sumbit" value="Submit">
</form>
While on update page
<?php
session_start();
echo $_SESSION['testing'];
?>
$.ajax({
type: "get",
url: "update.php",
success: function(){
alert("success");
// do something
}
});
Above is the example of jquery ajax.
Try this one:
First Page:
<form method="post" action="update.php">
<input type="submit" id="submit" name="sumbit" value="Submit" >
</form>
Update.php:
<?php
session_start();
$_SESSION['testing'] = 'Hello World';
echo $_SESSION['testing'];
?>
Since you want to print the session in the second page, you should put a value of session in update.php not in your first page..

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>

Button Click Counter in PHP

I'm trying to build a tiny skeleton framework for a friend, where each time a button is pressed a certain animation is played. He wants a way to count the number of times the button is clicked, as well, but I can't seem to get that part working. What am I doing wrong?
<?php
if( isset($_POST['mushu']) )
{
echo "Working.";
playAnimation();
clickInc();
}
function playAnimation()
{
/* ... */;
}
function clickInc()
{
$count = ("clickcount.txt");
$clicks = file($count);
$clicks[0]++;
$fp = fopen($count, "w") or die("Can't open file");
fputs($fp, "$clicks[0]");
fclose($fp);
echo $clicks[0];
}
?>
<html>
<head>
<title>Adobe Kitten</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="button"
value="Let's see what Mushu is up to."
name="mushu">
</form>
</body>
</html>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit"
value="Let's see what Mushu is up to."
name="mushu">
</form>
First of all use the form with method="post", or change $_POST[] to $_GET[] in your Script.
And If your Button is not a Submit button, then you are not submitting the form. So I've changed type="button" to type="submit".
Should work
The code looks fine, I tested it and it worked for me.
I suggest:
Make sure the file isn't read-only.
Make sure the file is called "clickcount.txt"
Make sure it's in the same folder as your script.
It would be helpful to know the error but a shot in the dark - it could be a problem with Write permissions?
also, change to:
<input type="submit" value="Let's see what Mushu is up to." name="mushu" />

Categories