0

I know this topic was already discussed a few times but I can't seem to find what I'm doing wrong.

What I'm trying to do: The user types in a number and by clicking on the button creates a table with that number of columns.

Heres the php:

<?php
$twig = require_once('bootstrap.php');
$hostname = 'localhost';
$username = 'root';
$password =  '';
$conn = new PDO("mysql:host=$hostname;dbname=mydb", $username, $password);

echo $twig->render('index.html', array());

$numOfRows = 1;

if(isset($_POST['button'])){
    $numOfRows = $_POST['num_input'];
}

html/javascript:

<html>
<head>
<script>
function insertRows(){
    var numOfRows = <?php echo json_encode($numOfRows) ?>;
    var out = "<table><tr><th>test</th>";

    for (i = 0; i < numOfRows; i++){
        out += "<th>test</th>";
    }

    out += "</tr></table>";
    document.getElementById("table").innerHTML = out;
}
</script>
</head>

<body>
<form action="index.php" method="post">
  <textarea id="num_input" name ="num_input"></textarea>
  <button type="button"  name="button" onclick="insertRows()"> Go </button>
</form>

<p id="table"></p>

</body>
</html>

Theres no error or anything since I'm not using a IDE, just doing it in vim but the error is that is just doesn't happen. If i change "numOfRows" in the for loop to a number it works, so I'm pretty sure the json_encode is the problem.

Thanks!

EDIT: Just to test it, I used a string variable $str = "test"; the php file, and instead of using the for loop, I just edited javascript to

var str = <?php echo json_encode($str); ?>;
alert(str);

and I also tried

var str = <?php echo $str; ?>;
alert(str);

but nothing works.

6
  • That's the js var numOfRows not the php one. Why would I have to use $ in javascript variable? Commented Oct 12, 2015 at 15:29
  • why are you using json ? Commented Oct 12, 2015 at 15:32
  • 1
    Why is numOfRows actually a count of the number of columns? Commented Oct 12, 2015 at 15:32
  • devlin carnate: why not, what else would be good to use? I'm new to php and I'm just playing around, I'm happy to try out something else :) Commented Oct 12, 2015 at 15:37
  • What do you see when you do View Source? Commented Oct 12, 2015 at 15:40

3 Answers 3

4

json_encode is not necessary in this case.

Simply replace

var numOfRows = <?php echo json_encode($numOfRows); ?>;

with

var numOfRows = <?php echo (int)$numOfRows; ?>;
Sign up to request clarification or add additional context in comments.

2 Comments

Added semicolons inside PHP part. Maybe it will help.
If that also doesn't work, then there's nothing wrong with json_encode(). The problem is that $numOfRows isn't defined when that line of code runs. $numOfRows is defined in your first code block, but how are the two code blocks connected to each other? Setting it in one place doesn't magically make it work elsewhere.#
0

Edit: You are missing a ; on the

<?php echo json_encode($numOfRows) ?>

Should be

<?php echo json_encode($numOfRows);?>

And in these cases, if would be good to check the server log, this will automaticly make you better at finding these mistakes yourself.


You are mixing up ints and strings. The database will in PHP always return strings and the way you are using the variable as an int in a for loop.

The following change i believe would achieve the right result.

$numOfRows = intval($_POST['num_input']);

Where you use PHP's conversion to integer function there is at a global level.

8 Comments

But i < numOfRows will convert numOfRows to an integer before comparing, so the result should be the same.
Tried it but unfortunately it didn't help :/
Updated my post, got that version working in phpfiddle without the javascript thou
; at the end doesn't help either.
@Martin Henriksen how do I do that? :) Do you mean debugging in developer tools?
|
-1

You did not forget any $. JS does not need $ for variables.

As far as your json_encode is concerned, if you are just passing an integer from PHP to JS, there is no need to json_encode. Just pass the variable to JS as <?=$numOfRows?> in the JS source.

2 Comments

Don't assume they have short-tags enabled.
WIth PHP 5.4+ the <?= (echo shorthand) is available without having short tags enabled ... granted you're then assuming they've either got short tags enabled or 5.4+ : php.net/manual/en/migration54.new-features.php

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.