2

basically i have this problem of having a button in a while loop to work the way i wanted it to. I have like say 5 rows echoed from my database in a while loop and each of them have a submit button. What i need it to function like is say i press the first row submit button, it will bring over all the data only from the first row to the form handling and update the row in the database. So when i post the data over it should only contain from that row i selected.

The table is something like that

while ($row = mysqli_fetch_array($result)) {

<td>
<td><?php echo  $jobid; ?></td>
<td><?php echo  $jobname; ?></td>
<input type="submit" />

</td> 

The form handling is something like that

$candidate_id = $_POST['can_id'];
$job_id = $_POST['job_id'];
$can_name =$_POST['can_name'];
$job_name =$_POST['job_name'];
$company_name =$_POST['company_name'];
$interviewer =$_POST['interviewer_name'];
$date =$_POST['date_pick'];
$email =$_POST['email'];




$insertQuery = "UPDATE application SET Email_checked = '1', Interview_person     ='$interviewer', Interview_datetime = '$date' WHERE Candidate_id = $candidate_id AND     Job_id = $job_id";
$inserted = mysqli_query($link, $insertQuery) or die(mysqli_error($link));

enter image description here From the image when i press the button "1", no matter which button i press it just changes the last one. Also i am attempting an email function for the specific row

2
  • Post your rendered HTML. The PHP seems irrelevant here. Commented Jan 13, 2015 at 14:28
  • 1
    Use Ajax. It is the simplest Commented Jan 13, 2015 at 14:33

1 Answer 1

1

You need to:

  • Determine which submit button was used to submit the form
  • Determine which data belongs to the row associated with that submit button

You'll generally want to do this using the data in the column you use as the PRIMARY ID in the database.

<button type="submit" name="row_id" value="<?php echo htmlspecialchars($row['id']); ?>">
    Submit this row
</button>

You can then determine which submit button was used by examining $_POST['row_id'].

Then to identify the row that the data belongs to, you can store it in the name of the input.

<input name="row_<?php echo htmlspecialchars($row['id']); ?>[name]" value="<?php echo htmlspecialchars($row['name']); ?>">

Giving output such as:

<input name="row_1[name]" value="Kevin koo">

You can then access the data, after it has been submitted via:

$row = $_POST['row_id'];
$name = $_POST["row_" + $row]['name'];
Sign up to request clarification or add additional context in comments.

2 Comments

i am a little confuse on the "<input name="row_<?php echo htmlspecialchars($row['id']); ?>[name]" value="<?php echo htmlspecialchars($row['name']); ?>">" and "$name = $_POST["row_" + $row]['name'];"
I guess i understand that the value is basically displaying the data on the text field?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.