PHP print message - php

i want to print a message from php code, when the user clicks on button. I added code in php, and placed and if condition that executes when the user clicks on the button.
Here's the code of default.php:
<?php
if (isset($_POST['button1'])) {
echo '<b>Ok.</b>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP3</title>
</head>
<body>
<p> </p>
<p> </p>
<p> </p>
<form id="form1" name="form1" method="post" action="default.php">
<p>
<label for="textfield1">User Name</label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="textfield2">Password</label>
<input type="password" name="password" id="password" />
</p>
<p>
<input type="submit" name="button1" id="button1" value="Submit" />
<br />
<br />
<label id="label1">
</label></p>
<p> </p>
</form>
<p> </p>
</body>
</html>
"Ok" should be printed.
But i see no "Ok" message get printed.
What can i do?
Thanks

Could you try to add <?php phpinfo(); ?> in your body tag ? It should confirm if php is installed on your server.
Edit, just saw your last message, if your server is not running PHP, then you have to install it to achieve the result you are looking for.

if you're just testing your PHP file locally then try installing XAMMP to test your php file again, if it works then PHP wasn't installed before. XAMMP includes PHP, MySQL and Apache.

you should submit the form to itself or leave it blank. Check the form action and make sure you are posting to right php file.

You're echoing "Ok" before the DOCTYPE declaration -- it may be that it's there and you're not seeing it in the browser. Have you tried viewing the source and making sure your "Ok" isn't really there?
You might want to move your php code into the body part of your document.

If you say you can't see the PHP code when you click view source in your web browser you don't have PHP installed on your server. You need PHP to run this script. You would need to get in touch with your web host or enable it on your web server if its your own.

Related

need help getting form to submit/go from a submit area

so I'm currently following this tutorial on PHP forms and ive made it to the bottom of this page where it says to test the form so far, http://www.webreference.com/programming/php/search/2.html
"Save your file and test your form at this point (search_display.php)."
When I go to type in a users name in thew design view of dreamweaver and hit submit nothing happens (as if the page is refreshing) however, when i navigate to my file from my MySQL (XAMP and phpMyADmin) server directory i can view my search page but there is no text box/area for me to enter in any data. I have followed/copied this tutorial verbatim and im quite unsure on why I can not get my data to submit to the query. Here is the code for my search page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search Contacts</title>
</head>
<p>
<body>
<h3>Search Contacts Details</h3>
<p>You may search either by first or last name</p>
<form method="post" action="search.php?go" id="searchform">
<p>
<input type="submit" name="submit" value="Search">
</p>
</form>
</body>
</p>
</html>
Thanks guys for the help.
Your HTML form is missing a text field. Please check the HTML sample on the first tutorial page.
Side note: you really should not be following this tutorial as it is greatly outdated, teaches bad practices and will not work when using PHP 7+ due to the removed mysql extension.
1. Place this code where you need the form. For example index.php . There is no input field in your form. So add input fields. For example i am adding email and mobile no as input fields
<h3>Search Contacts Details</h3>
<p>You may search either by first or last name</p>
<form method="post" action="search.php" id="searchform">
<input type="text" name="email" value="">
<input type="text" name="mobileno" value="">
<input type="submit" name="submit" value="Search">
</form>
2. Create another page called search.php and place the below code here
if(isset($_POST['submit'])){
$email = $_POST['email'];
$mobileno = $_POST['mobileno'];
}
Validate all the fields before inserting into database.

i get the error message on my server using xampp

Im getting this error message using xampp i couldnt figure out if it was the code or i needed to configure something in xampp im using windows 7
it works usually but for some reason the _get crashes it i think
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
here is my php code
<?php
if (isset($_GET('day'))){
};
//echo $_get('day');
$text = 'Hello World.'
?>
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<form action="index.php" method="GET">
day:<input type="text" name="day"value=""><br />
date:<input type="text" name="date"value=""><br />
name:<input type="text" name="name"value=""><br />
email:<input type="text" name="email"value=""><br />
<input type="submit"value = "submit">
</form >
</body>
</html>
i know this question has been asked before but i couldn't find a helpful answer

PHP script not returning a variable

I've taken the following code and placed them in their respective files. I've opened up register.html in Firefox and after entering the required details to the form it loads welcome.php albeit with out the entered information. The loaded page simply reads: "Welcome! You are years old!"
Both files are stored in the same folder.
Any suggestions on what I'm doing wrong?
register.html
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
welcome.php
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
For Apache:
1) Ensure the mime module is on.
2) Try making an .htaccess file in the same directory, with the following contents:
AddType application/x-httpd-php .php
This will only work if the rewrite module is on.
3) Otherwise, if you have access to the httpd.conf file, try adding that line somewhere in it, without the .htaccess file.
Make sure to restart the server any time you make changes to httpd.conf.
Edit:
I should have also mentioned to view source of the page, to see if the PHP code is in it. which would make these steps necessary.
Another troubleshooting technique is to add <?php error_reporting(E_ALL); ?> at the top of the page, in case PHP actually is running and since there are no errors. If it is disabled, it will enable reporting an Undefined index notice. It seems highly unlikely that it would be the case, but it also seems like a useful step that could help us understand exactly what's going on. Then again, I'm pretty sure PHP just isn't executing.
Close your body tag.
<body>
not
<body
I used the code as you posted it exactly, even with the broken body tag, and it still worked.
You should take a look at your apache installation and check that it's working correctly.
I'd recommend you run a simple phpinfo test, which is basically a document (ending with .php) with only this in it:
<?php phpinfo(); ?>
You can use this
<?php
if ($_POST) {
extract($_POST);
if (!empty($fname) || !empty($age)) {
echo 'First Name: '.$fname.' Age: '. $age;
} else {
echo 'Enter first name and age';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form</title>
</head>
<body>
<form action="" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

Action in Form is printing the file name rather than invoking the action's php file?

I am very new to php programming and particularly in eclipse. I am having this problem that wasn't happening before.
My file is called index.php, I have only one file called, which is index.php, and I have this very simple code in it. Basically my code just call itself:
<form id="form1" name="form1" method="post" action='index.php'>
<label>
<input name="textfield" type="text" id="textfield" />
</label>
<label>
<input type="submit" name="submitcontent" id="submitcontent" value="extract files" />
</label>
</form>
When I run the code and click the submit button, instead of reloading index.php as I have put in the action request, the output is a white page with a string "index.php". I spent several hours trying to find the error with no hope.
Can you please advice what is my mistake,
What is keeping me wondering is if I run this code in dreamweaver, it works perfectly i.e. reloading the index.php as it is suppose to do.
Many thanks
// EDIT: Here is the full code as requested..
In eclipse I use debug as PHP script. Does it matter ?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body class="twoColLiqLtHdr">
<form id="form1" name="form1" method="post" action='index.php'>
<label>
<input name="textfield" type="text" id="textfield" />
</label>
<label>
<input type="submit" name="submitcontent" id="submitcontent" value="extract files" />
</label>
</form>
</body>
</html>
Thank you
I have solved the problem. So here are the mistakes I made:
1- My problem wasn't really with PHP or Eclipse but rather with the server. My debugger wasn't configured correctly.
2- I was debugging using PHP script and not PHP webpage. The former can debug without the need to connect to the server (just a script), while the latter debug the web page on the server.
Once I ensured that my debugger is setup correctly using php -m then setting PDT was a piece of cake.

Getting input from text area and echoing it out

I have written some basic code so that a text area is displayed, and when the user inputs the text, clicks submit and it shows on the page in the method=''.
The code i have for the form is :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Fun Translator</h1>
<form method="post" action="query.php">
<textarea name="txtarea">
</textarea>
<input type="submit" />
</form>
The code on the page query.php is:
<html>
<body>
<?php
echo $_POST["txtarea"];
?>
</body>
</html>
By looking at google, other questions and such, this should work, but doesnt!
Solution:
Thanks to Marc Audet, I put in phpinfo and all that came up in a big table as it does, I took it out and it started working. Any explanations?
this code is perfectly fine. something else is going wrong.
The textarea is not link to your form, you should add form="" in textarea like this:
<form method="post" action="query.php" name="myform">
<textarea name="txtarea" form="myform">
use
echo htmlspecialchars($_POST['name']);
to get value.
some browsers like the ID...
add an ID like this:
<form method="post" action="query.php">
<textarea name="txtarea" id="txtarea">
</textarea>
<input type="submit" />
</form>
Best guess: Your server doesn't support PHP.
Check the source code as delivered to the browser. If the <?php bit shows up, then the server hasn't run the page through a PHP engine.
As far as I know you should be doing textarea in the quotes not txtarea, I am not really sure though, it might be beneficial to use an ID instead.

Categories