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>";
}
Related
I try to figure out how to handle two nested forms. The first one sends data from select-option dropdown to another_file.php. The nested one should send data from check boxes to current file and should be handled by isset($_POST['...']). Here is my simplified code:
if (isset($_POST['a']))
{
// do something
}
if (isset($_POST['b']))
{
// do something else
}
echo "<form action='another_file.php' method='post'>"; // begin of first form
echo "<table class ='table table-hover table-condensed table-striped table-bordered'>";
echo "<thead>";
echo "<th>ID</th><th>Jídlo</th><th>Množství</th><th>por_cislo</th><th>Odebrat</th>";
echo "</thead>";
while($rows = $stmt->fetch()){
echo "<tr><td>" . $rows['id'] . "</td><td>".$rows['jidlo'];
echo "<select name =".$rows['id']."_".$rows['por_cislo']."> ";
$stmt2 = $db->query($q2);
echo "<option value ='nic'> (vyberte potravinu) </option>";
$max_por_cislo = $rows['por_cislo'];
while($rows2 = $stmt2->fetch())
{
echo '<option value="'.$rows2['id'].'">'.$rows2['jidlo'].'</option>';
};
echo "</select>";
echo "</td> <td>" . $rows['mnozstvi'] . "g <input name = '".$rows['por_cislo']."' type='text' value = '-'></td><td>".$rows['por_cislo']."</td>";
echo "<td><form action ='this_file.php' method = 'post'>"; // begin of nested form
echo "<input type='checkbox' id='atur_peg' name='idecko[]' value=".$rows['id']."*".$rows['por_cislo']." /></td></tr> ";
};
echo "<input type='hidden' name='pc' value=".$plan_cislo.">";
echo "<tr><td colspan='2'><input name = 'go' type='submit' value='OK'/></td><td colspan='2'><input type = 'submit' name ='a' value='ADD'/></td>";
echo "<td><input type = 'submit' name ='b' value='DELETE checked'/></form></td></tr>"; // end of nested form
echo "</table>";
echo "</form>"; // end of first form
Is
there any way to do this correctly?
Instead of nested forms, use jquery to detect checkbox being checked and then change the form action url. Something on this line.. You will have to modify the same to suit your needs.
$(document).ready(function(){
$("#formname").on("change", "input:checkbox", function(){
if( $(this).is(":checked") ) {
$('#formName').attr('action', 'this_file.php');
$("#formname").submit();
}
});
});
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>";
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);
});
I'm beginner at PHP and I'm using a tutorial I found on the web to work a new piece of a new CMS I'm building. The problem is that the tutorial works just fine but when I start to alter it to my use it cannot connect to the database to update. I receive my error "Database Error: Unable to update record." I can add and delete but not update.
Index.php
<html>
<head>
<title>MySQLi Tutorial</title>
</head>
<body>
<?php
//include database connection
include 'db_connect.php';
$action = isset($_GET['action']) ? $_GET['action'] : "";
if($action=='delete'){ //if the user clicked ok, run our delete query
$query = "DELETE FROM family WHERE fid = ".$mysqli->real_escape_string($_GET['fid'])."";
if( $mysqli->query($query) ){
echo "User was deleted.";
}else{
echo "Database Error: Unable to delete record.";
}
}
$query = "SELECT * FROM family";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
echo "<div><a href='add.php'>Create New Record</a></div>";
if( $num_results ){
echo "<table border='1'>";//start table
//creating our table heading
echo "<tr>";
echo "<th>name</th>";
echo "<th>Action</th>";
echo "</tr>";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$name}</td>";
echo "<td>";
echo "<a href='edit.php?fid={$fid}'>Edit</a>";
echo " / ";
echo "<a href='#' onclick='delete_user( {$fid} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
//if table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
<script type='text/javascript'>
function delete_user( fid ){
//this script helps us to
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and fid to the record to be deleted
window.location = 'index.php?action=delete&fid=' + fid;
}
}
</script>
</body>
</html>
add.php
<html>
<head>
<title>MySQLi Create Record</title>
</head>
<body>
<!--we have our html form here where user information will be entered-->
<form action='#' method='post' border='0'>
<table>
<tr>
<td>Firstname</td>
<td><input type='text' name='name' /></td>
</tr>
<td></td>
<td>
<input type='hidden' name='action' value='create' />
<input type='submit' value='Save' />
<a href='index.php'>Back to index</a>
</td>
</tr>
</table>
</form>
<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
//include database connection
include 'db_connect.php';
//write query
$query = "insert into family
set
name = '" . $mysqli->real_escape_string($_POST['name']) ."'
";
if( $mysqli->query($query) ) {
echo "User was created.";
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}
?>
</body>
</html>
edit.php
<?php
//include database connection
include 'db_connect.php';
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){
//write query
$query = "update users
set
name = '".$mysqli->real_escape_string($_POST['name'])."',
where
fid='".$mysqli->real_escape_string($_REQUEST['fid'])."'";
if( $mysqli->query($query) ) {
echo "User was updated.";
}else{
echo "Database Error: Unable to update record.";
}
}
$query = "select fid, name
from family
where fid='".$mysqli->real_escape_string($_REQUEST['fid'])."'
limit 0,1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$fid = $row['fid'];
$name = $row['name'];
?>
<!--we have our html form here where new user information will be entered-->
<form action='#' method='post' border='0'>
<table>
<tr>
<td>Firstname</td>
<td><input type='text' name='name' value='<?php echo $name; ?>' /></td>
</tr>
<tr>
<td></td>
<td>
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='fid' value='<?php echo $fid ?>' />
<!-- we will set the action to edit -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Edit' />
<a href='index.php'>Back to index</a>
</td>
</tr>
</table>
</form>
>
I have been unable to find the proper language to fix this but I believe it's because I"m using some form of multiples but I am not familiar with the mysqli/php languages to fix it.
Thank you
You need to remove the comma at the end of:
name = '".$mysqli->real_escape_string($_POST['name'])."',
the query should be:
$query = "update users
set
name = '".$mysqli->real_escape_string($_POST['name'])."'
where
fid='".$mysqli->real_escape_string($_REQUEST['fid'])."'";
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.