How to properly pass form data in php? - php

I'm using php to build a small form that requires passing 2 variables for processing.
I got the example off w3schools and although the info gets passed in the URL, the php form doesn't process it in any way (let alone extract it).
I'm just wondering if there might be anything wrong with my WAMP server.
<html>
<body>
welcome
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
</body>
</html>
HTML form:
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

Indeed as already noted by various posts, this code should work.
In my experience the php tags are output directly if they're not parsed.
Do you see anything on the page at all? You could also check the sourcecode of the page and check if you can spot anything wrong there.
Also when copying from examples on the internet, sometimes you get weird characters that interrupt the parser. Usually this results in an error (which is not the case here), there's no harm in checking though.
Try outputing something simple and see if that works:
<?php echo "Hello World"; ?>
Can't think of anything else at the moment...

This should work fine code-wise. Did you save the php file in the same directory as 'welcome.php'?

Your code seems correct. Does PHP work? Does
echo phpinfo();
work? If yes, what does
var_dump($_SERVER);
give you? Do the GET parameters appear there?

Just add <?php echo phpinfo(); ?> after that run your page for example page http://localhost/yourpage.php again, now just see whether error_reporting = E_ALL and display_errors = 1 is set or not if not then you have to configure your php.ini,

Check URL if you can view values in url and still can't able to fetch it then try var_dump($_REQUEST); and my suggestion is to try to submit form in same page because when i was fresher i write both codes in same page but action page is different so i was confused why its not working

This script should work as far as I can see. What is your WAMP-setup?
Try to upload the script on a free server that uses php and run it there.

Related

Content type HTML on PHP Page

I found a Webpage saved as something.php. But the source code tells me <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I also found out that PHP code does not work on the webpage.
What is the need for making the file extension PHP if HTML is used?
(Not exactly HTML, but XHTML)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
What is the need for making the file extension PHP if HTML is used?
(Not exactly HTML, but XHTML)
Considering your comments so far, particularly you stated there is no PHP; you can just change the file extension to XHTML. You can always change it back.
I wonder what other PHP files exists where you "found" this page and why. Assuming someone before you developed the site, there is probably a reason they used PHP file extensions.
Unless your host doesn't support PHP, then you should be able to run php code anywhere on that page by placing it inside "" tags. The 'Content-type' isn't relevant to whether PHP can run or not. Try adding the following code somewhere in your page:
<?php echo "Hey there, I'm a friendly PHP string!"; ?>
add this <?php echo "Hello!"; ?> in your page to test, and make sure that your server is running, and normally it works
Are you using a wamp/mamp server? Have you tried to turn it on?
These code are meta tags
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and it has nothing to do with php unless you have included a php script to it.
Html/XHtml will run even though you are not using a php server. All php files has a .php file extension and will run only if you use a server like wamp for windows or mamp for mac.
You can still use html/xhtml code in a .php file.
For example I have an <h1>This is h1</h1> tag and you want to make it dynamic, you can put <?php ?> inside the tag and echo it out to display, <h1><?php echo "This is h1"; ?></h1>.
In case you want to put html code inside a php script, you can do it like this
<?php echo "<h1>This is h1</h1>"; ?>
You can learn more about php and other programming languages by the help of google. Just take your time, relax and enjoy learning. Don't pressure yourself, remember learning is not a medicine that when you take it, it will work in a few minutes. Learning takes time and practice. Enjoy coding.

GET parameters raise a 406 error

I have this simple PHP script:
<!DOCTYPE html>
<html lang="es-ES" xmlns="http://www.w3.org/1999/xhtml" xml:lang="es-ES">
<head>
<meta charset="iso-8859-1" />
</head>
<body><p><?php
if(isset($_GET['echo'])){
echo $_GET['echo'];
}
?></p>
<form action="" method="get">
<input type="text" name="echo" value="" />
</form>
<p>áéíóúàèìòùñ</p>
</body>
</html>
Submitting the spanish word "cañón" generates the GET parameter string ?echo=ca%F1%F3n, that my hosting answers with a 406 Not Acceptable error. It's important to me to keep the charset iso-8859-1, and have the script file codified in ANSI.
Using the accept-charset="UTF-8" attribute in the form element dodges the error, but the word echoed into the page shows as "cañón".
Is there some way of configuring Apache, hopefully using .htaccess, for avoiding this behaviour and rendering correctly the word "cañón" on the resulting page?
Thanks for your help.
Finally, the solution to the problem was to ask to my hosting to check the mod_security configuration. They fixed it in some way, and now the script works fine.
I think you can't use the word 'echo' as a variable since it is one of the php syntax words. Try using a different variable.

Simple HTML form and PHP throwing error

I am just calling a simple PHP script through a HTML form.
An error is thrown everytime : "The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol"
I have defined the encoding both in PHP as well as HTML as UTF-8 (please refer to code below). I am unable to solve this problem despite searching all over the web.
HTML code :
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<meta content="UTF-8" http-equiv="encoding"/>
<title>Test</title>
</head>
<body>
<div align="center">
<form action= "google1.php" method="get" accept-charset="UTF-8" >
Enter Your Name:
<INPUT TYPE = "text" NAME = "student"> <BR>
<!--input name="q" type="text"-->
<br/ >
<input name="btnG" type="submit" value ="test">
</form>
</div>
</body>
PHP Code
header("Content-type: text/html; charset=UTF-8");
print "<pre>";
print_r($_GET);
print "</pre>";
The result after submitting the button (along with the error) is :
"; print_r($_GET); print ""; ?>
I am using XAMPP. I tried to edit .htaccess (added : AddType 'text/html; charset=UTF-8' html) as suggested in some of the solutions over internet but that also did not help.
I found a site where there is a simple form which calls a PHP script again. http://www.tjhsst.edu/~dhyatt/superap/form1.html . When I try to submit value in the form, I get the same error.
So I thought, this could be a browser problem and I changed the default encoding of my browser to UTF-8. But this also did not help.
I am a novice in web programming and trying to learn. Appreciate if any one can help.
Thanks,
Ashutosh
It looks like you have some issue with opening/closing tags.
Ensure that you have php code wrapped with <?php and ?> in your process1.php3 (Some details: http://php.net/manual/en/language.basic-syntax.phpmode.php)
Like here:
<?php
header("Content-type: text/html; charset=UTF-8");
print "<pre>";
print_r($_GET);
print "</pre>";
?>
UPD:
After a long session of question/answer finally appeared that OP were opening file with a form using file:// protocol. Like file:///C:/xampp/htdocs/example/form.html and form were submitted to file:///C:/xampp/htdocs/example/google1.php?... As apache works with HTTP protocol only, PHP were not executed actually.
Your Code:
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
Correct code:
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
You have forget to put slash at the end of meta-tag. You have not closed the meta tag.
Though, its not very crucial, as you have tried everything. Try this one too. It might work for you.

Is this a right way to make php template?

Here is my code
<?php include('includes/header.php'); ?>
<?php
include_once "mysql_connect.php";
mysql_query("UPDATE viewcounter SET `views` = `views`+1 WHERE id='1'");
$sql = mysql_query("SELECT * FROM viewcounter WHERE id='1'");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$pagename = $row["pagename"];
$views = $row["views"];
};
?>
<!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>Like Dislike</title>
<meta name="Description" content="Like Dislike " />
<meta name="Keywords" content=" likedislike,tk,thumbs up,thumbs down,like,dislik,love,hate,tk" />
</head>
<body>
<center>
<h1>Like/Dislike</h1>
<a href="what-should-be-here" onMouseOver="image1.src=loadImage1.src;" onMouseOut="image1.src=staticImage1.src;">
<img name="image1" src="http://www.likedislike.tk/images/left-blue.png" border=0></a>
<img src="images/logo.gif" alt="desert" width="126" height="168" align="middle" /><a href="http://www.likedislike.tk/2-justin-bieber" onMouseOver="image2.src=loadImage2.src;" onMouseOut="image2.src=staticImage2.src;">
<img name="image2" src="http://www.likedislike.tk/images/right-blue.png" border=0></a>
<br />
<?php echo ThumbsUp::item('1')->template('thumbs_up_down') ?>
<?php echo ThumbsUp::item('1')->template('mini_poll') ?>
<?php print $views; ?> views
</center>
</body>
</html>
<?php include('includes/footer.php'); ?>
Now i am confused..
where should i put this <?php include('includes/header.php'); ?>
should i put inside body or outside.
and i want to add more java script so should i load them in header.php or in the page ?
What is the best way to speed up everything and load those java script fast?
Can i load all the java script,css and some php script in header.php?
Thanks i am only 14 so don't tell me i am stupid :)
I try it and it works but i just wanted to know its right way or not.
Thanks
It seems like your code <?php include('includes/header.php'); ?> has different purpose than you assumed. It seems it is designed to deal with HTTP request headers and should be invoked before any output is sent to the browser.
You should be aware that not even a space should be sent (displayed) before header.php is executed (assuming it contains header() PHP function calls) - please see documentation.
Secondly, if you want to include JavaScript, you should do it in HTML head section or later, in the body. The way you should solve it varies depending on what you want to achieve. The best way is to combine as much JavaScript as possible in one file and compress it, do similar thing with CSS (combine all files and compress them).
By combining and compressing JS and CSS files you are making less requests to the server and less requests will be blocked (browsers have limits for concurrent requests to same domain).
Here is some documentation on including JavaScript in HTML documents: www.w3.org/TR/html4/interact/scripts.html.
JavaScript includes and additional CSS files are part of the HTML markup, and should probably be in the <head> area.
You'll typically list your PHP includes at the top of a file. The PHP is executed by the web server, and only outputs text if you use echo, print, etc. You might pick apart some WordPress themes or PHP tutorials and look at how (and pay close attention to why) people structure their markup and PHP the way they do. There's usually no right or wrong way to do things, but there are best practices to follow and the best overall layout/plan depends on what you're trying to do.
It's better to load in header is only that you need for loading page correctly. Because each
script tag will lock your page loading until this one loads. So if you have big javascripts your users will see blank page long time(minutes, maybe hours :)))).

