I have the following page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
Click Browse and choose a file from your PC<br /><br />
<form method="POST" action="_URL_" enctype="multipart/form-data" name="IMGform">
<input type="file" name="image_upload"><br /><br />
<input class="button" type="Submit" value="Change Image">
<input type="hidden" name="add_image" value="true">
</form>
</body>
It simply produces nothing when I add a file to upload.
var_dump($_POST) only produces:
array(1) { ["add_image"]=> string(4) "true" }
Files are available through $_FILES, not $_POST.
See the documentation for further information.
In PHP when files are uploaded, they are accessible via the $_FILES superglobal, rather than in the $_POST superglobal. See POST Method Uploads
Related
below is my code where i am writing a code in php and calling it in body. When i run it and view source, the code gets visible. I have seen some site where nothing gets shown on source but a form appears on webpage. How can i do it too?
<?php
{ $avatar_form = '<form id="avatar_form" enctype="multipart/form-data" method="post" action="myphoto.php">';
$avatar_form .= '<h4>Change your avatar</h4>';
$avatar_form .= '<input type="file" name="avatar" required>';
$avatar_form .= '<p><input type="submit" value="Upload"></p>';
$avatar_form .= '</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=utf-8" />
<title>Avatar</title>
</head>
<body>
<div><?php echo $avatar_form; ?></div>
</body>
</html>
Thanks
Shail
Why not write it in html? There should not be any code showing like that:
<!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>Avatar</title>
</head>
<body>
<div>
<form id="avatar_form" enctype="multipart/form-data" method="post" action="myphoto.php">
<h4>Change your avatar</h4>
<input type="file" name="avatar" required>
<p><input type="submit" value="Upload"></p>
</form>
</div>
</body>
</html>
just using php to echo html is sometimes not faster or useful then writing it directly in .html files.
Check whether the file type is .php or .html?
if html, it will show the php code.
If so, save it as .php
how to input a number range, from 2 text fields, this is my code is seems to be not working, the range function i want it to be inserted to mysql database but this code is just to test if i can get the 2 textfields to connect to the range function. anyone can help me solve this problem?
<?php
$from = '.from';
$to = '.to';
$number = range('$from','$to');
print_r ($number);
?>
<!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>New Waybill</title>
<link rel="shortcut icon" href="../img/icon.ico">
<link rel="stylesheet" type="text/css" href="../css/forms.css" />
</head>
<body>
<form id="form3" name="form1" method="post" action="">
<p>From:
<input type="text" name="from" id="form_number" class="from" value="0"/>
- To:
<input type="text" name="to" id="form_number" class="to" value="50" />
</p>
<p>Waybill Booklet:
<select name="waybill_booklet" id="form_list">
</select>
</p>
<p>
<input type="submit" name="button" id="form_button" value="OK!" />
</p>
</form>
</body>
</html>
Replace your php code with this:
<?php
if (!empty($_POST)) {
$from = $_POST['from'];
$to = $_POST['to'];
$number = range($from,$to);
print_r($number);
}
?>
To access a form post in PHP you use the $_POST superglobal. More info found here: http://www.php.net/manual/en/reserved.variables.post.php.
I've put a check to see if it's empty or not at the start, so when the page first loads it won't attempt to run the PHP code. Presumably you'll then replace the print_r with whatever you need to insert the data into your database.
I am trying to ensure the parameter from a GET method of page 2 (which displays the result) will not be changed by others in the URL (the variables behind the question mark) by making sure users clicked the submit button on page 1 (which contains the form).
The professor ask us to use isset() to implement this requirement
Here's the login page: (page1.php)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ...> <html
xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Login
Page</title> </head>
<body>
<label>User ID:</label>
<input type="text" name="uid">
<br />
<input type="submit" value="SubmitCredentials" name="SubmitCredentials"/>
<br />
</form> </body> </html>
And this is the second page: (page2.php)
<?php
// get the data from the form
$uid = $_GET['uid'];
if(!isset($_GET['SubmitCredentials'])){
include('error.html');
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional
...>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HELLO</title>
</head>
<body>
<label>Hello,</label>
<span><?php echo $uid; ?></span><br />
</body>
</html>
However, the if statement doesn't seem to be working. It's supposed to forward the page to error.html when I change the variable uid in the URL (e.g. when I change the URL to http://localhost/1/page2.php?uid=admin) but it's not. How can I fix it?
PS: a relevant tutorial I found on google>>>
http://www.homeandlearn.co.uk/php/php4p7.html
Try:
if(!isset($_GET['SubmitCredentials'])){
header('Location: error.html');
}
Isset is not safe on Arrays. Try comparing with the ===operator instead:
if(!($_GET['SubmitCredentials'] === 'SubmitCredentials')) {
include('error.html');
exit();
}
Page #1:
<form method="GET" action="...">
<input>
<input>
<input>
</form>
Submit URL:
http://localhost/1/page2.php?SubmitCredentials=SubmitCredentials&uid=admin&i_like_php=yes&...
I just wanted to create a simple phonebook using php...I used the following code....but one entry overwrites another plz help...I want to do this without using MySQL
<?php
session_start();
if(isset($_SESSION['views']))
{
$_SESSION['views']=$_SESSION['views']+1;
}
else
{
$_SESSION['views']=1;
}
?>
<!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>Phonebook</title>
</head>
<body>
<form action="index1.php" method="post" style="border:thin">
Name: <input type="text" name="varname" style="border:dotted" />
<br/>
Roll Number:<input type="text" name="varroll" style="border:dotted" />
<br />
Phone Number: <input type="text" name="varno" style="border:dotted" />
<br/>
<input type="submit" name="submit" value="Register" /><br/>
</form>
<?php
$test1[$_SESSION['views']]=$_POST['varname'];
$test2[$_SESSION['views']]=$_POST['varroll'];
$test3[$_SESSION['views']]=$_POST['varno'];
for($j=1;$j<=$_SESSION['views'];$j++)
{
echo $test1[$j]." ".$test2[$j]." ".$test3[$j];}
echo "<br/>";
echo "No. of page views=".$_SESSION['views'];
?>
</body>
</html>
You could write it to a text file with | seperating each value or you could use an ini or xml file
You cannot just use $_SESSION, as it will be emptied when the user closes the browser.
Best to do something like this (sample untested code)
//loading
$data = unserialize( file_get_contents( 'mydata.txt' ) );
//editing
$entry = array();
$entry['roll']=$_POST['varroll'];
$entry['name']=$_POST['varname'];
$data[] = $entry;
//saving
file_put_contents( 'mydata.txt', serialize($data) );
I'm trying to require a file upload in a form, but cannot get it to work. Any ideas? I'd rather echo the php error on the page vs. a javascript popup. Thanks for taking a look:
<?php
// initialize $file1
$file1 = $_POST['file1'];
// check upload of $file1
if ($file1 == '' ) {
$error = "Please upload an image";
}
?>
<!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>Require upload of an file</title>
</head>
<body>
<?php if ($error) {echo $error;} ?>
<br /><br /><br />
<form id="form1" name="form1" method="post" action="nextpage.php">
<input type="file" size="8" name="file1" />
<input name="Submit" type="Submit" />
</form>
</body>
</html>
See this tutorial it have all that you need.
To sum up:
Use enctype="multipart/form-data" and method="POST" in the <form> tag.
In PHP use $_FILES['uploadedfile']['name'] to read original name ("uploadedfile" is the name of your file input - "file1" in your example).
In PHP use $_FILES['uploadedfile']['tmp_name'] to read server side temp file name.
In PHP use $_FILES['uploadedfile']['error'] to get the error (if any) see there for possible codes.
Also see the PHP manual for more info.
In your exemple use this form instead:
<form id="form1" name="form1" method="post" action="nextpage.php" enctype="multipart/form-data">
<input type="file" size="8" name="file1" />
<input name="Submit" type="Submit" />
</form>
In "nextpage.php":
//Use $_FILES['file1'] to check the file upload...
print_r($_FILES['file1']);