How to get the entries of field generated by a loop - php

Please I need assistance on how to get the input values of field that was generated by a loop:
if($moreDetails){
while($row=mysqli_fetch_assoc($moreDetails)){
$id="$row[id]";
$sname="$row[sname]";
$fname="$row[fname]";
$sub="$row[$subject]";
echo "
<tr>
<td>$id</td>
<td>$sname</td>
<td>$fname</td>
<td><label for='score'></label>
<input type='text' class='form-control' name='score'/>
<input type='hidden' name='assessment'/>
</td>
</tr>
";
}
}
My challenge is to get the input of the score entries.

You should give the inputs array-style names.
if($moreDetails){
while($row=mysqli_fetch_assoc($moreDetails)){
$id="$row[id]";
$sname="$row[sname]";
$fname="$row[fname]";
$sub="$row[$subject]";
echo "
<tr>
<td>$id</td>
<td>$sname</td>
<td>$fname</td>
<td><label for='score'></label>
<input type='text' class='form-control' name='score[$id]'/>
<input type='hidden' name='assessment[$id]'/>
</td>
</tr>
";
}
}
Then when you're processing the form, the $_POST parameters will be arrays, so you can access $_POST['score'][$id] and $_POST['assessment'][$id].
It's also strange that you don't have value="something" in the hidden input. Are you using Javascript on the client side to fill this field in automatically?

Related

Need to get the Checked input and checkbox value using jquery in php

