How to import txt file options in PHP Form that Echo values on page

I am new to PHP code. I built this form as a note-taking form. I need to be able to import the options from an external document either excel, notepad, therefore, I don’t need to change the form each time for an option change. I can’t figure out how to do it.

<?php // define variables and set to empty values $nameErr = $numberErr = $exposureErr = $oborefErr = $rfcoptionErr = $csroptionErr = ""; $name = $number = $exposure = $comment = $oboref = $rfcoption = $csroption = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); } if (empty($_POST["number"])) { $numberErr = "Number is required"; } else { $number = test_input($_POST["number"]); } if (empty($_POST["oboref"])) { $oboref = ""; } else { $oboref = test_input($_POST["oboref"]); } if (empty($_POST["rfcoption"])) { $rfcoption = ""; } else { $rfcoption = test_input($_POST["rfcoption"]); } if (empty($_POST["csroption"])) { $csroption = ""; } else { $csroption = test_input($_POST["csroption"]); } if (empty($_POST["adjuster"])) { $adjuster = ""; } else { $adjuster = test_input($_POST["adjuster"]); } if (empty($_POST["callertype"])) { $callertype = ""; } else { $callertype = test_input($_POST["callertype"]); } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["exposure"])) { $exposureErr = "exposure is required"; } else { $exposure = test_input($_POST["exposure"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>
        <div class="row align-items-start">
            <div class="col">
                <h2 class="text-center">Call Tracking Tool <span id="timer" style="font-size: 14px; color: black;",></span></h2></div>
        </div>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>">
            <p style="padding-bottom: 0px; margin-bottom: 0px; color: #AF4331; font-size: 14px;"><i class="fas fa-check" style="display:inline;padding-right:3px;"></i>Select Exposure</p>
            <div class="row align-items-start">
                <div class="col form-check">

                    <input class="form-check form-check-inline" type="radio" name="exposure" value="PD">
                    <label class="form-check-label" for="PD">exposure 1</label>
                    <input class="form-check form-check-inline" type="radio" name="exposure" value="Coll">
                    <label class="form-check-label" for="Coll">exposure 2</label>
                    <input class="form-check form-check-inline" type="radio" name="exposure" value="PIP">
                    <label class="form-check-label" for="PIP">exposure 3</label>
                    <input class="form-check form-check-inline" type="radio" name="exposure" value="BI">
                    <label class="form-check-label" for="BI">exposure 4</label>
                </div>
            </div>
            <hr style="margin: 0px; padding-bottom: 10px;">
            <p style="padding-bottom: 0px; margin-bottom: 0px; color: #AF4331; font-size: 14px;"><i class="fas fa-check" style="display:inline;padding-right:3px;"></i>Select Caller</p>
            <div class="row align-items-start" style="padding-bottom: 5px;">
                <div class="col form-check">

                    <input class="form-check form-check-inline" type="radio" name="callertype" value="Insd">
                    <label class="form-check-label" for="Insd">caller 1</label>

                    <input class="form-check form-check-inline" type="radio" name="callertype" value="Clmt">
                    <label class="form-check-label" for="Clmt">caller 2</label>

                    <input class="form-check form-check-inline" type="radio" name="callertype" value="Clmt Ins">
                    <label class="form-check-label" for="Clmt Ins">caller 3</label>

                    <input class="form-check form-check-inline" type="radio" name="callertype" value="Lawyer">
                    <label class="form-check-label" for="Lawyer">caller 4</label>
                </div>
            </div>
            <hr style="margin: 0px; padding-bottom: 10px;">
            <div class="col form-group row">
                <label class="col-3 col-form-label"><i class="fas fa-user" style="display:inline;margin:0;padding:0px;">Name</i></label>
                <div class="col-9">
                    <input type="text" class="form-control" name="name" placeholder="Enter Caller's Name">
                </div>
            </div>

            <div class="col form-group row">
                <label class="col-3 col-form-label"><i class="fas fa-phone" style="display:inline;margin:0;padding:0px;">Number</i></label>
                <div class="col-9">
                    <input type="number" class="form-control" name="number" placeholder="Enter Caller's Number">
                </div>
            </div>

            <div class="col form-group row">
                <label class="col-3 col-form-label"><i class="fas fa-hashtag" style="display:inline;">Ref</i>: </label>
                <div class="col-9">
                    <input type="text" class="form-control" name="oboref" placeholder="Behalf/*Claim#/*Ref#">
                </div>
            </div>

            <div class="col form-group row">
                <label class="col-3 col-form-label" for="inlineFormCustomSelect"><i class="fas fa-question-circle" style="display:inline;margin:0;padding:0px;">RFC</i></label>
                <div class="col-9">
                    <select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="rfcoption">
                        <option selected>defaut rfc option</option>
                        <option name="rfcoption">rfc option 1</option>
                        <option name="rfcoption">rfc option 2</option>
                        <option name="rfcoption">rfc option 3</option>
                        <option name="rfcoption">rfc option 4</option>
                    </select>
                </div>
            </div>

            <div class="col form-group row">
                <label class="col-3 col-form-label"><i class="fas fa-pen-square" style="display:inline;margin:0;padding:0px;">Notes</i></label>
                <div class="col-9">
                    <textarea class="form-control" name="comment" rows="1" col "s="40 " placeholder="Important Information"></textarea>
