php - Values null when passed from another page using modal and jquery
I am having problem on passing values into another page using jQuery and modal. So here is my code.
Firing up the modal:
echo "<input type='hidden' data_project='$row[PROJECT_NAME]' data_headmark='$row[HEAD_MARK]' data_id='$row[ID]'></input>";
echo '<button type="button" name="qcreject" id="qcreject" data-toggle="modal" data-target="#qcRejectModal">
<span > '.$row['PROJECT_NAME'].' / <b>'.$_POST["hm"].'</b>/'.$row['ID'].' ~ FAIL</span></button>';
<div >
<div >
<div >
<div >
This is the confirmation window to PASS all the Quality Control for, <br/><br/>
PROJECT : <b>'.$row['PROJECT_NAME'].'</b>
HEADMARK : <b>'.$row['HEAD_MARK'].'</b>
ID : <b>'.$row['ID'].'
</div>
</div>
</div><br/>
<div >
<div >
<div >
<textarea name="reasonForRejection" id="reasonForRejection" placeholder="Reason for rejection" rows="2" required></textarea>
</div>
</div>
</div>
</div>
<div >
<button type="button" data-dismiss="modal">Cancel</button>
<button type="button" name="rejectbutton" id="rejectbutton" >Confirm REJECT!</button>
</div>
and when user click the reject button it should pass PROJECT_NAME, HEAD_MARK, ID and values in the textarea to the testClass.php.
jQuery is,
$('#rejectbutton').click(function() {
$.post("testClass.php",{
projectName : this.getAttribute("data_project"),
headMark : this.getAttribute("data_headmark"),
id : this.getAttribute("data_id"),
reasonReject : this.getAttribute("reasonForRejection")});
});
and inside the testClass.php
echo 'TEST CLASS WORKS';
$val1 = $_POST["projectName"];
$val2 = $_POST["headMark"];
$val3 = $_POST["id"];
$val4 = $_POST["reasonReject"];
var_dump($val1);
var_dump($val2);
var_dump($val3);
var_dump($val4);
unfortunately all the values I'm getting in the testClass are null
TEST CLASS WORKSstring(0) ""
string(0) ""
string(0) ""
string(0) ""
Answer
Solution:
Add
id
attribute in your hidden field then use that injs
like below code.Answer
Solution:
Looking at this code:
it assumes, that when the element with id
#rejectbutton
is clicked, which is:It has the attributes you want on it, but it doesn't. Also, you are trying to get the attributes using
getAttribute()
- why don't you use the jquery method?this is how you do it. Update your HTML to be like this:
Then update your JS to get those attributes in your post request like this: