0

I have a little problem in my code, please review it and help me. I want to assign value of the span tag(fetched by id in html) to a php variable and then run a sql query to fetch record of the related value in the table. Here is my code.

I tried it in different ways but it not works. I print the sql query and its absolutely right but does not show the correct result. I copied that printed sql query and paste it into phpmyadmin query section and run it there. Then it works fine and show related records. Please help

$regg = '<span id="modal-myvalue"></span>';
$fee_detail = "SELECT * FROM `fee_enroll` WHERE registeration = '$regg' AND mode = 'ENABLE'";
//here i print the query and its output is correct but doesn't show the correct record here.      
echo $fee_detail;
 $result_fee = mysqli_query($con, $fee_detail);
 while($row_fee = mysqli_fetch_assoc($result_fee)) {
 echo $row_fee['registeration'];
 }

The data which i want to fetch in span tag(in id) is sent from a button through this script:

<script type="text/javascript">    
    var ATTRIBUTES = ['myvalue'];
    $('[data-toggle="modal"]').on('click', function(e) {
        var $target = $(e.target);
        var modalSelector = $target.data('target');

        ATTRIBUTES.forEach(function(attributeName) {
            var $modalAttribute = $(modalSelector + ' #modal-' + attributeName);
            var dataValue = $target.data(attributeName);

            $modalAttribute.text(dataValue || '');
        });
    });
</script>

and the button code is

<a class="btn" type=submit data-toggle="modal" data-target="#model-view" data-myvalue="<?php echo $row['registeration']; ?>">VIEW Detail</a>

When i print the query it shows:

(SELECT * FROM `fee_enroll` WHERE registeration = 'FA12-BSE-094' AND mode = 'ENABLE')

which works correctly in phpmyadmin query section. but not works here when i want to execute.

Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19
Archons
  • 11
  • 3

2 Answers2

0

You can try DOMDocument as follows,

$regg = '<span id="modal-myvalue">FA12-BSE-094</span>';
$doc = new DOMDocument();
$doc->loadHTML($regg);
$elements = $doc->getElementsByTagName('span')->item(0);
var_dump($elements->nodeValue);

And then you can use this variable in query like below,

$fee_detail = "SELECT * FROM `fee_enroll` WHERE registeration = '$elements->nodeValue' AND mode = 'ENABLE'";
Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19
  • It works correctly but it has a static value (FA12-BSE-094). I need it in different value as i sending the data from a button through js. – Archons Apr 19 '19 at 07:16
  • @Archons "I need it in different value as i sending the data from a button through js " => share that code in question. – Shoyeb Sheikh Apr 19 '19 at 07:19
0

You cannot insert a variable like that. See this answer.

Complex (curly) syntax

Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$.

Curly braces in string in PHP


My suggestion should be to change the query string into the following:

$fee_detail = "SELECT * FROMfee_enrollWHERE registeration = '{$regg}' AND mode = 'ENABLE'";

RonaldT
  • 338
  • 2
  • 10