简体   繁体   中英

Pass Value From URL Link To Form Field

I have a link in a table from results of an SQL query. Each row has a unique ID. I define this in my script as $id What I am trying to do is have a link that opens up a form in a modal box where a user can insert a note about that row and save it. This then gets inserted into DB with ID, rowID, Note, Date

echo "<td class='agentid'>" . $id. "<br><a href='#notes' data-toggle='modal' data-row-id='".$id."'>Add Note</a></td>" ;

The modal:

<div class="modal" id="notes">
  <div class="modal-dialog">
    <div class="modal-content">
      <form action="postnote.php" method="post">
      <div class="modal-body">
        <div class="modal-header">
          <h4 class="modal-title">Add a Note</h4>
          <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        </div>
        <p>Note will show when page is refreshed</p>
          <label for="fname">Note:  </label>
          <input type="hidden" name="rowID" value=""/>
          <input class="form-control" type="text" name="notes" value="<?php echo date('m/d/Y h:i'); ?> - "/>
          <div class="modal-footer">
            <button type="submit" value="Submit" class="btn btn-primary">Submit</button>
          </div>
        </form>
      </div>
    </form>
    </div>
  </div>
</div>


<script type="text/javascript">
$('#notes').on('show.bs.modal', function(e) {
    var row = $(e.relatedTarget).data('row-id');
    $(e.currentTarget).find('input[name="rowID"]').val(row);
});
</script>

my postnote.php

$pdoOptions = array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false
);

$pdo = new PDO("mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DATABASE, MYSQL_USER,MYSQL_PASSWORD,$pdoOptions);

$data = [
   'notes' => $_POST['notes'],
   'id' => $_POST['rowID'],
];
$sql = "INSERT INTO act_recon_notes (note_id, note) Values(:id, :notes)";
$stmt= $pdo->prepare($sql);
$stmt->execute($data);

header('Location: ' . $_SERVER['HTTP_REFERER']);

I have this exact script on another page and it is working just fine. This does not post the $id into the form. So after submit it is a blank rowID value in the DB.

The issue was I had conflicting JS on the page. I removed the JS that was not needed for this page and the issue resolved.

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM