Viewed   235 times

Thank you for reading. I'm trying to create a HTML form so that my friend can type text into it and thereafter, updates his web site with whatever is typed into the form. I'm trying to create a HTML form (on a php page) which posts whatever is entered within it's textarea to the home.php file. However, rather than simply do a "one-off" post, I'm trying to make it so that whatever is entered within the textarea saves the data into the home.php file. The home.php file is blank, and the form which I have created is as below:

<form method="post" action="home.php">
    <textarea id="element" name="element" rows="15" cols="80" style="width: 80%">
    </textarea>
    <input type="submit" name="save" value="Save" />
    <input type="reset" name="reset" value="Reset" />
</form>

For example, if the words "example" was typed into the form then submitted, the home.php file should have the words "example" written on it.

If you require more details, then please reply. Thank you. :)

 Answers

3
<?php

 $Input = $_POST['element'];

 $FileToUpdate = "home.php";
 $fh = fopen($FileToUpdate , 'w') or die("can't open file");

 fwrite($fh, $Input);

 fclose($fh);     

 ?>

The code above will do what you wish, but will overwrite the page (to append see this reference). But really I think you need to start from basics with a good PHP Tutorial.

Saturday, September 3, 2022
 
2

Thanks to everyone Now I finally got the output after editing the proces.php script by changing the line from $csv_line = array(); to $csv_line = $keys; and from if(!file_exists($fname)){$csv_line = "rn" . $csv_line;} to if(!file_exists($fname)){$csv_line = $csv_line."rn" ;} here is my final .php file

 <?php
 $fieldA = $_POST["textnames"];
 $fieldB = $_POST["fathername"];
 $fieldC = $_POST["paddress"];
 $fieldD = $_POST["personaladdress"];
 $fieldE = $_POST["sex"];
 $fieldF = $_POST["Stream"];
 $fieldG = $_POST["Core-1"];
 $fieldH = $_POST["Core-2"];
 $fieldI = $_POST["Elective-1"];
 $fieldJ = $_POST["Elective-2"];
 $fieldK = $_POST["Elective-3"];
 $fieldL = $_POST["Elective-4"];
 $fieldM = $_POST["Compulsory"];

 $keys = array($fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,$fieldI,$fieldJ,$fieldK,$fieldL,$fieldM); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
 $csv_line = $keys;
 foreach( $keys as $key ){
     array_push($csv_line,'' . $_GET[$key]);

 }
 $fname = 'data.csv'; //NAME OF THE FILE
 $csv_line = implode(',',$csv_line);
 if(!file_exists($fname)){$csv_line = $csv_line."rn" ;}
 $fcon = fopen($fname,'a');
 $fcontent = $csv_line;
 fwrite($fcon,$csv_line);
 fclose($fcon);
 ?>
Tuesday, November 29, 2022
 
mervs
 
1

The good news is that PHP and JavaScript have a similar idea about what values are true and false.

  • An empty string will be false on both sides. A string with something in it (except 0 in PHP) will be true on both sides.
  • The number 0 will be false on both sides. All other numbers will be true on both sides.

Since the values of a form will always be strings, as Quentin pointed out in his answer, a good practice might be to use an empty string as false value and something else (e.g. 'true') as true value. But I think your way of using 0 and 1 and testing the numerical values is the safest approach because it isn't misleading. (When someone sees 'true' they might think 'false' would also be usable for a false value.

Saturday, August 20, 2022
1

This code is working. You need to add some condition, that checks, if $username is posted or not.

Something like that:

if(count($_POST)){
    $username ='';
    if(isset($_POST['user'])){
        $username = $_POST['user'];
    if ($username==null || !$username)
         echo 'username is null';
     echo strlen($username);
     echo $username;
   }

 }
Thursday, August 18, 2022
 
adnaan
 
1

I see some errors in your HTML.

I formatted your source locally. I dont know if you did this on purpose (just copy a small part of the table) but there are some tags missing.

I corrected your HTML, maybe you should try that. I dont know if it solves the problem. But here are some errors:

  • You never close the <b> tags in the first table row.
  • There is a table data tag (<td>) you do not close (And which is one too many) after Week 2 hours.
  • You do not close the table row tage for the first row. (<tr>)
  • You do not close the form tag.
  • The word form between the method and action in your opening form tag is incorrect. This should be removed i guess.

<html>

<form method="POST" action="result.php">
<table>
    <tr>
        <td><b>Day of Week</b></td>
        <td><b>Week 1 Hours</b></td>
        <td><b>Week 2 Hours</b></td>
    </tr>
    <tr>
        <td>Monday</td>
        <td><input type="text" name="Monday" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"> <input type="checkbox" tabindex="-1" name="Stime1">Sick?<input type="checkbox" tabindex="-1" name="Vac1">Vacation?</td>
        <td><input type="text" name="Monday2" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"> <input type="checkbox" tabindex="-1" name="Stime2">Sick?<input type="checkbox" tabindex="-1" name="Vac2">Vacation?</td>
    </tr>
</table>
<input type="submit" value="submit">
</form>
</html>

Let me know if this helps. Cheers.

Thursday, October 6, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :