Call a variable from inside a while mysqli fetch assoc to a function to see if it match the content in a...
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a mysqli fetch assoc query wich give a result by limit of 1 at the time per page.
I have a dropdown list wich have the match to the result of the fetch assoc in one of the selections.
I want an Alert to say your correct and to show the correct answer from the fetch assoc if the answer is correct.
And an Alert that says nice try and show the correct answer from the fetch assoc if the answer is incorrect.
<!--Index page-->
<form action="titles.php" method="get">
<?php while($row5 = mysqli_fetch_assoc($res)): ;?>
<h1 align="center"> Title:<?php echo $row5['Title'] ?></h1>
<h1 align="center">author :<?php echo $row5['author'] ?></h1>
</form>
<!-- Select page, A dropdown -->
<select id="author">
<option value="">Select Major Section</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['authors_id'].'">'.$row['authors'].'</option>';
}
}else{
echo '<option value="">Author not available</option>';
}
?>
</select>
<!-- functionpage-->
<script>
$(function() {
$('#author').on('change', function(event) {
var opt = this.options[ this.selectedIndex ];
var correct = $(opt).text().match ($row5['author']]);
if (correct){
alert('Your answer is correct ' + ($row5['author']);
} else {
alert ('try again' + the ($row5['author']);
}
});
});
</script>
php jquery html mysqli
closed as too broad by John Dvorak, eyllanesc, Machavity, Makyen, Hovercraft Full Of Eels Nov 16 '18 at 23:41
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have a mysqli fetch assoc query wich give a result by limit of 1 at the time per page.
I have a dropdown list wich have the match to the result of the fetch assoc in one of the selections.
I want an Alert to say your correct and to show the correct answer from the fetch assoc if the answer is correct.
And an Alert that says nice try and show the correct answer from the fetch assoc if the answer is incorrect.
<!--Index page-->
<form action="titles.php" method="get">
<?php while($row5 = mysqli_fetch_assoc($res)): ;?>
<h1 align="center"> Title:<?php echo $row5['Title'] ?></h1>
<h1 align="center">author :<?php echo $row5['author'] ?></h1>
</form>
<!-- Select page, A dropdown -->
<select id="author">
<option value="">Select Major Section</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['authors_id'].'">'.$row['authors'].'</option>';
}
}else{
echo '<option value="">Author not available</option>';
}
?>
</select>
<!-- functionpage-->
<script>
$(function() {
$('#author').on('change', function(event) {
var opt = this.options[ this.selectedIndex ];
var correct = $(opt).text().match ($row5['author']]);
if (correct){
alert('Your answer is correct ' + ($row5['author']);
} else {
alert ('try again' + the ($row5['author']);
}
});
});
</script>
php jquery html mysqli
closed as too broad by John Dvorak, eyllanesc, Machavity, Makyen, Hovercraft Full Of Eels Nov 16 '18 at 23:41
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Please include a sample of what you've tried so far. Thanks.
– Nosajimiki
Nov 16 '18 at 17:43
I did, thanks..
– Elvis Cruz
Nov 16 '18 at 17:58
add a comment |
I have a mysqli fetch assoc query wich give a result by limit of 1 at the time per page.
I have a dropdown list wich have the match to the result of the fetch assoc in one of the selections.
I want an Alert to say your correct and to show the correct answer from the fetch assoc if the answer is correct.
And an Alert that says nice try and show the correct answer from the fetch assoc if the answer is incorrect.
<!--Index page-->
<form action="titles.php" method="get">
<?php while($row5 = mysqli_fetch_assoc($res)): ;?>
<h1 align="center"> Title:<?php echo $row5['Title'] ?></h1>
<h1 align="center">author :<?php echo $row5['author'] ?></h1>
</form>
<!-- Select page, A dropdown -->
<select id="author">
<option value="">Select Major Section</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['authors_id'].'">'.$row['authors'].'</option>';
}
}else{
echo '<option value="">Author not available</option>';
}
?>
</select>
<!-- functionpage-->
<script>
$(function() {
$('#author').on('change', function(event) {
var opt = this.options[ this.selectedIndex ];
var correct = $(opt).text().match ($row5['author']]);
if (correct){
alert('Your answer is correct ' + ($row5['author']);
} else {
alert ('try again' + the ($row5['author']);
}
});
});
</script>
php jquery html mysqli
I have a mysqli fetch assoc query wich give a result by limit of 1 at the time per page.
I have a dropdown list wich have the match to the result of the fetch assoc in one of the selections.
I want an Alert to say your correct and to show the correct answer from the fetch assoc if the answer is correct.
And an Alert that says nice try and show the correct answer from the fetch assoc if the answer is incorrect.
<!--Index page-->
<form action="titles.php" method="get">
<?php while($row5 = mysqli_fetch_assoc($res)): ;?>
<h1 align="center"> Title:<?php echo $row5['Title'] ?></h1>
<h1 align="center">author :<?php echo $row5['author'] ?></h1>
</form>
<!-- Select page, A dropdown -->
<select id="author">
<option value="">Select Major Section</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['authors_id'].'">'.$row['authors'].'</option>';
}
}else{
echo '<option value="">Author not available</option>';
}
?>
</select>
<!-- functionpage-->
<script>
$(function() {
$('#author').on('change', function(event) {
var opt = this.options[ this.selectedIndex ];
var correct = $(opt).text().match ($row5['author']]);
if (correct){
alert('Your answer is correct ' + ($row5['author']);
} else {
alert ('try again' + the ($row5['author']);
}
});
});
</script>
php jquery html mysqli
php jquery html mysqli
edited Nov 16 '18 at 23:07
Elvis Cruz
asked Nov 16 '18 at 17:39
Elvis CruzElvis Cruz
186
186
closed as too broad by John Dvorak, eyllanesc, Machavity, Makyen, Hovercraft Full Of Eels Nov 16 '18 at 23:41
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by John Dvorak, eyllanesc, Machavity, Makyen, Hovercraft Full Of Eels Nov 16 '18 at 23:41
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Please include a sample of what you've tried so far. Thanks.
– Nosajimiki
Nov 16 '18 at 17:43
I did, thanks..
– Elvis Cruz
Nov 16 '18 at 17:58
add a comment |
Please include a sample of what you've tried so far. Thanks.
– Nosajimiki
Nov 16 '18 at 17:43
I did, thanks..
– Elvis Cruz
Nov 16 '18 at 17:58
Please include a sample of what you've tried so far. Thanks.
– Nosajimiki
Nov 16 '18 at 17:43
Please include a sample of what you've tried so far. Thanks.
– Nosajimiki
Nov 16 '18 at 17:43
I did, thanks..
– Elvis Cruz
Nov 16 '18 at 17:58
I did, thanks..
– Elvis Cruz
Nov 16 '18 at 17:58
add a comment |
2 Answers
2
active
oldest
votes
Most obvious thing I see is that you are mixing your JS and PHP; so, lines like: var correct = $(opt).text().match (.$row['authors_id']); don't work because $row['authors_id'] is a PHP variable. Try using your PHP to write a JS array, then you can test against that.
Looks something like this
<script>
<?php
$myArrayString = "";
if($rowCount > 0){
while($row = $query->fetch_assoc()){
$myArrayString .= '"'.$row['authors'].'", '; // loops array values into a comma separated string.
}
rtrim($myArrayString, ', '); // removes trailing comma
echo ('var myArray = ['.$myArrayString.'];'); // echos a JavaScript array containing the data.
} else {
echo ('var myArray = ;');
}
?>
</script>
do you know where can I fine information to do what you said "try using your PHP to write a JS array, then you can test against that."
– Elvis Cruz
Nov 16 '18 at 23:08
add a comment |
Just print the PHP variable out into the JS like:
some JS / jQuery <?php echo $somePhpVariable;?> more JS code
This is normally the right idea in most cases, but in his example, there are multiple values to check against and one line of JS; so, it's not quite that simple. Also, if you want to just echo a single variable, PHP has a short-hand: <?=$somePhpVariable?>
– Nosajimiki
Nov 16 '18 at 18:43
I still don't quite understand what to do to make it work, but thanks guys!
– Elvis Cruz
Nov 16 '18 at 19:20
nope is not working
– Elvis Cruz
Nov 16 '18 at 23:03
@ElvisCruz the JS code does not know what$row5is, because it is a PHP variable. To pass in dynamic data from the server to the client (where the js is executed), you need to give the values along with the response, like Nosajimiki's answer.
– Hisham
Nov 17 '18 at 8:31
@Hisham, I am new at this, I don’t know ho to structure or where to put it. I have been looking and trying a lot of things but nothing seems to work. I understand what you’re saying but I don’t know how to take it out PHP to be able to call it in the function.
– Elvis Cruz
Nov 17 '18 at 15:05
|
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Most obvious thing I see is that you are mixing your JS and PHP; so, lines like: var correct = $(opt).text().match (.$row['authors_id']); don't work because $row['authors_id'] is a PHP variable. Try using your PHP to write a JS array, then you can test against that.
Looks something like this
<script>
<?php
$myArrayString = "";
if($rowCount > 0){
while($row = $query->fetch_assoc()){
$myArrayString .= '"'.$row['authors'].'", '; // loops array values into a comma separated string.
}
rtrim($myArrayString, ', '); // removes trailing comma
echo ('var myArray = ['.$myArrayString.'];'); // echos a JavaScript array containing the data.
} else {
echo ('var myArray = ;');
}
?>
</script>
do you know where can I fine information to do what you said "try using your PHP to write a JS array, then you can test against that."
– Elvis Cruz
Nov 16 '18 at 23:08
add a comment |
Most obvious thing I see is that you are mixing your JS and PHP; so, lines like: var correct = $(opt).text().match (.$row['authors_id']); don't work because $row['authors_id'] is a PHP variable. Try using your PHP to write a JS array, then you can test against that.
Looks something like this
<script>
<?php
$myArrayString = "";
if($rowCount > 0){
while($row = $query->fetch_assoc()){
$myArrayString .= '"'.$row['authors'].'", '; // loops array values into a comma separated string.
}
rtrim($myArrayString, ', '); // removes trailing comma
echo ('var myArray = ['.$myArrayString.'];'); // echos a JavaScript array containing the data.
} else {
echo ('var myArray = ;');
}
?>
</script>
do you know where can I fine information to do what you said "try using your PHP to write a JS array, then you can test against that."
– Elvis Cruz
Nov 16 '18 at 23:08
add a comment |
Most obvious thing I see is that you are mixing your JS and PHP; so, lines like: var correct = $(opt).text().match (.$row['authors_id']); don't work because $row['authors_id'] is a PHP variable. Try using your PHP to write a JS array, then you can test against that.
Looks something like this
<script>
<?php
$myArrayString = "";
if($rowCount > 0){
while($row = $query->fetch_assoc()){
$myArrayString .= '"'.$row['authors'].'", '; // loops array values into a comma separated string.
}
rtrim($myArrayString, ', '); // removes trailing comma
echo ('var myArray = ['.$myArrayString.'];'); // echos a JavaScript array containing the data.
} else {
echo ('var myArray = ;');
}
?>
</script>
Most obvious thing I see is that you are mixing your JS and PHP; so, lines like: var correct = $(opt).text().match (.$row['authors_id']); don't work because $row['authors_id'] is a PHP variable. Try using your PHP to write a JS array, then you can test against that.
Looks something like this
<script>
<?php
$myArrayString = "";
if($rowCount > 0){
while($row = $query->fetch_assoc()){
$myArrayString .= '"'.$row['authors'].'", '; // loops array values into a comma separated string.
}
rtrim($myArrayString, ', '); // removes trailing comma
echo ('var myArray = ['.$myArrayString.'];'); // echos a JavaScript array containing the data.
} else {
echo ('var myArray = ;');
}
?>
</script>
edited Nov 17 '18 at 7:44
answered Nov 16 '18 at 18:13
NosajimikiNosajimiki
7721613
7721613
do you know where can I fine information to do what you said "try using your PHP to write a JS array, then you can test against that."
– Elvis Cruz
Nov 16 '18 at 23:08
add a comment |
do you know where can I fine information to do what you said "try using your PHP to write a JS array, then you can test against that."
– Elvis Cruz
Nov 16 '18 at 23:08
do you know where can I fine information to do what you said "try using your PHP to write a JS array, then you can test against that."
– Elvis Cruz
Nov 16 '18 at 23:08
do you know where can I fine information to do what you said "try using your PHP to write a JS array, then you can test against that."
– Elvis Cruz
Nov 16 '18 at 23:08
add a comment |
Just print the PHP variable out into the JS like:
some JS / jQuery <?php echo $somePhpVariable;?> more JS code
This is normally the right idea in most cases, but in his example, there are multiple values to check against and one line of JS; so, it's not quite that simple. Also, if you want to just echo a single variable, PHP has a short-hand: <?=$somePhpVariable?>
– Nosajimiki
Nov 16 '18 at 18:43
I still don't quite understand what to do to make it work, but thanks guys!
– Elvis Cruz
Nov 16 '18 at 19:20
nope is not working
– Elvis Cruz
Nov 16 '18 at 23:03
@ElvisCruz the JS code does not know what$row5is, because it is a PHP variable. To pass in dynamic data from the server to the client (where the js is executed), you need to give the values along with the response, like Nosajimiki's answer.
– Hisham
Nov 17 '18 at 8:31
@Hisham, I am new at this, I don’t know ho to structure or where to put it. I have been looking and trying a lot of things but nothing seems to work. I understand what you’re saying but I don’t know how to take it out PHP to be able to call it in the function.
– Elvis Cruz
Nov 17 '18 at 15:05
|
show 1 more comment
Just print the PHP variable out into the JS like:
some JS / jQuery <?php echo $somePhpVariable;?> more JS code
This is normally the right idea in most cases, but in his example, there are multiple values to check against and one line of JS; so, it's not quite that simple. Also, if you want to just echo a single variable, PHP has a short-hand: <?=$somePhpVariable?>
– Nosajimiki
Nov 16 '18 at 18:43
I still don't quite understand what to do to make it work, but thanks guys!
– Elvis Cruz
Nov 16 '18 at 19:20
nope is not working
– Elvis Cruz
Nov 16 '18 at 23:03
@ElvisCruz the JS code does not know what$row5is, because it is a PHP variable. To pass in dynamic data from the server to the client (where the js is executed), you need to give the values along with the response, like Nosajimiki's answer.
– Hisham
Nov 17 '18 at 8:31
@Hisham, I am new at this, I don’t know ho to structure or where to put it. I have been looking and trying a lot of things but nothing seems to work. I understand what you’re saying but I don’t know how to take it out PHP to be able to call it in the function.
– Elvis Cruz
Nov 17 '18 at 15:05
|
show 1 more comment
Just print the PHP variable out into the JS like:
some JS / jQuery <?php echo $somePhpVariable;?> more JS code
Just print the PHP variable out into the JS like:
some JS / jQuery <?php echo $somePhpVariable;?> more JS code
answered Nov 16 '18 at 18:35
HishamHisham
194
194
This is normally the right idea in most cases, but in his example, there are multiple values to check against and one line of JS; so, it's not quite that simple. Also, if you want to just echo a single variable, PHP has a short-hand: <?=$somePhpVariable?>
– Nosajimiki
Nov 16 '18 at 18:43
I still don't quite understand what to do to make it work, but thanks guys!
– Elvis Cruz
Nov 16 '18 at 19:20
nope is not working
– Elvis Cruz
Nov 16 '18 at 23:03
@ElvisCruz the JS code does not know what$row5is, because it is a PHP variable. To pass in dynamic data from the server to the client (where the js is executed), you need to give the values along with the response, like Nosajimiki's answer.
– Hisham
Nov 17 '18 at 8:31
@Hisham, I am new at this, I don’t know ho to structure or where to put it. I have been looking and trying a lot of things but nothing seems to work. I understand what you’re saying but I don’t know how to take it out PHP to be able to call it in the function.
– Elvis Cruz
Nov 17 '18 at 15:05
|
show 1 more comment
This is normally the right idea in most cases, but in his example, there are multiple values to check against and one line of JS; so, it's not quite that simple. Also, if you want to just echo a single variable, PHP has a short-hand: <?=$somePhpVariable?>
– Nosajimiki
Nov 16 '18 at 18:43
I still don't quite understand what to do to make it work, but thanks guys!
– Elvis Cruz
Nov 16 '18 at 19:20
nope is not working
– Elvis Cruz
Nov 16 '18 at 23:03
@ElvisCruz the JS code does not know what$row5is, because it is a PHP variable. To pass in dynamic data from the server to the client (where the js is executed), you need to give the values along with the response, like Nosajimiki's answer.
– Hisham
Nov 17 '18 at 8:31
@Hisham, I am new at this, I don’t know ho to structure or where to put it. I have been looking and trying a lot of things but nothing seems to work. I understand what you’re saying but I don’t know how to take it out PHP to be able to call it in the function.
– Elvis Cruz
Nov 17 '18 at 15:05
This is normally the right idea in most cases, but in his example, there are multiple values to check against and one line of JS; so, it's not quite that simple. Also, if you want to just echo a single variable, PHP has a short-hand: <?=$somePhpVariable?>
– Nosajimiki
Nov 16 '18 at 18:43
This is normally the right idea in most cases, but in his example, there are multiple values to check against and one line of JS; so, it's not quite that simple. Also, if you want to just echo a single variable, PHP has a short-hand: <?=$somePhpVariable?>
– Nosajimiki
Nov 16 '18 at 18:43
I still don't quite understand what to do to make it work, but thanks guys!
– Elvis Cruz
Nov 16 '18 at 19:20
I still don't quite understand what to do to make it work, but thanks guys!
– Elvis Cruz
Nov 16 '18 at 19:20
nope is not working
– Elvis Cruz
Nov 16 '18 at 23:03
nope is not working
– Elvis Cruz
Nov 16 '18 at 23:03
@ElvisCruz the JS code does not know what
$row5 is, because it is a PHP variable. To pass in dynamic data from the server to the client (where the js is executed), you need to give the values along with the response, like Nosajimiki's answer.– Hisham
Nov 17 '18 at 8:31
@ElvisCruz the JS code does not know what
$row5 is, because it is a PHP variable. To pass in dynamic data from the server to the client (where the js is executed), you need to give the values along with the response, like Nosajimiki's answer.– Hisham
Nov 17 '18 at 8:31
@Hisham, I am new at this, I don’t know ho to structure or where to put it. I have been looking and trying a lot of things but nothing seems to work. I understand what you’re saying but I don’t know how to take it out PHP to be able to call it in the function.
– Elvis Cruz
Nov 17 '18 at 15:05
@Hisham, I am new at this, I don’t know ho to structure or where to put it. I have been looking and trying a lot of things but nothing seems to work. I understand what you’re saying but I don’t know how to take it out PHP to be able to call it in the function.
– Elvis Cruz
Nov 17 '18 at 15:05
|
show 1 more comment
Please include a sample of what you've tried so far. Thanks.
– Nosajimiki
Nov 16 '18 at 17:43
I did, thanks..
– Elvis Cruz
Nov 16 '18 at 17:58