Reputation: 3
Well for starter, from my past project, I've built a website that actually make use of Database, PHP, Javascript and Json(this is to parse data to android) that manipulate and display numbers which synchronize with android.
But then... this next project, it require me to actually store and call data into database. Well, no big deal, I've done it before but this is where it became weird when the data I want to call is a string and not integer.
This is the PHP side to call the stored information
<?php
$con=mysqli_connect('localhost','SE_Server','') or die(mysqli_error());
mysqli_select_db($con, 'seproject') or die("cannot select DB");
$info=mysqli_query($con, "SELECT * FROM fact WHERE factID='2'");
$rowC1 = mysqli_fetch_row($info);
if(!empty($rowC1)) {
$A = $rowC1[0];
$B = $rowC1[1];
$C = $rowC1[2];
}
else {
$A = $B = $C = 0;
}
mysqli_close($con);
?>
For Remark
row[0] --> INT ; stored_data = 1
row[1] --> VARCHAR ; stored_data = 520
row[2] --> VARCHAR ; stored_data = user1
And for the Javascript I use to call the stored data is as below
<div>
<input class="text-center" name="box">
</div>
<button onclick="test()">Try it</button>
<script>
var check = <?php echo $B ?>;
function test() {
document.getElementsByName('box')[0].value = check;
}
</script>
Whenever I call row[0] or row[1] that contain only integer, it work fine, just like my previous project. But when it comes to row[2] that contain text, nothing were display and there was a time where it display 'undefined'.
And I know, I can use JSON, JQuery and such... but for the sake of simplifying this project presentation later, let put it aside shall we?
Upvotes: 0
Views: 50
Reputation: 1520
If your value is a string, it needs to be wrapped in quotes to be valid javascript. json_encode()
is probably the best way to do this:
var check = <?php echo json_encode($B) ?>;
Although you could just add the quotes by hand if you know all your data is a string and doesn't contain apostrophes / single quotes etc.
var check = '<?php echo $B ?>';
Upvotes: 3