Space character issue with page to page form handling with PHP - php

I am trying to pass the 'name' text field input to blank2.php. I can send the data across
but anything after I type the space bar all the other input after it is omitted. What am I missing?
blank.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>First Page</title>
</head>
<body>
<form action="http://www.example.com/blank2.php" method="post">
<label for="name"></label>
<input type="text" name="name" placeholder="Name" id="name">
<div>
<input type="submit" id="submit" name="submit" value="Send">
</div>
<div>
</form>
</body>
</html>
Here is the second page:
<?php
session_start();
$_SESSION['name'] = $_POST['name'];?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Second Page</title>
</head>
<body>
<form action = "http://example.com/blank3.php" method="post">
<label for="name"></label>
<input type="text" name="name" placeholder="Name" id="name" value=<?php echo $_POST['name'];?>>
<br>
<div>
<input type="button" id="submit" name="submit" value="Send Message">
</div>
</form>
</body>
</html>
I'd really appreciate if if anyone can assist me.

Put quotes around the value.
<input type="text" name="name" placeholder="Name" id="name" value="<?php echo $_POST['name'];?>">

Related

html form with Php code not executing

I'm new to PHP. I have this html file with php in it:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Php torturail 1</title>
</head>
<body>
<p>php ahead:</p>
<?php
if (isset($_POST['submit'])){
printf('User Name: %s', $_POST['name']);
}
?>
<form method="post" action="">
<p>name:</p>
<input type="text" name="name">
<p>pass:</p>
<input type="password" name="pwd">
<p>massage:</p>
<textarea name="area"></textarea>
<p>accept:</p>
<input type="checkbox" name="chb" value="on">
<p>lucky number:</p>
<input type="radio" name="group1" value="option1">1
<input type="radio" name="group1" value="option2">2
<p>button:</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
When I open it on on a browser and click submit, the form's fileds are empty again but nothing is printed.
What is the problem?
Thank you.
2 things:
1-you must save your file as .php file.
2-fill action in your form tag
<form method="post" action="where.php">

image uploading using advance php

I am working on oops based project in php when I upload an image it is not uploade with information that I want to upload with this image. Here i connect the controller page I create object for uploading the image and information in database.
Code for html page
<?php
include('include/control.php');
include('include/connect.php');
error_reporting(0);
if(count($_FILES) > 0){
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$i=addslashes(file_get_contents($_FILES['image']['tmp_name']));
$j=getimagesize($_FILES['image']['tmp_name']);
$objectNew=new add;
if(isset($_POST['submit'])){
$info1=$_POST['info1'];
$info2=$_POST['info2'];
$info3=$_POST['info3'];
$info4=$_POST['info4'];
$info5=$_POST['info5'];
$imagetype=$i;
$imageData=$j;
$ob=$objectNew->addInfo($imagetype,$imagedata,$info1,$info2,$info3,$info4,$info5);
}
}
}
?>
<!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>Untitled Document</title>
</head>
<body>
add info<br />
<form name="form" enctype="multipart/form-data" action="" method="post">
<input type="text" name="info1" /><br />
<input type="text" name="info2" /><br />
<input type="text" name="info3" /><br />
<input type="text" name="info4" /><br />
<input type="text" name="info5" /><br />
<input type="file" name="image"/><br />
<input type="submit" name="submit" />
</form>
</body>
</html>
Code for controller
function addInfo($imagetype,$imagedata,$info1,$info2,$info3,$info4,$info5)
{
$addI=$this->conn->prepare('insert into `addinfo` (imagetype,image,info1,info2,info3,info4,info5) VALUES (?,?,?,?,?,?,?) ');
$addI->bind_param("sbsssss",$imagetype,$imagedata,$info1,$info2,$info3,$info4,$info5) ;
$addI->execute();
}
This is just a suggestion.
You could add "required" attribute to your inputs so it won't be submitted if its empty.
This works on latest browser
your code
<input type="text" name="info1" /><br />
<input type="text" name="info2" /><br />
can be
<input type="text" name="info1" required /><br />
<input type="text" name="info2" required /><br />

Passing a url param to a search form

Exact problem: Trying to pass a param from the url to a search box destination page.
The param is "subid" this way:
http://www.domain.com/?subid=3456
and the current (non working) search form code in php:
<!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>
<form action="http://search.yahoo.com/search?subid=<?php print $_GET['subid'];?>&" target="_blank" id="search-box">
<input type="text" name="p" size="31" />
<input type="submit" value="Search" />
</form>
</body>
</html>
I can see with firebug that the subid is in the form action when I go here:
http://www.domain.com/?subid=3456
but when I run a query in the form, is not passing to yahoo.
so where is the problem?
You should post subid as a parameter also, and specify method as get.
<form action="http://search.yahoo.com/search" target="_blank" method="get" id="search-box">
<input type="hidden" name="subid" value="<?php print $_GET['subid'];?>" />
<input type="text" name="p" size="31" />
<input type="submit" value="Search" />
</form>
As was asked by the author, added some filtering for GET parameter:
<form action="http://search.yahoo.com/search" target="_blank" method="get" id="search-box">
<input type="hidden" name="subid" value="<?=(int)$_GET['subid']?>" />
<input type="text" name="p" size="31" />
<input type="submit" value="Search" />
</form>
Or you can use strip_tags + htmlentities functions, before output.

echo "<p>" . $value . "</p>";

