Radio button stays checked after reload using Javascript and PHP session - javascript

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;
?>

Related

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

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

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

Assigning a selected value from a dropdown list to a php variable onchange, without pressing submit button in a html form?

I have a html form and a dropdown list like this:
<form name="form1" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<div>
<span>Select Event</span>
<select id = "events" onchange="run()" class = "pass" style="width: 209px">
<?php while($row = oci_fetch_array($curs)):?>
<option value="<?php echo $row[0];?>"><?php echo $row[1];?></option>
<? endwhile; ?>
<input type="submit" name="action" value="New Event">
</select>
TextBox1<br>
<input type="text" id="srt" placeholder="get value on option select"><br>
</div>
</form>
I can successfully show the values that I take from the database table. And after that I want to use the selected value for another query which needs this selected value simultaneously without pressing submit button.And my run function is like:
<script>
function run() {
document.getElementById("srt").value = document.getElementById("events").value;
}
How can I assign this selected value to a php variable simultaneously?
`
It is not possible.
PHP script will never execute on onchange event.
Don't confuse it with JavaScript.
PHP scripts will only run on events like GET, POST, etc.

How can I reloaded URL link, that it stay the same after I click on submit button

I have simple form:
<div class="form-style-2">
<form action="" name="formular" id="formular" method="GET">
<label for="actual_position"><span>From: <span class="required"></span></span><input name="actual_position" type="text" maxlength="512" id="actual_position" class="searchField"
<?php if(!empty($actual_position)){ ?>
value="<?php echo $_GET['actual_position']?>"
<?php
}else {
?> value = ""; <?php
} ?>/></label>
<label for="final_position"><span>To: <span class="required"></span></span><input name="final_position" type="text" maxlength="512" id="final_position" class="searchField" <?php if(!empty($final_position)){ ?>
value="<?php echo $_GET['final_position']?>"
<?php
}else {
?> value = ""; <?php
} ?>/></label>
<input type="submit" value="Find path" />
And another multiselect in form who gets values form url link and compere with database and get som results. Here is a code:
<table width= "570px">
<tr><td width="200px" style="align:center"><b>Waypoints:</b> <br>
<tr><td width="370px"><select style="float:center; margin-left:5px" multiple id="waypoints">
if(!empty($urls)){
foreach($urls as $url){
if($result = $conn->query("SELECT * FROM $table where $ID = '$url' "));
$atraction = $result->fetch_array(); ?>
<option value="<?php echo $atraction['lat']. "," . $atraction['lon']; ?>"
> <?php echo "<b>".$atrction['City']. ", " . $atraction['Name'];?> </option>
<?php
}
}
?>
</select></td></tr>
<br>
</table>
</form>
And getting ID-s from url code:
if(!empty($_GET[$ID])){
$urls = $_GET[$ID];
foreach($urls as $url){
// echo $url;
}
}
... and after submit, it Post to URL some variables like this:
http://127.0.0.1/responsiveweb/travel.php?actual_position=Paris&final_position=Praha&ID[]=23&ID[]=15&ID[]=55
... very important for me are ID-s values ... but when I change for example actual position and then submit I lost my ID-s and I get something like this: http://127.0.0.1/responsiveweb/travel.php?actual_position=Berlin&final_position=Praha
Can you help me how to get after clicking on submit button full url link? Thanks
I had some trouble understanding your question OP, but I think I understood somehow what you ment, so I decided to try giving you a answer.
I have re-written your code, and tried to make somehow better code-structure. I have also used form method POST in my example, so you can see how you can change the get data on the redirection url.
See my code example here: http://pastebin.com/wQ7QCBmt
I also decided to use the form method POST instead of GET, so you can easily do back-end tasks, and extend your link if neccessary. You could also add more data to the link even when using GET. You could add an hidden input inside your form, example:
<input type="hidden" name="more_data" value="a_value" />

confusion passing variable to php through multiple scripts

I am having difficulty passing a variable from a first form to a second form. There are four scripts involved: debug.php, getVar.php, printme.php and scripta.php. Running debug.php and typing "blah" for a "password to continue", select scripta.php from the pulldown and hit "Submit", I expect to see $dbpass="blah" for all of the scripts. I see it for the first page, but after the second pages' "Submit" button is pressed, the value is forgotten once inside of "printme.php". I suspect this has to do with variable scope. Any help is appreciated.
debug.php:
<html>
<body>
<form name="gateway" action= "" method="POST">
<fieldset>
<label>password to continue:</label>
<input type="text" id="dbpass" name="dbpass">
<label>Select Script:</label>
<select name="scriptSelect" id="scriptSelect">
<option value="">Please make a selection</option>
<option value="scripta.php">scripta</option>
</select>
<input name="updateGateway" type="submit" value="Submit">
<input name="resetForm" id="resetForm" type="reset" value="Reset Form">
</fieldset>
</form>
</body>
</html>
<script type="text/javascript">
document.getElementById('scriptSelect').addEventListener('change', function(e){
var selected_value = e.target.value;
document.forms['gateway'].action = selected_value;
alert(selected_value);
});
</script>
scripta.php:
<html>
<body>
<?php require 'getVar.php'; ?>
<form name="secondform" action= "printme.php" method="POST">
<fieldset>
<label>Hit submit to continue:</label>
<input name="updateScripta" type="submit" value="Submit">
</fieldset>
</form>
</body>
</html>
getVar.php:
<?php
if (isset($_POST['dbpass'])) {
$dbpass = #$_POST["dbpass"];
}
echo "you entered $dbpass";
?>
printme.php:
<?php
echo "Inside of printme, you entered $dbpass";
?>
Thanks
In scripta.php, add the line: <input type="hidden" name="dbpass" value="<? echo $dbpass ?>" /> somewhere inside the form tag.
In printme.php, add this line at the top of the page <? $dbpass = $_POST['dbpass'] ?>
There may be other errors in the scripts you have provided. Check back once you have made the above changes.
Mate... that's not how it works... Each request is independent...
In order to pass a value from one script to the other you either use sessions ($_SESSION) or you need to repost the variable.
Also, I don't know what you're trying to accomplish but passing passwords around, in plain text...
Repost variables
In scripta.php add this
<input name="dbpass" value="<?php $dbpass; ?>" type="hidden"/>
to your form. This will send the value contained in $dbpass in a hidden value.
Then, in printme.php you need to retrieve the value so just require that getVar.php script
require 'getVar.php';
echo "Inside of printme, you entered $dbpass";
Using Sessions:
change your getVar.php
session_start();
if (isset($_POST['dbpass'])) {
$_SESSION['dbpass'] = $_POST["dbpass"]; // you don't need that #
} else {
$_SESSION['dbpass'] = null;
}
then in your subsequent scripts, everytime you want to access dbpass just use
session_start();
$_SESSION['dbpass']
example:
<?php
session_start();
$_SESSION['dbpass']
echo "Inside of printme, you entered $dbpass";

Categories

Resources