PHP - Escaping em dashes

I am $_POST'ing the following headline from a form:
Google’s New Partner Android Update Initiative: Very Promising — Maybe; We’ll See
And on the handler page, if the first thing I do is
echo "<pre>";
print_r($_POST);
die();
I see:
Google’s New Partner Android Update Initiative: Very Promising — Maybe; We’ll See
I understand that there are functions to convert & escape characters and their HTML equivalents, but how can I ensure that this content is added into the $_POST in the correct encoding?
Cheers,
not sure if that helps, but it seems like the UTF-8 encoding got mixed up (control characters seem somewhat familiar to me ...). Try to output with utf8_encode() or utf8_decode().
Both the page with the form, and the displaying page need to use the same charset. To reproduce the behavior you show, I had to create 2 pages, one a form, with
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
which posted to a page without that.
If I put the meta tag in both pages, it outputs correctly, if I remove it from both pages, it outputs correctly.
If only the form has it, you get what you posted, and if only the receiving page has it, you get the ?'s.
test.php
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<form method="post" action="test2.php">
<input type="text" name="string">
<input type="submit">
</form>
test2.php
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<?php
if(isset($_POST['string']))
{
echo "<pre>";
print_r($_POST['string']);
die();
}
?>
If I paste your string into the input box in test.php, hit submit, I get it back properly in test2.php. If I remove the first line of test2.php, I get the behavior you describe.

Categories