Am having table data (retrieve data from mysql table and fetch in to table). table contains several records.I want to display checked checkbox value with input box value and checkbox when i clicking button in php. Checked checkbox value and checked input has deen displayed correctly using join function. but checked with checkbox is not showing correctly. In my code, when i clicking button all checked check values are displayed. my problem to display only checked checkbox with checkbax using join function.
My table:
<table border="0" cellpadding="10" cellspacing="1" width="500" class="tblListForm">
<tr class="listheader">
<td></td>
<td>Username</td>
<td>First Name</td>
<td>Last Name</td>
<td>Permissions</td>
<td>CRUD Actions</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
$classname="evenRow";
else
$classname="oddRow";
?>
<tr class="<?php if(isset($classname)) echo $classname;?>">
<td><input type="checkbox" class="chk_id" name="chk_id" id="chk_id" value="<?php echo $row["userId"]; ?>" /></td>
<td><?php echo $row["userName"]; ?></td>
<td><input type="text" name="firstName" class="firstName" id="firstName" value="<?php echo $row["firstName"];?>" /></td>
<td><?php echo $row["lastName"]; ?></td>
<td><input type="checkbox" name="grant" class="grant" id="grant" value="Y" /></td>
<td><img alt='Edit' title='Edit' src='images/edit.png' width='15px' height='15px' hspace='10' /> <img alt='Delete' title='Delete' src='images/delete.png' width='15px' height='15px'hspace='10' /></td>
</tr>
<?php
$i++;
}
?>
</table>
<input type="button" id="save_value" name="save_value" value="Save" />
my jquery code what i have tried:
$('#save_value').click(function () {
alert("Checkbox running");
var chk_id = [];
var firstName = [];
var grant = [];
$.each($("input[ id='chk_id']:checked"), function () {
chk_id.push($(this).val());
firstName.push($(this).parent().parent().find("#firstName").val());
grant.push($(this).parent().parent().find($("#grant").is(':checked'));
});
alert(chk_id);
alert(firstName);
alert(grant);
});
Here,
am getting checked checkbox and checked input value. my problem to dispaly the checked checkbox with check value.
Thanks#
You made a few small mistakes:
You can't have multiple elements with the same ID, IDs must be unique. So I removed all duplicate IDs from your HTML (id="chk_id",id="firstName",id="grant") and in your JS, used the classes instead.
You missed a closing bracket in grant.push($(this).parent().parent().find($(".grant").is(':checked')));.
.find($(".grant").is(':checked')) isn't working as you expect, and also not necessary.
Use this instead: .find(".grant:checked").
And finally, the reason why your alert showed two values whether the checkboxes were checked or not: grant.push( ... ); always pushes something into the array, if the jQuery-selector matched nothing and would return false, that value would still be pushed into the array.
In fact, if you correct all three points above, and don't check the permission checkbox, the value in the array will be undefined. If you do check the box, it will be Y.
So, in order to make it work, you just have to put the grant.push( ... ); inside an if-clause, where you check for ".grant:checked":
if ($p.find(".grant:checked").length) {grant.push($p.find(".grant:checked").val());}
- $p stands for $(this).parent().parent(), I stored a reference in a var.
- .length checks if the length of the returned object is greater than 0. Without it, the if-clause would still always be true, because jQuery still returns an object (with value undefined).
See code snippet below for a demo:
$('#save_value').click(function() {
var chk_id=[], firstName=[], grant=[];
$.each($("input[class='chk_id']:checked"),function() {
var $row = $(this).parent().parent();
chk_id.push($(this).val());
firstName.push($row.find(".firstName").val());
if ($row.find(".grant:checked").length) {grant.push($row.find(".grant:checked").val());}
});
console.log(chk_id, firstName, grant);
});
table,input[type=button] {float:left;} /*ONLY SO console.log() DOESN'T COVER BUTTON*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="0" cellpadding="0" cellspacing="0" width="500" class="tblListForm">
<tr class="listheader"><td></td><td>Username</td><td>First Name</td><td>Last Name</td><td>Permissions</td></tr>
<tr class="evenRow">
<td><input type="checkbox" class="chk_id" name="chk_id" value="4" /></td>
<td>sardhar</td>
<td><input type="text" name="firstName" class="firstName" value="sardhar" /></td>
<td>mohamed</td>
<td><input type="checkbox" name="grant" class="grant" value="Y" /></td>
</tr>
<tr class="oddRow">
<td><input type="checkbox" class="chk_id" name="chk_id" value="3" /></td>
<td>fg</td>
<td><input type="text" name="firstName" class="firstName" value="vb" /></td>
<td>vb</td>
<td><input type="checkbox" name="grant" class="grant" value="Y" /></td>
</tr>
</table>
<input type="button" id="save_value" name="save_value" value="Save" />
jsfiddle: https://jsfiddle.net/3utno9at/

Echo drop down list in a PHP Form

I have been trying to display this drop down list in my PHP form but when I open it on the browser it gives me a blank page, I have tried opening the page by removing the list and it runs properly. I also tried running the drop down list code independently and it runs properly. I have checked the and codes too but to no avail. The form is in a function as shown below:
function showForm() {
include("library/daloradius.conf.php");
echo " <b>
".$configValues['CONFIG_SIGNUP_MSG_TITLE']."
</b>
<br/><br/>
<form name='signup' action='".$_SERVER['PHP_SELF']."' method='post'>
<table>
<tr><td><b>First name:</b></td><td> <input type='text' value='Conference' name='firstname' /> </td></tr>
<tr><td><b>Number:</b></td><td> <input type='text' value='' name='number' /> </td></tr>
<tr><td><b>Bundle Type</b></td><td>
<select name = "Bundle" size = "1"> <option value ="10MB">10MB</option><option value ="25MB">25MB</option><option value ="50MB">50MB</option></select></td></tr>
<tr><td><b>Enter the verification code in the image:</b> <img src='include/common/php-captcha.php'></td>
<td><input name='formKey' type='text' id='formKey' /></td></tr>
</table>
<br/><br/>
<tr><td><input type='submit' name='submit' value='Register' /> </td></tr>
<br/><br/>
</form>
";
}
switch ($status) {
case "firstload":
You are jumbling with single and double quotes.
<select name = "Bundle" size = "1"> <option value ="10MB">10MB</option><option value ="25MB">25MB</option><option value ="50MB">50MB</option></select></td></tr>
Change it to:
<select name = 'Bundle' size = '1'> <option value ='10MB'>10MB</option><option value ='25MB'>25MB</option><option value ='50MB'>50MB</option></select></td></tr>
Please check for whole code the same way.

Submitting form issues: TypeError: this.form is null

I have a php page that displays all of the service logs for a given site in a table.
At the end of the table I have a new row that has an "Add new service log" button.
Clicking the button hides that row and displays a new row with a form and the desired inputs as well as a new row with buttons "Save" and "Cancel".
The "Save" button calls a Javascript function that checks that the key input has been filled out. When hitting "Save", I see in the console log
TypeError: this.form is null
Here is my code:
// SQL Query
// Table data of queried results
// Add a new service log
echo "<tr id='AddNew'>
<td><input type=button value='Add New' onclick=\"$('#AddNew').hide();$('#AddNewHid').show();$('#SaveCancel').show();\"></td>
<td colspan=12> &nbsp </td></tr>
<form name='AddNewLog' method='POST' action='servicelook.php' id='AddNewService'>
<tr style='display: none;' id='AddNewHid'>
<td><input type='text' value='$lastID' name='ticketid' size=10></td>
<td><input type='text' value='$siteID' name='SiteID' size=4></td>
<td><input type='text' value='$siteName' name='SiteName' size=20></td>
<td><input type='text' value='$userid' name='takenBy' size=4></td>
<td><input type='text' value='$now' name='callTime' size=20></td>
<td><input type='text' value='' name='caller' size=20></td>
<td><input type='text' value='' name='callPhone' size=14></td>
<td><textarea name='issue' value = '' rows=3 cols=10></textarea></td>
<td><textarea name='fix' value = '' rows=3 cols=10></textarea></td>
<td><input type='text' value='$userid' name='solvedBy' size=4></td>
<td><input type='text' value='$now' name='solvedTime' size=20></td>
<td style='min-width:100px;max-width:100px;'><input type='checkbox' name='fup' value='fup'>Follow Up</td></tr>
<input type='hidden' value='Yes' name='AddNew'>
<input type='hidden' value='$userid' name='userid'>
<input type='hidden' value='$session' name='session'>
<input type='hidden' value='$siteid' name='siteid'>
<tr style='display: none;' id='SaveCancel'>
<td colspan=2>
<input name='save' type='button' onclick='inputCheck(this.form.issue);' value='Save'> &nbsp
<input type='button' value='Cancel' onclick=\"$('#AddNew').show();$('#AddNewHid').hide();$('#SaveCancel').hide();\"></td>
<td colspan=11> &nbsp </td>
</tr></form>";
echo "</table>";
My inputCheck function looks like this:
<script type="text/javascript">
function inputCheck(areaName)
{
console.log("checking...");
if (areaName.value.length > 0)
{
$('form#AddNewService').submit();
}
else
{
alert("You didn't describe the problem!");
}
} // end noteCheck function
</script>
I tried changing the submit button to this:
<input type=submit value=Save>
but nothing happens, nothing on the page or in the log.
Anyone have any idea as to what I am doing wrong or what the problem is here?
Thanks in advance!
EDIT:
After using Steven's advice, I was able to successfully submit the form.
However, the inputs aren't properly being sent. I am submitting to the same page so that the table reloads and and the user can see their latest entry. At the beginning of the script I have this:
if($AddNew == 'Yes')
{
echo "YES...Adding New";
echo $ticketid .$SiteID. $SiteName. $takenBy. $callTime. $caller. $callPhone. $issue. $fix. $solvedBy. $solvedTime;
// Start SQL Query
}
The inputs return null. Is there something wrong with the way I am setting my inputs?
The general strategy here is to make the save button a submit button, then put the validation on the form's submit handler and cancel submit if validation fails, e.g.
<form onsubmit="return inputCheck(this.issue);" ...>
then if validation fails, return false from the inputCheck function.
Your issue is probably because you have a form in a place it can't be, so it's being moved outside the table, i.e. you have:
<table>
<form>
...
</form>
</table>
which the browser is "correcting" to:
<table>
...
</table>
<form>
</form>
Now the form is outside the table but the controls are still inside it. So change the markup to:
<form>
<table>
...
</table>
</form>
so the table and form controls stay inside the form.
when this.form is null:
onclick='inputCheck(this.form.issue);'
replace with:
onclick='inputCheck();'
and edit your inputCheck method:
function inputCheck() {
console.log("checking...");
if ($('textarea[name=issue]').val().length > 0){
$('form#AddNewService').submit();
} else {
alert("You didn't describe the problem!");
}
}

Automatically get logged in user's username in Name field of html form

Currently users logged in have to type their name manually to comment or post but i want to
get the username in the field name of html form inserted automatically. Also at the same time user cannot change the name field.
how can i create such session in username field, i tried but code isnt working..
like this
//create the form to submit comments
//you can add more fields, but make sure you add them to the db table and the page, submitcomment.php
echo "
<a name=\"post\">
<div id=\"submitcomment\" class=\"submitcomment\">
<form name=\"submitcomment\" method=\"post\" action=\"submitcomment.php\" onSubmit=\" return form_Validator(this)\">
<table width=\"100%\">
<tr>
<th colspan=\"2\"><h4><span>Leave your comment:</span></h4></th>
</tr>
<th scope=\"row\"><p class=\"req\">Name:</p></th>
<td><input type= class=\"form\" tabindex=\"1\" id=\"name\" name=\"name\" /></td>
</tr>
<tr>
<th scope=\"row\"><p class=\"opt\">Email:</p></th>
<td><input class=\"form\" tabindex=\"2\" id=\"email\" name=\"email\" /></td>
</tr>
<tr>
<th scope=\"row\"><p class=\"opt\">URL:</p></th>
<td><input class=\"form\" tabindex=\"3\" id=\"url\" name=\"url\" /></td>
</tr>
<tr valign=\"top\">
<th scope=\"row\"><p class=\"req\">Comments:</p><br /></th>
<td><textarea class=\"formtext\" tabindex=\"4\" id=\"message\" name=\"message\" rows=\"10\" cols=\"50\"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type=\"submit\" name=\"post\" class=\"submit\" value=\"Submit Comment\" /><br />
<p>Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted. </p>
<p>In Beta Phase</p>
</td>
</tr>
</table>
<input type=\"hidden\" name=\"tuturl\" value=\"$tuturl\" />
<input type=\"hidden\" name=\"tutid2\" value=\"$tutid2\" />
</form>
</div>
";
}
?>
<?php
session_start();
// after user logs in successfully
$_SESSION['username']=$username;
// to read it
$username=$_SESSION['username'];
// then store $username in database when inserting comment
?>
Try using the following code on your page where you want to add name automatically.
<?php
session_start();
$_SESSION['name']=$name;
$name=$_SESSION['name']; // assign session to name variable
?>
write $name anywhere you want name to be displayed.
then edit your following line of code
<td><input type= class=\"form\" tabindex=\"1\" id=\"name\" name=\"$name\" readonly=\"readonly\" /></td>

Passing hidden values, html

I have a the following php file. displayitems.php
<?php
*
*
*
echo "<form action='http://retailthree.nn4m.co.uk/alex/add_data.html'>";
echo "<input type='hidden' name='value' value='$value'/>";
echo "<button type='submit'>Add</button>";
echo "</form>";
?>
Then the html file. add_data.html:
<form method="post" name="form">
<table id="mytable" border="1">
<tr>
<td>Trend <input type="text" name="trend" size="20"></td>
//many other fields
</tr>
</table>
</forn>
Then the aforementioned html will perform an action on a php file.
However that I want to achieve is to pass the hidden data ->$value from the first php file, to the Trend input box(to print the $value content to the input box). Is this possible?
You would simple use the posted variable and place it in the value attribute of your <input> like so:
<input type="text" value="<?php echo $_GET['value'] ?>" name="trend" size="20">
Of course you should do some validation before echoing it to the <input>
EDIT:
Quite rightly mentioned by #ocanal - GET is the default method for forms. You will not be able to process these forms with PHP if your file is *.html it must be a *.php file.
change the name of add_data.html file to add_data.php use following code in add_data.php file
<?php
// your php code
?>
<form method="post" name="form">
<table id="mytable" border="1">
<tr>
<td>
Trend <input type="text" name="trend" size="20"
value="<?php echo $_POST['trend'] ?>">
</td>
//many other fields
</tr>
</table>
</forn>
I'm a little lost but assuming you mean that you want the hidden value to appear in a text input field on another page I would suggest this:
HTML page
<form name='form' action='yourPhpFile.php' method='POST'>
<input name='hiddenGuy' type='hidden' value='hello from the hidden guy'/>
<input type='submit' value='Send'/>
</from>
Now for your php file called yourPhpFile.php
<?php
//your value from the hidden field will be held in the array $_POST from the previous document.
//the key depends on your field's name.
$val = $_POST['hiddenGuy'];
echo "<form name='form' action='yourPhpFile.php' method='POST'>
<input name='showingInText' type='text' value='".$val."'/>
</from>";
?>
This can be achieved on the same page too by removing the form action attribute. and echoing the a different input type and value depending on whether $_POST is set using the isset method.
if(isset($_POST['hiddenGuy'])){
echo "<input name='showingInText' type='text' value='".$_POST['hiddenGuy']."'/>";
}
else{
echo "<input name='hiddenGuy' type='hidden' value='hello from the hidden guy'/>
<input type='submit' value='Send'/>";
}

Categories