John Davidson

html - My browser keeps sending me to the raw PHP file when I try to send form data to a raw text file?

0 comments
Message:


sorry if the answer is really simple but I wanted to send data from a form on a modal to a raw text file using PHP but I can't seem to get it to work. It just redirects me to a PHP file and doesn't generate the text file.


HTML form:


                <!-- The Modal -->
<div class="modal fade" id="myModal">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">

<form action="action.php" method="POST">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title"#>Input your details</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>

<!-- Modal body -->
<div class="modal-body">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" required value="Enter Username" name="username" id="usr">
</div>
<div class="form-group">
<label for="psw">Password:</label>
<input type="password" class="form-control" id="psw" required value="Password" name="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}">
</div>
</div>

<!-- Password validation -->
<div id="message">
<h3>Password must contain the following:</h3>
<p id="letter" class="invalid">A <b>lowercase</b> letter</p>
<p id="capital" class="invalid">A <b>capital (uppercase)</b> letter</p>
<p id="number" class="invalid">A <b>number</b></p>
<p id="length" class="invalid">Minimum <b>8 characters</b></p>
</div>

<!-- Modal footer -->
<div class="modal-footer">
<button type="submit" value="Submit" name="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
</div>

PHP:


<?php
$path = 'data.txt';
if (isset($_POST['username']) && isset($_POST['password'])) {
$fh = fopen($path,"a+");
$string = $_POST['username'].' - '.$_POST['password'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
exit;
}
?>

I appreciate any help, thank you!



My Answer:
It looks like your PHP code is correct for writing the form data to a text file. The issue might be with the form action attribute in your HTML code.

Make sure that the action attribute in your form tag is pointing to the correct PHP file. In your case, it should be action="action.php" since that is the file where you are processing the form data.

If the issue persists, you can try adding an echo statement in your PHP code to see if the form data is being submitted correctly. This way, you can debug and see if the PHP code is being executed when the form is submitted.

Additionally, you can check the file permissions of the directory where you are trying to create the text file. Make sure that the directory has write permissions so that the PHP script can create the text file.

I hope this helps! Let me know if you have any other questions.

Rate this post

5 of 5 based on 1839 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us