How to get changing content from PHP in web and console? - javascript

When i creating a quiz, i found something interesting. When trying to change content with PHP using jQuery everything changing in web. row1 as row 1, row2 as row2; question display correctly, but when i would like to get value from question 2 i gat value from question 1. So question only load value, doesn't mean if i'm on question 1,2,3,4,5,6 or 10. I get value from 1st displayed question.
jQuery:
$(document).ready(function(){
var quizCount = 1;
$("#next").click(function(){
quizCount = quizCount+1;
$("#quiz").load("load-quiz.php",{
quizNewCount : quizCount
});
});
$('.service-container').each(function(){
$("input[name=quizid"+quizCount+"]:radio").change(function () {
return ans = $("input[name=quizid"+quizCount+"]:checked").val();
});
});
});
That is a part of my code, but most interesting, as you see i using increasing value for "quizId". Is same file have div with id quiz.
PHP inside quiz:
<h3><?=$row['topic'];?></h3>
<h4><?=$row['question'];?></h4>
<div>
<input type="radio" name="<?= $choice ?>" value="<?= $ans_array[0]?>">
<label id="<?= $ans_array[0];?>">A) <?= $ans_array[0];?>
</label></br>
</div>
<div>
<input type="radio" name="<?= $choice ?>" value="<?=$ans_array[1]?>">
<label id="<?= $ans_array[1];?>">B) <?= $ans_array[1];?>
</label></br>
</div>
<div>
<input type="radio" name="<?= $choice ?>" value="<?=$ans_array[2]?>">
<label id="<?= $ans_array[2];?>">C) <?= $ans_array[2];?>
</label></br>
</div>
<div>
<input type="radio" name="<?= $choice ?>" value="<?=$ans_array[3]?>">
<label id="<?= $ans_array[3];?>">D) <?= $ans_array[3];?>
</label></br>
</div>
<div>
<input type="radio" name="<?= $choice ?>" value="<?=$ans_array[4]?>">
<label id="<?= $ans_array[4];?>">E) <?= $ans_array[4];?>
</label></br>
</div>
same code is a part of my grade.php file.
Resuming:
server side $choice = client side ans;
every time value is equal. but i get result only for 1st displayed question , when i load my grade.php i don't have a output because, i getting value only from 1st question.
Can somebody give me some clue?!
Update:
That php display title from database
<h3><?=$row['topic'];?></h3>
and i get like:
"Javascript "
when i use ajax call to load next title from database:
$(document).ready(function(){
var quizCount = 1;
$("#next").click(function(){
quizCount = quizCount+1;
$("#quiz").load("load-quiz.php",{
quizNewCount : quizCount
});
});
I get "CSS", when click again i get "HTML", click again "jQuery" etc... everything display correct, but not for value, every time i see "Javascript" value.
Question updated

Related

Radio button stays checked after reload using Javascript and PHP session

Since I already know how to use the $_GET to store value from a radio button, this time I wanted it to be stored in a PHP session. The page reloads after clicking a button. I used javascript and PHP to do this. What I intended to do was the value from the radio button when clicked goes to the post() function. The post() sends the value to the Test.php file through $.post. The test.php file sends back the value to be stored in the variable a which will later output in the PHP session variable. The $state will be used to make the radio buttons stay checked after reload. However, the code has an error as it does not let me click any of the radio buttons and after clicking one, the whole page stops working and other buttons cannot be clicked anymore.
<script>
function post(){
var eqstate = $('#state').val();
$.post('test.php', {statevalue:state},
function(data){
var a = data.value;
});
}
</script>
<?php
$_SESSION['state']="<script> document.write(a)</script>";
$state=$_SESSION['state'];
?>
<span style="margin-left:35px;">STATE</span><br><br>
<form>
<input type="radio" name="state" id="state" value="allstate" onclick="post();" <?php echo
$state==='allstate' ? 'checked' : '' ?>> All State <br>
<input type="radio" name="state" id="state" value="new" onclick="post();" <?php echo $state==='new' ? 'checked' : '' ?>> New EQ: <br>
<input type="radio" name="state" id="state" value="old" onclick="post();" <?php echo $state==='old' ? 'checked' : '' ?>> Old EQ: <br>
<input type="radio" name="state" id="state" value="unknown" onclick="post();" <?php echo $state==='unknown' ? 'checked' : '' ?>> Unknown State: <br>
</form>
Test.php code:
<?php
$a=$_POST['statevalue'];
echo $a;
?>

prevent result, show through AJAX, after reload browser

I have a insert query through ajax. It is working correctly. But when I reload browser then result disappears from div section and if I insert form through ajax again then result is showing.
I have a file first.php (in which, form is present), a AJAX code and a firstcall.php where query will be execute.
My first.php (html form) is:
<form class="reservation-form mb-0" action="" method="post" autocomplete="off">
<input name="name1" id="name1" class="form-control" type="text" placeholder="Enter Name" required aria-required="true">
<input name="age" id="age" class="form-control" required type="number" placeholder="Enter Age" aria-required="true">
<input type="checkbox" id="checkbox" class="checkbox1" name="namec[]" value="<?php echo $value['id']; ?>" >
<input type="button" class="pull-right btn btn-warning" value="Submit" id="submit">
</form>
Here data should be display:
<div class="col-md-5">
<div class="panel panel-primary" id="showdata">
<!-- Here is the results, but when reload browser then result disapper-->
</div>
</div>
AJAX is:
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
var name1 = $("#name1").val();
var age = $("#age").val();
var chkArray=[];
$('.checkbox1:checked').each( function() {
chkArray.push($(this).val());
} );
var selected;
selected = chkArray.join(',') ;
if(selected.length > 1){
$.ajax( {
url:'firstcall.php',
type:'POST',
data:{name1: name1,age: age,namec: chkArray},
}).done(function(data){
$("#showdata").html(data);
});
}
else{
alert("Please at least one of the checkbox");
}
});
});
</script>
firstcall.php is:
<div class="panel panel-primary" id="showdata">
<?php
foreach($_POST['namec'] as $selected){
echo $selected;
$_SESSION['name1']=$_POST["name1"];
$_SESSION['age']=$_POST["age"];
echo $name1=$_SESSION['name1'];
echo $age=$_SESSION['age'];
$query=mysql_query("insert into patient_details (p_name,p_age,g_number) values ('$name1','$age','$selected')") or die(mysql_error());
}
?>
First of all fix your query to use MySQLi, instead of MySQL, check this or the PHP manual
Also don't ever add direct $_POST or $_GET variables into your mysql query, filter them first using mysqli_real_escape.
$name1 = mysqli_real_escape($link, $_POST["name1"]);
$age = mysqli_real_escape($link, $_POST["age"]);
After that, to show the data when the page reloads, you need to load the data with the page, easiest way to do that is just add in your HTML PHP tags with an echo command inside, adding your variables.
If I understand your question correctly, you want the Ajax result to also show on page load?
Right now you only execute the JS after a click (so on page load/relaod you will just see the html), you might want to execute it after page load aswell (so execute the script without the .click)
You could create a function once, and call it when the page is ready and on click.

