Jquery Multiple Attribute Selector not working - php

I am trying to select for 2 attributes that are variable on a multiple choice quiz. I echoed the correct answer to each question in an input that doesn't display on the page. I think my line where I select for 2 different attributes is the problem.
JQUERY:
var zz = 1;
while (zz <= <?php echo $num_rows ?>){ //I'm 100% postive $num_rows returns a value of 3
var zstring = '#answer' + zz;
var theanswer = $(zstring).attr('value'); //should return "a" or "c" or whatever the answer is
if $("input[name=zz][value=theanswer]").is(':checked') { //this is the line that's not working
alert("the code worked");
}
zz++;
}
HTML echoed from PHP
echo "<input type='radio' name=" . $question_number ." value='a'> A. " . $chA . "<br><br>";
echo "<input type='radio' name=" . $question_number ." value='b'> B. " . $chB . "<br><br>";
echo "<input type='radio' name=" . $question_number ." value='c'> C. " . $chC . "<br><br>";
echo "<input type='radio' name=" . $question_number ." value='d'> D. " . $chD . "<br><br>";
echo "<input type='radio' name=" . $question_number ." value='e'> E. " . $chE . "<br>";
echo "<input type='text' id='answer".$question_number."' style='display: none;' value='".$correct."' />";

I see that in the if statement you have this:
if $("input[name=zz][value=theanswer]").is(':checked') { //this is the line
it should be:
if ($('input[name="zz"][value="theanswer"]').is(':checked')) { //this is the line

Related

PHP Getting Id Of Submit Button in Next Page

if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<form name=frm action=edit.php method=POST";
echo "<tr>";
echo "<td>" . $row["Sl_no"]. "</td>";
echo "<td> " . $row["nam"]. "</td>";
echo "<td> " . $row["age"]. " </td>";
echo "<td> " . $row["dob"]. "</td>";
echo "<td> " . $row["gender"]. " </td>";
echo "<td> " . $row["married"]. " </td>";
echo "<td> " . $row["temp_addr"]. " </td>";
echo "<td> " . $row["fix_addrs"]. " </td>";
echo "<td> " . $row["email"]. " </td>";
echo "<td> " . $row["phone"]. " </td>";
echo "<td> " . $row["mother_tounge"]. " </td>";
echo "<td> " . $row["nationality"]. " </td>";
echo "<td> " . $row["clg"]. " </td>";
echo "<td> " . $row["sem"]. " </td>";
echo "<td> " . $row["grad"]. " </td>";
echo "<td> " . $row["qual"]. " </td>";
echo "<td> " . $row["branch"]. " </td>";
echo "<td> <input type=submit name =" .$row["Sl_no"]. " value =Edit ><input type=button id=d".$row["Sl_no"]." value=Delete />";
echo "</tr>";
echo"</form>";
}
I am using this code
and in each field there will be a random named submit will generate.
I want to transfer the name of the button to next page or pass the value of first tag help me.
On the next page you can use $_POST['key']; or $_GET['key']. That will give you access to your value from the key.
You can not post submit button value by using name attribute. just add one hidden field with this form. like
echo "<td> <input type=hidden value=" .$row["Sl_no"]. " name=submit_btn_val >"
You need to pass the value from database to the submit button value, instead of name if you want to get the value on POST, because on post you're getting the name as key and value from input as values, like this:
<input type=submit name="name_here" value ="<?php echo $row['Sl_no']; ?>" >

when I am trying to get values from checkbox in PHP- i get "on" as an output - how is that?

when I am trying to get values from checkbox in PHP , I get
this as output, printing $_POST
Array ( [days] => on [submit] => save )
the view code
$days_numbers = explode(',',$user->work_days);
$week = array('Saturday','Sunday' ,'Monday','Tuesday' ,'Wendnesday' ,'Thursday' ,'Friday');
?>
<form method='post' action='' >
<?php
for($i=0 ; $i< count($week); $i++)
{
if(in_array($i,$days_numbers))
{ echo "<input type='checkbox' name='days' checked >" . $week[$i]. "<br/>";
}else
echo "<input type='checkbox' name='days' >" . $week[$i] . "<br/>";
}
?>
Your input element is missing with value attribute
replace
echo "<input type='checkbox' name='days' >" . $week[$i] . "<br/>";
with
echo "<input type='checkbox' name='days' value=".$week[$i]." >" . $week[$i] . "<br/>";
I found that i forget to get write html view like this:
echo "<input type='checkbox' name='days[]' value='$i' checked >" . $week[$i] . "<br/>";
I forget to give value of input so the output was on and also get the value as an array by adding name name="days[] .

how to store answers from an online examination system to database

I am creating an online examination system where am fetching questions and answers from database and displays options in radio button. But, the problem is if i click save it just saves one answer not answer from all questions
php codes for fetching questions
$quer=" SELECT * FROM questionset ORDER BY RAND() ";
$query=mysqli_query($sql,$quer);
while($row = mysqli_fetch_array($query))
{
$na= $row['qus'];
$opa= $row['opa'];
$opb= $row['opb'];
$opc= $row['opc'];
$opd= $row['opd'];
echo "<div class='pr3'>";
echo "<div id='question'>";
echo $na;
echo "</div>";
echo "<br/>";
echo "<form name='cart' method='post'>";
echo "<input type='radio' name='ans' value='" . $opa. "' onclick='answ(this.value)' >";
echo $opa;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opb. "' onclick='answ(this.value)'>";
echo $opb;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opc. "' onclick='answ(this.value)'>";
echo $opc;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opd. "' onclick='answ(this.value)'>";
echo $opd;
echo "<br/>";
echo "<input type='text' id='ansval' name='ansval' >";
echo "</form>";
echo " </div>";
}
Javascript that updates just one text box
<script>
function answ(answer) {
document.getElementById("ansval").value = answer;
}
</script>
The radio buttons name are all same for all questions. You have to make the radio buttons names dynamically for each answer set. You can use the following code :
$quer=" SELECT * FROM questionset ORDER BY RAND() ";
$query=mysqli_query($sql,$quer);
while($row = mysqli_fetch_array($query))
{
$questionId = $row['questionId']; //primary key of question table
$na= $row['qus'];
$opa= $row['opa'];
$opb= $row['opb'];
$opc= $row['opc'];
$opd= $row['opd'];
echo "<div class='pr3'>";
echo "<div id='question'>";
echo $na;
echo "</div>";
echo "<br/>";
echo "<form name='cart' method='post'>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opa. "' onclick='answ(this.value,'<?php echo $questionId; ?>')' >";
echo $opa;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opb. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opb;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opc. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opc;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opd. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opd;
echo "<br/>";
echo "<input type='text' id='ansval<?php echo $questionId; ?>' name='ansval<?php echo $questionId; ?>' >";
echo "</form>";
echo " </div>";
}
And You javascript function will be looks like :
<script>
function answ(answer,qId) {
document.getElementById("ansval"+qId).value = answer;
}
</script>
After the answer submit you can get the submitted values in same way. i.e $_POST['ansval'.$questionId]; Before get the value you have to create another loop to get all question Ids.
ID field must be unique
++$counter;
echo "";
$_POST['ansval1'],$_POST['ansval2'],$_POST['ansval3'].....

Undefined index PHP placing at host

I have to make a site for school. It need to be linked with a database. On localhost everything is ok and works, but when I tried to upload it on the host of the school, I get this error:
Notice: Undefined index: vragen in /mnt/studentenhomes/arnaud.gandibleux/public_html/datamanagement/index.php on line 44
I can't find the solution
index.php
<div id="tekst">
<table align='center'>
<?php
//if (isset($_GET['vragen'])){
if ($_GET['vragen'] === 'Alleclubs') {
getclubs();
} elseif ($_GET['vragen'] === 'ledenvjf') {
getVJFleden();
echo "test";
} elseif ($_GET['vragen'] === 'ledenffbj') {
getFFBJleden();
}
elseif (isset($_GET['clubnr'])) {
getLedenPerClubEnID($_GET['clubnr']);
}
else{
getclubs();
}
// }
?>
</table>
Crud.php
function getVJFleden() {
global $mysqli;
$result = $mysqli->query("SELECT * FROM Leden l JOIN Clubs c ON l.clubnr = c.clubnr join Bonden b
ON b.ID_bond = c.ID_bond LEFT JOIN adressen a ON a.ID_adress = l.ID_adress WHERE b.naam_bond = 'VJF';");
if ($result) {
if ($result->num_rows > 0) {
echo"<caption>Alle leden VJF</caption>";
echo "<th>Voornaam</th><th>achternaam</th><th>leeftijd</th><th>Kye</th><th>adress</th>";
while ($leden = $result->fetch_object()) {
echo "<tr><td>$leden->lid_voornaam</td> ";
echo "<td>" . $leden->lid_achternaam . "</td> ";
echo "<td>" . $leden->lid_leeftijd . "</td> ";
echo "<td>" . $leden->kye . "</td> ";
echo "<td>" . $leden->straatnaam . " " . $leden->huisnummer . " " . $leden->postcode . " " . $leden->gemeente . "</td> ";
echo "<td><form id='update' action='update.php' method='POST'>
<input type='hidden' name='id' value='" . $leden->ID_lid . "'/>
<input type='hidden' name='clubnr' value='" . $_GET['clubnr'] . "'/>
<input type='image' src='update.png' alt='Update' width='22' height='22'>
</form>
<form id='delete' action='deleteLid.php' method='POST'>
<input type='hidden' name='id' value='" . $leden->ID_lid . "'/>
<input type='image' src='delete.png' alt='detele' width='22' height='22'>
</form></td> ";
}
}
}
$mysqli->close();
}
function getFFBJleden() {
global $mysqli;
#, Adressen a
#AND l.ID_adress = a.ID_adress
$result = $mysqli->query("SELECT * FROM Leden l JOIN Clubs c ON l.clubnr = c.clubnr join Bonden b ON b.ID_bond = c.ID_bond LEFT JOIN adressen a ON a.ID_adress = l.ID_adress WHERE b.naam_bond = 'FFBJ';");
if ($result) {
if ($result->num_rows > 0) {
echo"<caption>Alle leden VJF</caption>";
echo "<th>Voornaam</th><th>achternaam</th><th>leeftijd</th><th>Kye</th><th>adress</th>";
while ($leden = $result->fetch_object()) {
echo "<tr><td>$leden->lid_voornaam</td> ";
echo "<td>" . $leden->lid_achternaam . "</td> ";
echo "<td>" . $leden->lid_leeftijd . "</td> ";
echo "<td>" . $leden->kye . "</td> ";
echo "<td>" . $leden->straatnaam . " " . $leden->huisnummer . " " . $leden->postcode . " " . $leden->gemeente . "</td> ";
echo "<td><form id='update' action='update.php' method='POST'>
<input type='hidden' name='id' value='" . $leden->ID_lid . "'/>
<input type='hidden' name='clubnr' value='" . $_GET['clubnr'] . "'/>
<input type='image' src='update.png' alt='Update' width='22' height='22'>
</form>
<form id='delete' action='deleteLid.php' method='POST'>
<input type='hidden' name='id' value='" . $leden->ID_lid . "'/>
<input type='image' src='delete.png' alt='detele' width='22' height='22'>
</form></td> ";
}
}
}
$mysqli->close();
}
You need to ensure that the array index exists before you try to use it. Being that it is a $_GET variable, it may not have been passed as a URL parameter.
Uncomment
//if (isset($_GET['vragen'])){
To
if (isset($_GET['vragen'])){
Two things are happening:
you have Notice warnings on. See here on how to turn them off: (How do I turn off PHP Notices?)
It's probably one of the $_GET parameters that you didn't specify on the url. So when you load the page, you're accessing the $_GET superglobal, but there may be nothing in it, and you're trying to print out that value.
You can either turn off notices or use a function to get a values from $_GET, and in this function check that the value isset() first before you access it.
when you visit the site you do something like: mysite.com?vragen=ledenvjf
if you forget the part after the ? there is no $_GET['vragen'] and the notice is thrown
this line
//if (isset($_GET['vragen'])){
prevents the notice from being thrown you should uncomment it together with this line:
// }
It means that variable vragen is not set so your $_GET array doesn't have any element with index vragen.
Uncomment //if (isset($_GET['vragen'])){ and // } since this was checking if $_GET variable with name vragen was set. Example of when this check will pass index.php?vragen=ledenvjf

Dynamic naming of radio buttons

I have "n" number of rows being returned by a sql statement. Works great. For every row returned, I would like the user to pick an option (either option 'a' or option 'b')
As standard practice a while loop is used to iterate through each row, and I attempt to name my options by the row number. However, I run into problems when I attempt to get the value of the radio button the user has selected.
I can't seem to dynamically get the value of the radio button named 1 in the first loop (or any loop with it's appropriate name) of the where clause.
Code snippet here:
$counter = 1;
while($row = mysql_fetch_array($result))
{
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $counter . ">";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['d_id'] ."> ";
echo "<br>";
echo "<input type='hidden' name='choice' value=" . $_GET["m" . $counter] . " >";
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
$counter ++;
}
updatepics.php:
Game
<?php
echo $_POST['game'];
echo " " . $_POST['player'] . " picks: ";
echo " " . $_POST['choice'] ;
?>
output of updatepics.php...
Game 1 granny picks:
Notice 'choice' is null
According to the HTML4 specification, names attributes are defined as CDATA and CDATA is defined as follows:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Modify your code to have all name values start with at least one letter.
I bailed out from dynamically creating a radio button name. Now all radio buttons have the same name through each iteration of the loop.
It seems to work passing the correct value at the correct time even if you select all of the buttons are set before hitting the submit button for each iteration.
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='mw' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $row['f_id'] . ">";
echo "<input type='hidden' name='player' value='" . $user . "' >";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='mw' value= ". $row['d_id'] ."> ";
echo " " . $row['line'];
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
updatepics.php is just as simple, and it works:
$_POST['mw']

Categories