-3

In my project some php codes need to be inside the javascript codes. I typed following codes. But its wrong and i am not skilled on javascript. Can you help me to edit this code. Error is around the $rowno variable. $rowno is undefined in php code whereas $rowno is ok in first line in var html of javascript. How i can defined $rowno for php...

<script type="text/javascript">

    $(document).ready(function (e) {
        $("#add").click(function (e) {

            $rowno = $("#container div.sss").length;
            $rowno = $rowno + 1;

            var html = "<div class='sss' id='row" + $rowno + "'>This Form</div><div class='col-sm-12'><?php include_once 'db.php'; $sql = mysqli_query($connection, 'SELECT * FROM service'); while ($row = $sql->fetch_assoc()) {
                    echo "<label><input name='checkbox["$rowno"][]' value='$row[id]' type='checkbox'>$row[title]</label>";
                } ?></div></div>";

            $("#container").append(html);
        });

    });

</script>
emen
  • 170
  • 2
  • 19
  • 1
    `+` is not used to concat within php. I recommend pulling out as much php from the js block as possible. Connect and query before you begin your js then only output php data once you've entered the js block. – mickmackusa Aug 18 '18 at 06:43
  • 2
    Your file is a PHP file or JavaScript file?! – mcssym Aug 18 '18 at 06:44
  • @mcssym php file – emen Aug 18 '18 at 06:44
  • @mickmackusa Thanks.But it have problem without '+' – emen Aug 18 '18 at 06:45
  • You are mixing oo with procedural mysqli_ syntax, choose one or the other. I always quote wrap my associative keys. Do you have a working connection? Any errors in your log? Or dev console? Adding `$` to your js variables only adds to my confusion when trying to breakdown the code. – mickmackusa Aug 18 '18 at 06:47
  • You don’t kneed to concat PHP. Remove `+ "` before `` – mcssym Aug 18 '18 at 06:47
  • @mcssym it have problem without *+* before ** when i deleted this. – emen Aug 18 '18 at 06:53
  • You are trying to access $rowno in PHP but it is defined in JavaScript. Replace `"+$rowno+"` by `\"+$rowno+\"`. Your way PHP thinks $rowno is a variable and execute it. – mcssym Aug 18 '18 at 06:56
  • @mickmackusa no error in consol.my form is like this image.https://i.stack.imgur.com/AIFid.png . user click to add row and java script run.adding checkboxes(i removed som inputs from top code) checkboxes must come from database – emen Aug 18 '18 at 07:01
  • I have offered several good hints / pieces of advice. After you update your question with improvements I'll re-engage. – mickmackusa Aug 18 '18 at 07:04
  • It seems much of my advice was disregarded. If you don't want php to touch your js variable, leave php before writing the variable, then reenter php on the other side of the js variable. Good luck. – mickmackusa Aug 18 '18 at 07:12
  • 1
    @emen, better off to separate the php from the javascript as the php wont be able to see whatever the js has and vice versa (without passing them between client and server). i assume you are trying to have a button that when clicked add a checkbox with content comes from a query. i suggest, use an ajax to ask the php code to retrieve the said row then the php code returns the row information in json for instance. [see the example for jquery ajax request backed with php on this QA](https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Bagus Tesa Aug 18 '18 at 07:18

1 Answers1

1

Try this one and let me know

<script type="text/javascript">

    $(document).ready(function (e) {
        $("#add").click(function (e) {

            $rowno = $("#container div.sss").length;
            $rowno = $rowno + 1;

            var html = "<div class='sss' id='row" + $rowno + "'>This Form</div><div class='col-sm-12'><?php include_once 'db.php'; $sql = mysqli_query($connection, 'SELECT * FROM service'); while ($row = $sql->fetch_assoc()) {
                    //echo "<label><input name='checkbox["$rowno"][]' value='$row[id]' type='checkbox'>$row[title]</label>";
                      echo "<label><input name='checkbox[\"+\$rowno+\"][]' value='".$row[id]."' type='checkbox'>".$row[title]."</label>";
                } ?></div></div>";

            $("#container").append(html);
        });

    });

</script>
  • You have to use \" without it PHP will execute "
  • You have to use also \$without it PHP will think $rownois a variable
  • And to concat string in PHP we use .not +
mcssym
  • 956
  • 5
  • 8