0

I would like to send 2 variables on a click of a button My Code so far is that:

HTML:

            <?php                   

              while ($Case=mysql_fetch_assoc($records)) {

                echo "<tr>";
                echo "<td>".$Case['SubmissionID']."</td>";
                echo "<td>".$Case['AppID']."</td>";
                echo "<td>".getFullNameApp($Case['AppID'])."</td>";
                echo "<td>".getCourseName($Case['SubmissionCourseID'])."</td>";
                echo "<td>".getDepartmentName($Case['SubmissionCourseID'])."</td>";
                echo "<td>".DateFormat($Case['Date'])."</td>";
                echo "<td>".$Case['SubmissionStatus']."</td>";
                if ($Case['SubmissionStatus'] == 'Draft') {
                  echo "<form action='ApplicantApplyDetails.php' method='POST'>";
                  echo "<td><button type='submit' class='btn btn-success btn-xs' name='modifybtn' value='".array($Case['SubmissionID'], $Case['SubmissionCourseID'])."'>Modify</button></td>";
                }
                else {
                  echo "<td><button type='submit' class='btn btn-danger btn-xs disabled' disabled='disabled' name='modifybtndis' value=''>Modify</button></td>";
                }
                echo "</form>";         
                echo "</tr>";
              }

              mysql_close();
            ?>

PHP:

if (isset($_POST['modifybtn'])) {

$SubmissionID =$_POST['modifybtn'][0];
$CourseID = $_POST['modifybtn'][1];
echo $CourseID;
}

And then get those variables for further processing. Is that possible? I've done single ones but no arrays. My code is not working. The problem I think is in the HTML part, but I have no idea how to fix it. Any hints?

4
  • what does var_dump($_POST['modifybtn']) looks like? start from there, or just follow Xorifelse answer. Commented Jan 4, 2017 at 1:34
  • There are some ways to pass two variables to php script, but i think it is not possible to pass an array. You can pass as a string with delimeter and split on a server, convert to a json and send to a server, create a formData object and add values to it and send to a server, use input hidden fields with values and submit a form in a standard way... Hopefully i gave you an idea. Commented Jan 4, 2017 at 1:36
  • Change name='modifybtn' to name='modifybtn[]' php will pick up that it an array that you are posting. Commented Jan 4, 2017 at 1:47
  • @jeff True, but this is a button, not a checkbox. Commented Jan 4, 2017 at 1:51

1 Answer 1

1

To answer your question; You can convert an array to a string:

$value = json_encode([$Case['SubmissionID'], $Case['SubmissionCourseID']]);
echo "<td><button type='submit' class='btn btn-success btn-xs' name='modifybtn' value='".$value."'>Modify</button></td>";

And covert the string back to the array when reading.

var_dump(json_decode($_POST['modifybtn'], true));

However there is also the option to submit a hidden field:

<input type="hidden" name="courseid" value="<?= $Case['SubmissionCourseID']>">

And just use:

var_dump($_POST);
Sign up to request clarification or add additional context in comments.

1 Comment

The hidden field did the job..Very smart..Thanks Xorifelse

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.