PHP
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<tr>";
for($i=0;$i<7;$i++){
if($i==0){
echo "<td><input type='text' val='$row[$i]' /></td>";
}
if($i>1){
echo "<td>$row[$i]</td>";
}}echo "</tr>";
}
Except for the first column, which contains the input tag, everything comes out fine. The input tag shows up, but it appears empty--nothing to see, nothing to highlight. If I do 'inspect element', however, I see it has the correct value according to the output from the queried table.
Any thoughts as to what causes this strange behavior and how to fix it?
You have to use basic HTML Tag
You have to use "value" instead of "val"
See the Basic HTML Tag
<input type="text" value="Nikunj"/>
You should use value attribute instead of val:
echo "<td><input type='text' val='$row[$i]' /></td>";
-----------------------------^
should be:
echo "<td><input type='text' value='$row[$i]' /></td>";
-----------------------------^
Change val to value. val is not a valid attribute.
<input type='text' val='$row[$i]' />
--------------------^
Related
i want to print a input value using html.
input will be like below..
<input type='text' name='tr_id' class='form-control' id='tr_id' style='height:35px;width:200px;' value='<?php echo $row['$tr_id'];'>
i am using below code to get the required input
$field_name='tr_id';
$field_type='text';
$html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='<?php echo $row['".field_name."']'>";
echo $html;
but i think i am getting error on value. can you please help how can i print
Your intent is not so clear, however it might be logical to assume that you may really have a typo in your PHP Code where you were echoing out the field_name.
Here is a correction on that part.
<?php
$field_name ='tr_id';
$field_type ='text';
$html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='{$row[$field_name]}' />";
echo $html;
Notice that the line: $html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='<?php echo $row['".field_name."']'>";
has changed to reflect what is presumably what you intended to do: $html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='{$row[$field_name]}' />"
The reason for this is because you are already in PHP Mode so you don't need to do something like <?php echo $row...?> within a PHP block... (meaning: you don't open & close a PHP Block within an already existing PHP Block) and, in fact, since you are doing echo $html it would be pointless to echo anything again within the code that builds up your string. The Point here is that you should rather build-up your String without echoing anything and then finally echo $html once you are done building up your HTML String. This way you'd avoid the Errors you got.
try below code..
<input type='text' name='tr_id' class='form-control' id='tr_id' style='height:35px;width:200px;' value='<?php echo $row[$tr_id];'>
And
$field_name='tr_id';
$field_type='text';
$row = array('field_name','test');`enter code here`
$html .= '<input type ="'.$field_type.'" name="'.$field_name.'" class="form-control" id="'.$field_name.'" style="height:35px;width:200px;" value="'.$row["field_name"].'" >';
echo $html;
Is it possible to use PHP variables as attributes in form inputs?
I have the following variable already declared:
$InStock //number of items in stock
I would like to use this variable per below, but the code isn't working (the max constraint is not being applied to the field):
print "<td><input type='number' name='product1' id='product1' min='0' max='<?php echo $InStock ?>' value='0'></td>";
Is this legal, or am I attempting something syntactically impossible? Sorry if this is a stupid question - I'm new at this, and I embarrass myself frequently. : /
It's possible, your syntax is incorrect though. You're double dipping on the php brackets.
I usually prefer to do something like this instead of using print, it's easier to read, especially if your IDE is doing syntax highlighting:
?><td><input type='number' name='product1' id='product1' min='0' max='<?php echo $InStock ?>' value='0'></td><?php
You could also do it within a print/echo statement, but you need to have the variable inline without the extra brackets:
echo "<td><input type='number' name='product1' id='product1' min='0' max='$InStock' value='0'></td>";
Note that without concatenation, this only works with double quotes.
Instead of your:
print "<td><input type='number' name='product1' id='product1' min='0' max='<?php echo $InStock ?>' value='0'></td>";
Use:
print "<td><input type='number' name='product1' id='product1' min='0' max='$InStock' value='0'></td>";
This is because PHP will insert the values of variables into a string if you put the variable name into the string and PHP will replace this with the value of your variable. For example print "Hi there, $name"could produce "Hi there, James" when executed - assuming $name is equal to "James".
Please check this Muddal: http://muddal.com/muddaltut.php?id=163
I have multiple input elements arranged in 16 rows and 5 columns (Just like a grid) . The contents of the 16 rows are inserted into a mysql table as 16 records with each record contains 5 fields. I want a method to get the value of all the input into an array using jquery and pass this to a ajax post request.
dataentry.php contains this code
jQuery('#metentry').submit(function(e){
e.preventDefault();
// getting data from form to variables
var date=jQuery('#stddate').val();
var kgsunit=jQuery('#unit').val();
var kgsshift=jQuery('#shift').val();
//sending data to script
jQuery.post("get_blank_form.php", {
"date":date,
'unit':kgsunit,
'shift':kgsshift,
}, function(data) {
//displaying message
jQuery('#blankform').html(data);
jQuery('#formpanel').hide();
});
get_blank_form.php contains this code:
echo'<form id="shiftentry" name="shiftentry" >';
echo "Date:<input id=shiftdate value='".$date."'/>";
echo "Unit:<input id=shiftdate value='".$unit."'/>";
echo "Shift:<input id=shiftdate value='".$shift."'/><br/>";
echo "<table><tr><th>No</th><th>Ele</th><th>Location</th><th>Drate </th><th>H3 Val </th><th>Part </th> <th>Iod</th><th>Cont. </th></tr>";
while($row2=mysql_fetch_array($result_sec))
{
echo "<tr>";
echo "<td>".++$rc."</td>";
echo "<td><input size='5' id='".$row2['ele']."' value='".$row2['ele']."' /></td>";
echo "<td><input id='".$row2['loc_id']."' value='".$row2['loc']."' /></td>";
echo "<td><input size='5' id='drate".$rc."' value='".$row2['drate']."'/></td>";
echo "<td><input size='5' id='h3".$rc."' value='0' /></td>";
echo "<td><input size='5' id='part".$rc."' value='0' /></td>";
echo "<td><input size='5' id='iod".$rc."' value='0' /></td>";
echo "<td><input size='5' id='cont".$rc."' value='0' /></td>";
echo "</tr>";
}
echo " </table>";
echo '<div align="center">';
echo '<input type="submit" id="submit2" name="submit2" value="Submit" width="30" />';
echo '</div>';
echo '</form>';
Both the above scripts are working and this will add a form"shiftentry" to the DOM in dataentry.php. This form conains around 21 rows and 8 columns containing input elements. I want a method to get the values in this 21 x 8 input elements and pass it to a jQuery post request. There are two issues.
Since the form is added after the DOM is loaded, how to get the value?
Is there any way to read all the input values using a loop instead of separate lines for each input element?
My final aim is to append this 21 rows into a mysql table. This part I knew, once I get all the values. Any suggestions ?
I have this function which create a multi dimensional array of input values. But in this casethe 'form1' is loaded before DOM is completed. But in the above scenario, form is dynamically added after DOM loding is completed
jQuery('#form1').submit(function(e){
e.preventDefault();
var arr = jQuery('#form1 tr:gt(0)').map(function() {
return [jQuery('input', this).map(function() {
return this.value;
}).get()];
}).get();
});
I also have another jquery script in this page which is used to traverse through the input boxes (obviously, these input boxes are loaded after DOM)
jQuery(document).delegate('input','keyup',function(e){
if(e.which==39)
jQuery(this).closest('td').next().find('input').focus();
else if(e.which==37)
jQuery(this).closest('td').prev().find('input').focus();
else if(e.which==40)
jQuery(this).closest('tr').next().find('td:eq('+jQuery(this).closest('td').index()+')').find('input').focus();
else if(e.which==38)
jQuery(this).closest('tr').prev().find('td:eq('+jQuery(this).closest('td').index()+')').find('input').focus();
});
How can I use the delegate option in the script used for traverse in the script for getting input values?
On form submit we can use $("#frmServiceMaster").serialize() to get the serialize string if you want JSON then use $("#frmServiceMaster").serializeArray()
It will convert all form input to String or JSON
$("#frmServiceMaster").submit(function(e){
event.preventDefault();
alert($("#frmServiceMaster").serialize());
alert($("#frmServiceMaster").serializeArray());
});
working FIDDLE
I have tried to search through the forums but I am a novice and am getting more confused.
I am trying to bring an input from a form and use it as a variable in a MySql query. A shortened version of the form is -
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input value=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";
I am then trying to store the input into a variable using code -
$newVar = $_GET['leave'];
However I am getting an undefined index error.
Can anyone help with this? Sorry if its a very basic problem :)
The problem is with your HTML. You need to name the input.
echo '<input name="leave" class="text" autocomplete="off" type="text" value="' . $_SESSION['leave'] . '" />';
You declaring the "value attribute twice, you need to declare name:
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input name=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";
echo '<form method="get" action="">';
echo "<tr><td>Leave:</td><td><input value='{$_SESSION['leave']}' class='text' autocomplete='off' type='text' name='leave'/></td></tr>";
echo "</form>";
If you use single quotes and doubles quotes alternating, you can make your code look nicer.
For your problem, you're missing your input name:
<input type=".." name="leave" ..>
Also, notice in your output of the field, you have the value set to the session value and an empty value near the end.
value=\"\"
I want to send data from one page to the other via a form in PHP. In the initial page I have included the following PHP script that generates an input form with hidden fields. The names of these hidden fields are generated by the PHP:
<?php
echo "<form class='available-form' name='available_os' method='get' action='process-results.php'>";
echo "<input type='hidden' name='$software'></input>";
echo "<input type='hidden' name='$version'></input>";
echo "<input type='submit' name='available-button' value='Find Available Libraries for this Software'></input>";
echo "</form>";
?>
In the second page, named process-results.php, I would like to get the names of these hidden fields via the $_GET method but of course using $_GET[$software] and $_GET[$version] wouldn't work...Can someone tell me if there is a solution to this issue or if there is a better alternative? Thanks in advance
Instead of
"<input type='hidden' name='$software'></input>";
you should use
"<input type='hidden' name='software' value='".$software."'></input>";
for each. This way, you can use $_GET['software'] to retrieve the value. Do this for each of your hidden inputs.
I think you may want something like:
<form ... >
<input type="hidden" name="software" value="<?php echo $software ?>" />
<input type="hidden" name="version" value="<?php echo $version ?>" />
</form>
and then
$_GET['software'];
$_GET['version'];
I'm not sure what you're trying to accomplish, but this looks odd to me. Isn't the below code more of what you're looking for?
<?php
echo "<form class='available-form' name='available_os' method='get' action='process-results.php'>";
echo "<input type='hidden' name='software' value='$software'></input>";
echo "<input type='hidden' name='version' value='$version'></input>";
echo "<input type='submit' name='available-button' value='Find Available Libraries for this Software'></input>";
echo "</form>";
?>
That way you will get a query string in form of ?software=yoursoftwarename&version=yourversion and it will be available via $_GET["software"] and $_GET["version"] on the next page.
You could iterate over each of the items in the $_GET array on process-results.php. The problem is that the keys for the value will be whatever $software and $version are set to on the first page. Try something like this:
foreach($_GET as $key=>$string) {
// Do stuff with them
}
Add
enctype="multipart/form-data"
To the tag... so it looks like
<form enctype="multipart/form-data" method......
If you really need to have the dollar-sign inside the name, escape it:
echo "<input type='hidden' name='\$software'>";
or put the string in single-quotes:
echo '<input type="hidden" name="$software">';
Otherwise PHP is looking for a variable named "$software", if you look inside the browser-source you will see that the name-attributes are empty(except you're having those variables defined somewhere).