Roy Monden
Roy Monden

Reputation: 11

Unable to set PHP variables as values for input field in form

I have a PHP file which SELECT's all from the row found based on an SQL query. When I put the echo in a div, I get all information, but when I try to echo it into an input box in a form, it does not shows.

What am I doing wrong?

Please also note that I am aware that I am (most likely) making a lot of mistakes when it comes to security practices or programming standards, but this whole thing (PHPDesktop > https://github.com/cztomczak/phpdesktop) will get packed into an EXE file which will run locally only (no need for an online connection as the SQLite3 DB gets packed in with the EXE), and I am still figuring out how to program this in the first place, so efficient and tidy coding are not high on my list yet ;-)

DO_CUSTEDIT.PHP

$custName = $_POST['custName'];

$query = "SELECT * FROM `CustDB` WHERE CustName LIKE '%$custName%'";

$result = $db->query($query);

while ($row = $result->fetchArray()) {
      $custID = $row['CustID'];
      ......;
}

if (!$result) {
   echo $db->lastErrorMsg();
   $db->close();
   exit;
} else {
   echo $custID;
   echo ......;
   $db->close();
   exit;
}

EDITCUST.PHP / Javascipt

    <script>
      $(document).ready(function() {
        $("#subeditcust").on('click',function(e) {
          e.preventDefault();
          $.ajax( {
            url: "lib/do_editcust.php",
            method: "post",
            data: $("form").serialize(),
            dataType: "text",
            success: function(strMessage) {
              if (strMessage == "Customer updated successfully") {
                $("#message").text(strMessage);
                $("#neweditform").get(0).reset();
                } else {
                  $("#message").text(strMessage);
                }
              }
          });
        });
      });
    </script>

EDITCUST.PHP / HTML

      <form id="editcustform" name="editcustform" action="" method="post">
        <div class="row">
          <div class="column-half" style="background-color:#fff">
            <table>
              <tr>
                <td>
                  <a>Customer ID</a>
                </td>
                <td>
                  <div class="inputb">
                    <input type="text" name="custID" value="<?php echo (isset($custID)) ? $custID: ''; ?>" readonly/>
                  </div>
                </td>
              </tr>
            </table>
          </div>
        </div>
      <div class="row">
        <table style="table-layout:fixed">
          <td style="background-color: rgb(215,215,215); padding:0 10px;">
            <button id="subeditcust" type="submit" class="mainbtn">Create</button>
          </td>
          <td style="background-color: rgb(215,215,215); padding:0 10px;">
            <button id="reseteditcust" type="reset" class="mainbtn">Reset</button>
          </td>
        </table>
      </div>
    </form>

Upvotes: 0

Views: 866

Answers (4)

Roy Monden
Roy Monden

Reputation: 11

I managed to figure it out!

DO_CUSTEDIT.php / PHP

$results = array($custID, $custName);
echo json_encode($results, JSON_PRETTY_PRINT);

EDITCUST.php / HTML

<input type="text" id="custID" name="custID" value="" readonly/>
<input type="text" id="custName" name="custName" value="" readonly/>

EDITCUST.php / JS

    <script>
      $(document).ready(function() {
        $("#subeditcust").on('click',function(e) {
          e.preventDefault();
          $.ajax( {
            url: "lib/do_editcust.php",
            method: "post",
            data: $("form").serialize(),
            dataType: 'json',
            success: function(data){
              document.getElementById('custID').value = data[0];
              document.getElementById('custName').value = data[1];
            }
          });
        });
      });
    </script>

Upvotes: 0

Nilesh
Nilesh

Reputation: 75

EDITCUST.PHP is your first page from you are submitting form to DO_CUSTEDIT.PHP

  1. When you land on EDITCUST.PHP variable $custID is not created
  2. When the form is submitted through ajax, the ajax returns the data inform of object or array depending on how you are echoing the data from DO_CUSTEDIT.PHP I would recommend to use json_encode() php function to return inform of array
  3. To debug the value by logging the data in console (though function console.log())
  4. After returning the value from ajax, you have to populate the value in form through jquery something like $(input[name="custID"]).val( data.custID )

Upvotes: 0

Furqan Ansari
Furqan Ansari

Reputation: 150

<input type="text" name="custID" value="<?php echo (isset($custID) ? $custID: ''); ?>" readonly/>

Replace this line with yours it will work. IA

Upvotes: 1

Volkkerine
Volkkerine

Reputation: 86

It seems you have two different files, right? DO_CUSTEDIT.PHP and EDITCUST.PHP

The variables are being created on DO_CUSTEDIT.PHP and the when you are creating the HTML code those variables ($custID) are not setted.

Is one file including or requiring the other?

Upvotes: 0

Related Questions