</div>
  <div class="col form-group row">
  <label class="col-3 col-form-label" for="inlineFormCustomSelect"><i class="fas fa-star" style="display:inline;padding-right:3px;">Action</i></label>
  <div class="col-9"><select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="csroption">
    <option selected>default csr option</option>
    <option name="csroption">csr option 1</option>
    <option name="csroption">csr option 2</option>
    <option name="csroption">csr option 3</option>
    <option name="csroption">csr option 4</option>
  </select>
  <div class="col form-group row">
  <label class="col-3 col-form-label text-right" for="inlineFormCustomSelect"><i class="fas fa-address-book" style="display:inline;margin:0;padding:0px;">ADJ</i></label>
  <div class="col-9"><select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="adjuster">
   	<!-- Beginning Adjuster Selection -->
 	<option selected>Adjuster</option>
	<option name="adjuster">Adjuster 1</option>
	<option name="adjuster">Adjuster 2</option>
	<option name="adjuster">Adjuster 3</option>
	<option name="adjuster">Adjuster 4</option>
  </select>
  	<!-- End Adjuster Selection -->
Submit
<?php echo "

Copy & Paste Text Below

"; echo "Caller: " . $callertype . "-" . $name . "
"; echo "Number: " . $number . "
"; echo "OBO/Claim/Ref#: " . $oboref . "
"; echo "Exposure: " . $exposure . "
"; echo "RFC: " . $rfcoption . "
"; echo "Comments: " . $comment . "
"; echo "CSR Action: Advd adj contact info & " . $csroption . "
"; echo "Adjuster: " . $adjuster . "
"; ?>

From a coding standpoint, the simplest method would be to define the options in php array variables, in a .php configuration file, that you simply require into your main code.

The most general purpose solution would be to store the options in database table(s) and provide an ‘edit’ user interface to define and change the choices.

Other methods that require you read and parse data from a file are not worth using due to the amount of code you will have to write and test.

The method you choose is dependent on who is expected to manage the data.

Next, the php form processing code style you have either came directly or indirectly from w3schools and except for the line testing if the request method is post, is all nonsense and should be thrown away.

The form processing code should detect that a post method form was submitted (which it is doing), trim all the submitted data at once, with a single statement, dynamically validate the data, by looping over an array that defines the expected form fields and what validation each requires, sets validation errors in an array, then finally uses the submitted form data if there are no validation errors. It should NOT create discrete variables for each possible form field and each error and it should not use the w3schools nonsense test_input() function.

Also, if you need to use an Excel file for input, that is quite easy using a library like “PHPExcel”.
With that library, you can pull out data from any cell in an Excel sheet. This library is free.

To read a Notepad file, that is just a text file. There are simple PHP commands that read a text file.
Some read the entire file into a variable and others read one line of the text file at a time.
If you have the optional values always in set places in the text file, it is easy to decode them.

Not sure if any of this helps or not.

Sponsor our Newsletter | Privacy Policy | Terms of Service