Javascript button click change / create PHP variable on same page

I have searched stackoverflow for the past hour and have found similar questions but attempting the solution has given me only errors - it may be as I am trying to load it on the same page.
I have tried saving the value to a cookie in JS then loading it in php, also attempted through ajax, etc.
From what I've read it can't just be done within as PHP is server side and Javascript is browser side which I get.
I currently have a .html page with a form that sends values through POST:
form.html
<form id="calc" form name="calculator" method="post" action="/calculate.php">
The solution is not with the form, I can't have hidden fields as I need to user input. I could have a radio box in the form yes and then get the value that way, but that's not my question. My question is how I can get the user's input and turn it into php on the page the form redirects to calculate.php:
calculate.php
<?php
if(isset($_POST['submit'])){
$x= $_POST['value'];
echo "your something:";
echo $x;
?>
Click button below for option one to do this, or click two for that, or three for something else...
<button id="one"> one </button>
<script>
$(document).ready(
function() {
$("#one").click(function() {
make php value 1
});
</script>
use php value from click
So the user will already have clicked submit. Then be redirected onto the .php page with their inputs, they can then choose button 1, 2 or 3 for example through javascript on calculate.php NOT the form.
My problem is I need to know on the same page - calculate.php, which button has been clicked in php. So that I can use their input and do different things with it depending on which button.
Is it possible to get a value to be set or create one from a button click on calculate.php itself with the javascript button producing a php value on the same page? Does it need to be loaded from an external.php page through ajax? Is there any other way?
PHP Option:
<?php
if(isset($_POST['submit'])){
$x= (isset($_POST['one'])) ? $_POST['one'] : '';
$y = (isset($_POST['two'])) ? $_POST['two'] : '';
$z = (isset($_POST['three'])) ? $_POST['three'] : '';
echo "your something:";
echo $x;
echo $y;
echo $z;
?>
<form name="calculator" method="post">
<INPUT TYPE="RADIO" NAME="one" VALUE="1"> ONE
<INPUT TYPE="RADIO" NAME="two" VALUE="2"> TWO
<INPUT TYPE="RADIO" NAME="three" VALUE="3"> THREE
<input type="submit" value="Submit">
</form>
JS mode:
if(isset($_POST)){
$value= (isset($_POST['value'])) ? $_POST['value'] : '';
$previous = (isset($_POST['previous'])) ? $_POST['previous'] : '';
//play with value and previous one
$x = //your algorithm;
echo "your something:";
echo $x;
?>
<form name="calculator" method="post">
<button type="button" value='1'>ONE</button>
<button type="button" value='2'>TWO</button>
<button type="button" value='3'>THREE</button>
<input name='value' type='hidden' value=''>
<input name='previous' type='hidden' value='<?= (isset($x)) ? $x : ''?>'>
</form>
<script>
$(document).ready(
function() {
$("button").click(function() {
$('input[name=value]').val($(this).val());
$('form').submit();
});
</script>
use php value from click

How would I show this checkbox on realtime?

I'm currently studying html and php and I made this little project about a "notification system". I guess I first have to explain this before I go in detail on my real problem.
My table looks like this:
My first php command would get the values of each feature column and store them into $a, $b, $c, $d and $e.
My second php command would echo 5 textboxes and mark them as checked based on the value of $a,$b,$c,$d and $e so the output based on my table would show five checkboxes with the 1st, 2nd and 4th checkbox with a check.
My third php command would gather the values from the checkboxes because the user might want to change his preferred notifications. After clicking submit the code would update the new values of the textboxes.
Below is the code on my current progress:
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('main_database') or die(mysql_error());
$fetchchoice=mysql_query("SELECT * FROM notification_table WHERE username='carl'");
while($getchoice=mysql_fetch_assoc($fetchchoice)){
$a=$getchoice['feature1'];;
$b=$getchoice['feature2'];
$c=$getchoice['feature3'];
$d=$getchoice['feature4'];
$e=$getchoice['feature5'];
}
?>
<html>
<head>
</head>
<body>
<form method="POST">
<?php
echo
'
<input type="hidden" name="choice1" value="">
<input type="checkbox" name="choice1" value="checked" '.$a.' > Feature 1 <br>
<input type="hidden" name="choice2" value="">
<input type="checkbox" name="choice2" value="checked" '.$b.' > Feature 2 <br>
<input type="hidden" name="choice3" value="">
<input type="checkbox" name="choice3" value="checked" '.$c.' > Feature 3 <br>
<input type="hidden" name="choice4" value="">
<input type="checkbox" name="choice4" value="checked" '.$d.' > Feature 4 <br>
<input type="hidden" name="choice5" value="">
<input type="checkbox" name="choice5" value="checked" '.$e.' > Feature 5 <br>
';
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
$f1choice = $_POST['choice1'];
$f2choice = $_POST['choice2'];
$f3choice = $_POST['choice3'];
$f4choice = $_POST['choice4'];
$f5choice = $_POST['choice5'];
echo "1.";
echo $f1choice;
echo "<br />";
echo "2.";
echo $f2choice;
echo "<br />";
echo "3.";
echo $f3choice;
echo "<br />";
echo "4.";
echo $f4choice;
echo "<br />";
echo "5.";
echo $f5choice;
$kweri = "UPDATE `main_database`.`notification_table` SET `feature1` = '".$f1choice."', `feature2` = '".$f2choice."', `feature3` = '".$f3choice."', `feature4` = '".$f4choice."', `feature5` = '".$f5choice."' WHERE `notification_table`.`username` = 'carl'";
mysql_query($kweri);
}
?>
</body>
</html>
PROBLEM
My problem now is that I have to refresh the page to have the updated checkbox to appear since this was only done on php, I have no experience in javascript at all but I think it's the best way to show my current textbox on realtime, however, I have no idea where to start or if there is another way I could go about showing my checkbox on realtime.
EXTRA PROBLEM (This has something to do with my system but not regarding this code. If you guys could take a look at this problem it would mean a lot.)
As you guys have seen, the table above was just to keep track of what a user's preference regarding notifications would be. But the system doesn't stop there, I need to figure out how to pass notifications after a feature has been used. What my idea was is that I should add an extra sql command after every feature to put what has happened on a different table. Example would be registration, after the insert command for the registration details to be saved, I would also insert that, user "example" has "registered" on "date" in a separate table, namely "notif_table". My columns would be, username, action and date so my notifications page can show that "User" has "Registered" on "January 18, 2015".
But then again, that was just my theory on notifications and I'm very open to suggestions and corrections.
Just put the part with if(isset($_POST['submit'])) prior to your select statement.
EXTRA PROBLEM:
Just add a column to your existing table (e.g. 'created') and put now() into it when inserting the row.

Return checked option from array

I am using radio boxes to save values into an array, the issue I am having is trying to automatically check the checkbox for the corresponding value after the page is refresh or whatever
I have tried the following;
<h3 style="margin-bottom: 0px;">Floating</h3></br>
<input type="radio" name="lu_ban_data[noticeType]" value="multi"<?php echo ('multi' == get_option( 'noticeType' ))? 'checked="checked"':''; ?> /></input>
<h3 style="margin-bottom: 0px;">Floating</h3></br>
<input type="radio" name="lu_ban_data[noticeType]" value="floating"<?php echo ('floating' == get_option( 'noticeType' ))? 'checked="checked"':''; ?> /></input>
The value is being saved when I click either one array (size=6)
'noticeType' => string 'multi' (length=5) but the corresponding checkbox isnt being checked.
Anyone help?
output markup
<div style="margin: 10px;">
<h3 style="margin-bottom: 0px;">Multiple</h3></br>
<input type="radio" name="lu_ban_data[noticeType]" value="multi" /></input>
<h3 style="margin-bottom: 0px;">Floating</h3></br>
<input type="radio" name="lu_ban_data[noticeType]" value="floating" /></input>
</div>
the "checked" is not being printed
You say its in an array? how are you getting that array?
To use get_option() at some point you have to use update_option()
Try echo get_option( 'noticeType' ) and see what is stored in the wordpress option.
I made it work like so;
value="multi" <?php $value = get_option('lu_ban_data'); checked( $value['noticeType'], 'multi' ); ?>

Categories

Resources