I have a html form which collects, name, place, email on submission it posts to csv file. But the php file creates a file without any data. Can any body help me in correcting the code. The full code is given below.
HTML form CODE:
<form method="post" name="excisedata" action="csv.php">
Enter Full Name: <br>
<input type="text" name="name"><br>
Enter Place /Location : <br>
<input type="text" name="place"><br>
Enter Valid Email :<br>
<input type="text" name="email"><br>
<input type="submit" name='submit' onclick="show_confirm()" value="SUBMIT">
</form>
PHP CODE
<?php
$name = $_GET["name"];
$place = $_GET["place"];
$email = $_GET["email"];
$list = array($name, $place, $email,);
$file = fopen("mylist.csv","a");
foreach ($list as $line) {
fputcsv($file,explode(',',$line)); }
fclose($file);
?>
first, i would add static values to your fputcsv function to make sure you have no file permissions problems.
fputcsv($file,'bird,dog,fish');
once you confirm that is ok, try this:
your form is posting data, your processor is looking for GET data.
change
$name = $_GET["name"];
$place = $_GET["place"];
$email = $_GET["email"];
to
$name = $_POST["name"];
$place = $_POST["place"];
$email = $_POST["email"];
<?php
$name = $_POST["name"];
$place = $_POST["place"];
$email = $_POST["email"];
$list = array($name, $place, $email,);
$file = fopen("mylist.csv","a+");
fputcsv($file,$list);
fclose($file);
?>
Related
My Goal is to insert the value from a textbox from a form to csv one by one.
I tried to look for answers in the internet but I can't seems to find similar scenario on this problem
<?php
if(isset($_POST['submit']))
{
//read data from form
$lName = filter_input(INPUT_POST, "lName");
$fName = filter_input(INPUT_POST, "fName");
$email = filter_input(INPUT_POST, "email");
$phone = filter_input(INPUT_POST, "phone");
//generate output for text file
$output = $fName . " ";
$output .= $lName . " ";
$output .= $email . " ";
$output .= $phone . " ";
//here "t" means tab
//here "n" newline
//open file for output with append mode "a"
$fp = fopen("contacts.csv", "a");
//write to the file
fwrite($fp, $output);
fclose($fp);
}
else
{
echo "empty submit";
}
?>
<html>
<head>
</head>
<body>
<form action="next_page.php" method="post" >
Lastname:<input type="text" name="lName" />
Firstname:<input type="text" name="fName" />
Email:<input type="text" name="email" />
Phone:<input type="text" name="phone" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
My expected output is Lastname will be save in first cell, Firstname will be saev in 2nd cell, and so on.
Pretty much in the title. I want to make a solution that saves the input data of a html form and display the entire csv file in a separate page that isn't connected. Here's the code I'm currently using that isn't saving the input data nor display the csv file.
Input Data Page:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<h2>PHP Form Validation Example</h2>
<form method="post" name="test1" action="" onsubmit="required()">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Website: <input type="text" name="website">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['website'];
$comment = $_POST['comment'];
$csv =
array($_POST['name'],$_POST['email'],$_POST['website'],$_POST['comment']);
$file = fopen('form.csv', 'a');
foreach ($csv as $line)
{
fputcsv($file,explode(',',$line));
}
fclose($file);
?>
</html>
PHP:Display CSV file
<!DOCTYPE html>
<html>
<?php
echo "</br >";
echo '<table style="width=100%">';
// Print other student details here (minus the private stuff)
//The CSV file is read and printed on the page when using the 'r' value.
$myfile = fopen("form.csv" , "r") or die ("Unable to open file");
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
echo "</table>";
?>
<body>
</body>
</html>
I have recently installed Apache 2.4.7 server, as well as, PHP 5.5.10. I'm just beginning to learn PHP. I'm currently working through the w3school's tutorial on form processing with PHP, but I can't get a particular example to work. Does anyone here see an error?
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Website: <input type="text" name="website"><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male<br>
<input type="submit">
</form>
</body>
</html>
When I visit this website, I get a quote and a greater than sign before the input field name specifically: "> Name. When I submit the form, the URL shows as
http://127.0.0.1/%3C?php%20echo%20$_SERVER[
I can't enter the exact contents of the URL, because this website won't let me, but the %3C actually shows up as a less than sign. The %20 shows up as a space. So, the problem is that the php script inside the action tag does not run. The action variable is then filled with the php script instead of the location of the current page. Why is the PHP script not running inside my form tag?
Solution:
Thank you everyone for helping. Abhik Chakraborty your comment led me to the solution. No, I did not save my file with a .php extension. I changed the extension to .php and it worked perfectly. I would have posted this as a solution, but I have to wait eight hours because I don't have enough reputation points.
If you leave action tag undefined it will use current location.
somefile.php:
<form method="post">
will execute the same uri
Replace action="<?php echo $_SERVER["PHP_SELF"];?>" by action=""
Remove action tag in your form. bcoz action is not mandatory for current page.
if u given $_SERVER['PHP_SELF'] . concatinate to your full path.
Try this one, this should work
<?php echo "<form method='post' action='".$_SERVER["PHP_SELF"]."' >";
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
try this one this is the method posted on w3school to refer to self page at till now faced no problems with it.
you are not processing the data you can try this
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Website: <input type="text" name="website"><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male<br>
<input type="submit">
</form>
Name: <?php echo $name?> <br />
Email: <?php echo $email?> <br />
Gender: <?php echo $gender?> <br />
Comment: <?php echo $comment?> <br />
Website: <?php echo $website?> <br />
</body>
</html>
I worked out a form to fill out on a simple php site, how can I set up a script to put that information in a html/php file and finally save it on the server (file name should be the same name as the $name)
$name = $email = $friendcode = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$friendcode = test_input($_POST["friendcode"]);
}
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name"><?php echo $nameErr;?>
<br><br>
E-mail: <input type="text" name="email"><?php echo $emailErr;?>
<br><br>
Friendcode: <input type="text" name="friendcode"><?php echo $friendcodeErr;?>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
You can use the fopen(), fwrite() and fclose() functions provided by PHP.
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$friendcode = test_input($_POST["friendcode"]);
$file=fopen($name,"a") or die("can't open file");
fwrite($file,$email." ".$friendcode);
fclose($file);
}
This will result in a simple text file, but you can also fwrite() any HTML/PHP code you like.
Try to create a file using fopen,
$save_str = '<p>Name :'.$name.'<br/>e-mail:'.$email.'<br/>friend code'.$friendcode.'</p>';
$handle = fopen($name.'.html','c');
fwrite ($handle, $save_str);
fclose ($handle);
more about fwrite
I want to add new data to an array dynamically using HTML and PHP
Here is my code:
<form action="" method="">
<input type="text" name="name">
<input type="submit" name="send">
</form>
<?php
if(isset($_POST['send']))
{
$name = $_POST['name'];
for($=1;$i<2;$++)
{
$name = array($name);
$names = array_push($names,$name);
}
print_r($names);
}
?>`
Does anyone have a better way?
I prefer to create indexes after its declaration:
$test = array();
$test['a'] = 'value';
$test['b'] = 'value';
$test['c'] = 'value';