Ok my first time asking question here. This as been very helpful in the past but now i'm lost.
I'm trying to understand how php work with the help of a book. So i did the exercise as it was shown in the book and the result if not what it should be.
Here's the code:
<div id="content">
<p>Here's a record of everything in the REQUEST array:</p>
<?php
foreach($_REQUEST as $value) {
echo "<p>" . $value . "</p>";
}
?>
</div>
<div id="footer"></div>
</body>
And here's the result:
Here's a record of everything in the REQUEST array:
" . $value . "
"; } ?>
Why is not showing the info it is suppose to? Thanks.
Ok here's all the code:
showRequestInfo.php
<!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" /> <link
> href="/wamp/www/livrephp/css/phpMM.css" type="text/css"
> rel="stylesheet" />
>
> <title>Untitled Document</title> </head>
>
> <body> <div id="header"><h1>PHP & MySQL: The Missing
> manual</h1></div>
> <div id="example">Example 3-2</div>
>
> <div id="content">
> <p>Here's a record of everything in the REQUEST array:</p>
> <?php foreach($_REQUEST as $value) { echo "<p>" . $value . "</p>"; } ?>
>
>
> </div>
> <div id="footer"></div> </body> </html>
And this goes with this file called "socialEntryForm.php"
<!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" />
<link href="/wamp/www/livrephp/css/phpMM.css" rel="stylesheet" type="text/css" />
<title>Untitled Document</title>
</head>
<body>
<div id="header"><h1>PHP & MySQL: The misiing manual</h1></div>
<div id="example">Example -1</div>
<div id="content">
<h1>Join the missing manual (Digital) Social Club</h1>
<p>Please enter your online connections below:</p>
<form action="../showRequestInfo.php" method="post">
<fieldset>
<label for="first_name">First Name:</label>
<input type="text" name="first_name" size="20" /><br />
<label for="last_name">Last Name:</label>
<input type="text" name="last_name" size="20" /><br />
<label for="email">Email Address:</label>
<input type="text" name="email" size="50" /><br />
<label for="facebook_url">Facebook URL:</label>
<input type="text" name="facebook_url" size="50" /><br />
<label for="twitter_handle">Twitter Handle:</label>
<input type="text" name="twitter_handle" size="50" /><br />
</fieldset>
<br />
<fieldset class="center">
<input type="submit" value="Join The Club" />
<input type="reset" value="Clear and Restart" />
</fieldset>
</form>
</div>
<div id="footer"></div>
</body>
</html>
Are you sure your file is a PHP file and it's being run on a PHP enabled server? The browser seems to be receiving the code unparsed, thinking that there's a tag starting at <?php and ending at the first <p> tag. If you look at the source, you'll probably see your PHP code, untouched by the server.
In other words: Your code is correct and the problem is your file type or server configuration. If you are indeed using a server on your machine, make sure you're running the file right, e.g. if it's in the root, open http://localhost/your_file.php, and not C:\xampp\htdocs\your_file.php.
EDIT: Just for the heck of it, I replicated your issue with a fiddle. I got the exact same output as you, meaning it's not getting parsed by the server. Who said JSFiddle was only good for JavaScript?
$_REQUEST Contains data which is gathered from cookies, $_POST and $_GET .. Are you sure that your data is properly assigned?
Take this example.
print_r($_REQUEST); just doing that without no <form method="get/post"> will produce a blank array, that might be why you are getting nothing
Your snippet is correct.. you are lacking a html form to go with that..
Example:
<form method="POST">
<input type="submit" name="test" value="ThisIsCorrect">
</form>
With your code you have shown your question.. your $_REQUEST array will return the value of the button. In this case "ThisIsCorrect"
Moral Of this?
Ensure that you are using using post/get/cookies before calling the $_REQUEST, and for future reference, just using $_POST & $_GET is cleaner to use.. But that is down to my personal preference.
How is the $_REQUEST Array constructed?
Consider this:
the array will contain the name as the array key and the value as the value..
So taking this into account:
<form method="POST">
<input type="text" name="username">
<input type="submit" name="test" value="ThisIsCorrect">
</form>
For your text box & Submit button the array will be:
array ("username" => "UserInputData",
"test" => "ThisIsCorrect");

$_POST[] not working in php

I've started learning PHP. Managed to setup things.
I'm using php version 5.3.13.
I'm trying to post some info to a html form and receive it in a php file.
For the purpose i'm using $_Post variable and the ouput at the php file is blank.
Below is the html code.
<body>
<form action="report.php" method="POST" >
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" /><br />
<input type="submit" value="Report Abduction" name="submit" />
</form>
</body>
And below is the report.php code
<html>
<head>
<title></title>
</head>
<body>
<?php
$name = $_POST['firstname'] ;
print($name);
?>
</body>
</html>
Can any one advise what i'm missing ?
Thanks
Here is a super simple example, I suggest you begin to look for example tutorials # your favorite search engine, or buy a book.
Edit: Do you even have PHP installed? you mention inetpub which is a IIS path.
<?php
error_reporting(E_ALL);
if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['firstname'])){
//Do something with posted data
$out = htmlentities($_POST['firstname']).' has been abducted!';
}else{
//Form has not been posted so show form
$out = <<<FORM
<form action="" method="POST" >
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" /><br />
<input type="submit" value="Report Abduction" name="submit" />
</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>My first test Script</title>
</head>
<body>
<h1>My first test Script</h1>
<?php echo(isset($out))?$out:null; ?>
</body>
</html>

Categories