update data when click a button on each row - php

I'm a newbie programmer. I need to select then update data when click a button on each row. now I can select and show data but just first update button is working. I think cause of this issue is button id is same. I can't figure out how to create unique id. I try to change id="updatedb" to id="$i" and nothing working
my script is :
$(function(){
$('#updatedb').click(function(){
var a = $('#updatedb').attr('id');
$('#textstatus').val(a);
alert ($('#updatedb').attr('id'));
});
});
and php is :
$sql = $connectdb->query($checknode);
$i = 0;
echo "<table border=1>";
if (!$sql) {
echo $connectdb->error;
else {
while ($row=$sql->fetch_row()) {
$crow = "updatedb".$i;
//echo $crow."<br />";
echo "<form method='post' action=''>";
echo '<tr><td>'.$row[0].'
</td><td>'.$row[1].'</td><td>'.$row[2].'
</td><td>'.$row[3].'</td><td>'.$row[4].'
</td><td>'.$row[5].'</td><td>
<input type="button" id="updatedb" name="updatedb" value="update">
<input type="text" id="textstatus" name="textstatus"
value="" disabled="disabled"></td></tr>';
$i++;
}
}
echo "</form>";
echo "</table>";

You can use class rather then id for button to update see the code below
jquery
$(function(){
$('.updatedb').click(function(){
var a = $(this).attr('class');
$(this).parents('tr').find('.textstatus').val(a);
alert (a);
});
});
php
$sql = $connectdb->query($checknode);
$i = 0;
echo "<table border=1>";
if (!$sql) {
echo $connectdb->error;
}else
{
while ($row=$sql->fetch_row()) {
$crow = "updatedb".$i;
//echo $crow."<br />";
echo "<form method='post' action=''>";
echo '<tr><td>'.$row[0].'
</td><td>'.$row[1].'</td><td>'.$row[2].'
</td><td>'.$row[3].'</td><td>'.$row[4].'
</td><td>'.$row[5].'</td><td>
<input type="button" class="updatedb" name="updatedb" value="update">
<input type="text" class="textstatus" name="textstatus"
value="" disabled="disabled"></td></tr>';
$i++;
}
}
echo "</form>";
echo "</table>";

Related

basic multiple answers quiz system checkbox ambiguity

I am trying to build a basic quiz system and everything seems ok.
The following code shows how a users choose the correct answer and the getresult.php shows the result. In my database, there is a question, opt1, opt2, opt3 opt4 and answer column.
<form method="POST" action="getresult.php">
<label>Enter Your Name:</label><br>
<input type="text" name="name" required><br><br>
<?php
$db = new mysqli("localhost", "root", "","learndb");
$stmt=$db->prepare("SELECT * FROM quiz");
$stmt->execute();
$result=$stmt->get_result();
while($myrow = $result->fetch_assoc())
{
echo "<form method='POST' action='getresult.php'>";
echo $myrow['id'];
echo ".";
echo $myrow['question'];
echo "<br>";
echo "<input type='checkbox' name='mycheck[]' value=".$myrow['opt1'].">";
echo $myrow['opt1'];
echo "<br>";
echo "<input type='checkbox' name='mycheck[]' value=".$myrow['opt2'].">";
echo $myrow['opt2'];
echo "<br>";
echo "<input type='checkbox' name='mycheck[]' value=".$myrow['opt3'].">";
echo $myrow['opt3'];
echo "<br>";
echo "<input type='checkbox' name='mycheck[]' value=".$myrow['opt4'].">";
echo $myrow['opt4'];
echo "<br><br>";
}
?>
<input type="submit" name="submit" value="Get Results" class="btn btn-primary">
getresult.php
<?php
extract($_POST);
$db = new mysqli("localhost", "root", "","learndb");
$stmt=$db->prepare("SELECT * FROM quiz");
$stmt->execute();
$result=$stmt->get_result();
$myrow = $result->fetch_assoc();
$totalCheckboxChecked = sizeof($_POST['mycheck']);
$submit=isset($_POST['submit']);
$count=0;
if($submit)
{
for($i=0;$i<$totalCheckboxChecked;$i++)
{
if($mycheck[$i]==$myrow['answer'])
{
$count=$count+1;
}
}
echo "Hello ";
echo $_POST['name'];
echo "<br>";
echo "You scored ";
echo $count;
}
Now the problem is with the checkbox, I can check all the values from all the questions. And when I use radio button I can check only one value from all questions. How can I check only one value from one question.
I don't know why you're not using radio buttons if you plan to allow only one selection, but you can set a limit to have the same thing with checkboxes with some JavaScript (or jQuery).
Here is an example: see fiddle demo
var limit = 1;
$('input').on('change', function(event) { // this could've been a 'click' event too.
if($(this).siblings(':checked').length >= limit) {
this.checked = false;
/* OR you can do like:
if($("input[name='mycheck']:checked").length > limit) { //... }
*/
}
});
if the question contain only one answer better use radio buttons to select the option
or
else
use this jquery to select single value from checkbox
$('input[type="checkbox"]').on('change', function() {
$('input[type="checkbox"]').not(this).prop('checked', false);
});

How to get value of input field from a form in a script

I am trying to display a value from a form in a script using console.log() for troubleshooting purposes but it seems to not be working. Here is my script.
Is there something I am missing?
jQuery(document).ready(function() {
var user = jQuery('#id').val;
var file = jQuery('#custom-file-input').val;
console.log(file);
});
echo "<form method='post' enctype='multipart/form-data' id='test_ajax'>";
echo "<select name='id' id='form-option' class='test-only'>";
echo '<option selected="selected">' .'Choose a User'. '</option>';
foreach ($registeredUsers as $key => $value) {
$registered = JFactory::getUser($value);
echo '<option value="'.$registered->id.'">'.$registered->name.'</option>';a
}
echo "</select>";
echo "<input name='uploadedfile' type='file' id='custom-file-input' class='test-only' /> <br/>";
echo '<input type="submit" name="submit" value="Upload" id="custom-submit-input">';
echo "</form>";
Use val() to get the value of an element.
var user = jQuery('#id').val
Should be
var user = jQuery('#form-option').val();
Notice () of val
The id of element is form-option, id is the name of the dropdown.

Updating checkbox to the server and disable it

I have a question regarding my project.
I need to make a database update section which pulls down from the server and showing all the values then update it to the database.
My approach is, getting the value and then display it as a check box. When user select one of the checkboxes, it will change the character from N to Y and then disable it button so people cannot make further update. I can successfully implemented the pulldown and getting the info from the server. but i dont know how to implement the checkbox process, update it to the server and set disable on it.
Please help me
This is the JS section on update_fab.php
<script>
// ===================================== //
// ===================================== //
$(function(){
// SHOW RECORD
$('#show').click(function () {
$.post('data2.php', {
action: "show",
"hm": $('#headmark').val()
}, function(res) {
$('#result').html(res);
});
});
});
</script>
<?php
$result = oci_parse($conn, 'SELECT HEAD_MARK FROM FABRICATION');
oci_execute($result);
echo '<div class = "wrapper">';
echo '<div id = "contact_form">';
echo '<span>Head mark</span><label><SELECT name="headmark" id="headmark">'.'<br>';
echo '<OPTION VALUE=" ">'."".'</OPTION>';
while ($row = oci_fetch_array($result, OCI_ASSOC)) {
$HM = $row ['HEAD_MARK'];
echo "<OPTION VALUE='$HM'>$HM</OPTION>";
}
echo '</SELECT></label><br />';
?>
<input class ="showButton" type="button" value="show" name="SHOW RECORD" id="show"/>
and on the data2.php
// IF SHOW KEY HAS BEEN PRESSED
if ($_POST['action'] == 'show') {
$sql = "SELECT * FROM SUB_MASTER_DRAWING
WHERE SUB_MASTER_DRAWING.HEAD_MARK = '{$_POST["hm"]}'";
$query = oci_parse($conn, $sql);
$query_exec = oci_execute($query);
echo "<table border='1'>";
while ($row = oci_fetch_assoc($query)) {
echo '<div id="content">';
echo '<table cellspacing = "0"';
echo '<tr><th>Head Mark</th>
<th>Cutting</th>
<th>Assembly</th>
<th>Welding</th>
<th>Drilling</th>
<th>Finishing</th>
<th>Update</tr>';
echo "<tr><td><b>$row[HEAD_MARK]/$row[ID]</b></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='button' value='UPDATE' name='updatefab'/></td>
</tr>";
if (isset($_POST['updatefab'])) {
echo '<script type = "text/javascript">submit pressed !</script>';
}
echo '<table cellspacing = "0"';
echo '</div>';
}
echo "</table>";
}

Submit form of Iframe from the parent window

<iframe id="frame1" name="frame1" align="center" src="committee_assign1.php" height="400" width="700">
</iframe>
<center><input onClick="submitiframeform(); return false;" type="button" name="submit" value="Submit" />
<script type="text/javascript">
function submitiframeform(){
window.frames['frame1'].document.forms['fypassign'].submit();
}
</script>
The above is the main page name committee_assign.php ..
And below is the page where the iframe called committee_assign1.php.
<?php
include '../database.php';
include 'valid_login.php';
if(isset($_POST['submit'])) {
$continue = FALSE;
$i = 0;
while ($continue == FALSE) {
if (isset($_POST['id_'.$i])) {
$fypcomm = $_POST['fypcomm_'.$i];
$user = $_POST['id_'.$i];
$sql = mysql_query(" UPDATE Lecturer SET LectFypCommittee = '$fypcomm' WHERE LectID = '$user' ")
or die(mysql_error());
mysql_query($sql);
} else
{$continue = TRUE;}
$i++;
}
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='../committee/committee_assign1.php'
</SCRIPT>");
}
?>
<head>
</head>
<body>
<form id="fypassign" name="fypassign" method="post" action="" target="_self" onSubmit="">
<?php
$counter = 0;
echo "<table class ='box'>";
echo "<thead>";
echo "<tr>";
echo "<th align='left' valign='top'>"."Lecturer Name"."</th>";
echo "<th align='left' valign='top'>"."FYP Committee"."</th>";
echo "</tr>";
$sql = mysql_query(" SELECT * FROM Lecturer ORDER BY LectFypCommittee DESC, LectName ASC ") or die(mysql_error());
while($info = mysql_fetch_assoc($sql)) {
$idcount = "id_".$counter;
echo "<input type='hidden' name='$idcount' id='$idcount' value={$info['LectID']} />";
echo "<tr>";
echo "<td>";
echo $info['LectName'];
echo "</td>";
echo "<td>";
$formname = "fypcomm_".$counter;
echo "<select name='$formname'>";
//to convert the flag value to user understandable language
if ($info['LectFypCommittee'] == '0'){
$dbfyp = 'No';
}
else $dbfyp = 'Yes';
echo "<option selected='selected' value='{$info['LectFypCommittee']}'>".$dbfyp."</option>";
if ($info['LectFypCommittee'] == '0'){
echo "<option value='1'>".'Yes'."</option>";
}
else echo "<option value='0'>".'No'."</option>";
echo "</select>";
echo "</td>";
echo"</tr>";
$counter++;
}
echo "</table>";
?>
</form>
</body>
I clicked submit button at the parent page and the page refresh but the value is not update.
Can anyone here guide me on this please?
So sorry to post such long codes as I hope you guys could understand more what I am doing. TQ
You have no input in your committee_assign.php named submit:
if(isset($_POST['submit']))
You must check with something like this:
if(isset($_POST))
You are checking if submit has been posted but you dont have an input named submit. Add an input named submit with some value and check if that post submit exists. You can make it hidden if you dont want to see an extra input.
if($_POST['submit']) is checking if there is a value with key 'submit' in the post array. All the key and value in $_POST array is name and value of a form element respectively.

previously submitted form data disappears when new form is submitted

I have multiple forms on a single page and all gets redirected to same page when form is submitted but the previously submitted form values disappears when new form is submitted.
I tried using sessions didn't worked for me what else? please help
<script language="javascript">
function row(x,y){
//var len=document.forms[x].name.value;
if(x.value.length >=3)
{
//alert("Message form no> "+y+"will be submited");
document.forms[y].submit();
}
}
</script>
</head>
<body >
<center>
<h2>Database App</h2>
<table>
<tr>
<th><lable>Name :</label></th>
<th><label>E_Id :</label></th>
<th><label>Email :</label></th>
<th><label>Other Info :</label></th></tr>
<tr>
<?php
error_reporting(E_ALL ^ E_NOTICE);
// code check for name in database and if exists,displays in table row
//for($i=0;$i<150;$i++)
//{
//$E_id=array();
if($_POST){
$i = $_GET["uid"];
//echo "fhwefwej==".$i;
$x='name'.$i;
// echo 'dasvds'.$x;
if($_POST[$x])
{
$name = strtolower($_POST[$x]);
$E_id[$i] = "";
$Email[$i] = "";
$Otherinfo[$i] = "";
$con = mysql_connect('localhost','root','') or die("npt");
$db = mysql_select_db("trainee")or die("nptdff");
$query = "Select * from reguser where fname like '".$_POST[$x]."%'";
$result = mysql_query($query);
mysql_num_rows($result);
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$str=$row['fname'];
$initials = strtolower(substr($str,0,3));
if($name == $initials)
{
//echo "exist"."<br>";
$E_id[$i]= $row['fname'];
$Email[$i]=$row['lastname'];
$Otherinfo[$i]=$row['address'];
break;
}
}
}
else
{
$msg[$i] = "no user with these initials";
}
mysql_close($con);
}
}
for($i=0;$i<150;$i++)
{
//session_start();
//echo session_name($i)."<br>";
echo "<form name='form$i' action='new2.php?uid=$i' method='post'>";
echo "<td><input type='text' name='name$i' id='name$i' onkeyup='row(this,$i);' /><br />";
echo "<span id='availability_status' >";
if($_POST[$x]){echo $msg[$i];}
echo "</span> </td>";
echo "<td><input type='text' name='E_id' id='E_id' value='";
if(isset($_POST[$x])){ echo $E_id[$i];}
echo "' disabled='disabled' />";
echo "</td>";
echo "<td><input type='text' name='email' id='email' value='$Email[$i]' disabled='disabled' />";
echo "</td>";
echo "<td><input type='text' name='otherinfo' id='otherinfo' value='$Otherinfo[$i]' disabled='disabled' />";
echo "</td></tr>";
echo " </form>";
}
//echo '<script language="javascript">document.getElementById(\'name0\').focus();</script>';
?>
</table>
</center>
</body>
</html>
Why don't you Use AJAX. It will help to keep you posed different form information in back-end
you can either store those information in database or in file.
It will be the best way.

Categories