I have a HTML form that writes user entered data to a file and puts it into a specific directory using a PHP script which is working well.
Here is my HTML page:
<form id="form" action="write.php" class="u-clearfix u-form-spacing-25 u-form-vertical u-inner-form" name="form" method="POST" style="padding: 10px;" redirect="true" redirect-address="write.php">
<div class="u-form-group u-form-name u-form-partition-factor-2">
<label for="field1" class="u-custom-font u-heading-font u-label u-text-body-alt-color u-label-1">field1</label>
<input id="field1" type="text" placeholder="field1" name="field1" minlength="7" maxlength="15" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$" input required class="u-border-1 u-border-custom-color-1 u-custom-font u-heading-font u-input u-input-rectangle u-radius-50 u-white u-input-1" required="required">
</div>
<div class="u-form-email u-form-group u-form-partition-factor-2">
<label for="field2" class="u-custom-font u-heading-font u-label u-text-body-alt-color u-label-2">field2</label>
<input id="field2" type="number" placeholder="field2" name="field2" class="u-border-1 u-border-custom-color-1 u-custom-font u-heading-font u-input u-input-rectangle u-radius-50 u-white u-input-2" required="required">
</div>
<div class="u-form-group u-form-select u-form-group-3">
<label for="field3" class="u-custom-font u-heading-font u-label u-text-body-alt-color u-label-3">field3</label>
<div class="u-form-select-wrapper">
<select id="field3" name="field3" class="u-border-1 u-border-custom-color-1 u-custom-font u-heading-font u-input u-input-rectangle u-radius-50 u-white u-input-3" required="required">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
<option value="option4">option4</option>
</select>
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="12" version="1" class="u-caret"><path fill="currentColor" d="M4 8L0 4h8z"></path></svg>
</div>
</div>
<div class="u-form-group u-form-partition-factor-2 u-form-select u-form-group-4">
<label for="field4" class="u-custom-font u-heading-font u-label u-text-body-alt-color u-label-4">field4</label>
<div class="u-form-select-wrapper">
<select id="field4" name="field4" class="u-border-1 u-border-custom-color-1 u-custom-font u-heading-font u-input u-input-rectangle u-radius-50 u-white u-input-4">
<option value="1">100 (Default)</option>
<option value="10">1000</option>
<option value="100">10,000</option>
<option value="1000">100,000</option>
</select>
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="12" version="1" class="u-caret"><path fill="currentColor" d="M4 8L0 4h8z"></path></svg>
</div>
</div>
<div class="u-form-group u-form-partition-factor-2 u-form-group-5">
<label for="field5" class="u-custom-font u-heading-font u-label u-text-body-alt-color u-label-5">field5</label>
<input type="number" placeholder="field5" id="field5" name="field5" class="u-border-1 u-border-custom-color-1 u-custom-font u-heading-font u-input u-input-rectangle u-radius-50 u-white u-input-5" required="required">
</div>
<div class="u-align-left u-form-group u-form-submit">
<a href="#" class="u-btn u-btn-round u-btn-submit u-button-style u-custom-color-2 u-custom-font u-heading-font u-radius-50 u-btn-1">Submit</a>
<input type="submit" name="submit" value="submit" class="u-form-control-hidden">
</div>
Here is my PHP script:
<?php
if ( isset( $_POST['field1'] ) && isset($_POST['field2']) && isset($_POST['field3']) && isset($_POST['field4']) && isset($_POST['field5']) ) {
$path = '/usr/local/data/' . trim($_POST['field1']) . ',' . trim($_POST['field2']) . ',' . trim($_POST['field3']) . '.conf';
if ( $fh = fopen($path,"a+") ) {
$string = 'field1='.$_POST['field1']. "\n". 'field2='.$_POST['field2']. "\n". 'field3='.$_POST['field3']. "\n". 'field4='.$_POST['field4']. "\n". 'field5='.$_POST['field5'];
if ( fwrite($fh,$string) ) {
} else {
}
fclose($fh);
} else {
}
} else {
}
?>
I have slowly been adding more inputs into the HTML form for more config options for the user however I am having an issue with creating non-required inputs. For example all the fields in the current form have to be entered to submit the form however I want to add a new field which is not mandatory yet if this is completed the PHP script will write it to the .conf file and if it isn't completed not write anything.
Currently it write the input to a .conf file like so as all of the fields have to be completed:
field1="field1 input"
field2="field2 input"
field3="field3 input"
I would like it so if I add say field6 to the HTML which sometimes may be completed by the user and sometimes not, the PHP script writes the .conf file like the below.
If field6 is populated on submit:
field1="field1 input"
field2="field2 input"
field3="field3 input"
field6="field6 input"
If field6 is not populated on submit:
field1="field1 input"
field2="field2 input"
field3="field3 input"
I can do the required config on the HTML however I am not sure how this would work on the PHP side. Could someone please point me in the right direction or help me out with this? Thanks in advance!
I managed to resolve this by usingtrim()
for each of the possible field variables that may be passed to the PHP script.
if ( trim( $_POST[field1] ) !="" ) {
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.