Updating what has worked and the current issue:
I have 1 clear button beside each field. Each clear button works. The problem is after clearing both fields, when selecting another file from the dropdown, it only populates the text field with the file name and does not display the contents in the second textarea until refreshing the page.
<input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection" id="Selection" value="-1"><div><font color=#2F6054>Below is the list of your saved codes. To view or delete your codes, select it from the list.</font></font></div><p>
<select size="1" name="CodeList" id="CodeList">
<?php
$directory = $directory = 'users/' . $_SESSION['username'];
$filesContents = Array();
$files = scandir( $directory ) ;
foreach( $files as $file )
{
if ( ! is_dir( $file ) )
{
$filesContents[$file] = file_get_contents($directory , $file);
echo "<option>" . $file . "</option>";
}
}
?>
</select><p>
<font color=#2F6054><font size=4>Enter Codes Here</font></font>
<form method="post" action="/evo/avsaveprocess.php">
<input type="hidden" name="Action" value="SAVE" />
<input type="hidden" name="CodeId" id="CodeId" value="0" />
<table width="100%">
<tr>
<td>Description:</td>
<td><input type="text" name="CodeDescription" size="40" maxlength="50" id="CodeName" value="" /></td>
<td><button type="reset" class="clear-tn" value="Clear"></button></td>
</tr>
<tr>
<td valign="top">Code:</td>
<td>
<textarea rows="10" style="width:99%" name="Code" id="CodeValue"></textarea>
</td>
</tr>
</table>
<input type="submit" value="Save" />
function code:
<script>
$(function(){
$('.clear-btn').on('click',function(e){
e.preventDefault();
$(this).closest('tr').find('input[type="text"],textarea').val('');
});
});
</script>
Edited to show current working code
Editing to show on change script at the bottom of my form for the dropdown
<script>
$(document).ready(function(){
// apply a change event
$('#CodeList').change(function() {
// update input box with the currently selected value
$('#CodeName').val($(this).val());
$.get( '<? echo $directory ?>' + '/' + $('#CodeName').val(), function( data ) {
$( "#CodeValue" ).val( data );
});
});
});
</script>
You can try the below link, it works without javascript or jquery. Just simple HTML
<form>
<input type="reset" />
<input type="submit" />
<input type="text" />
<textarea></textarea>
</form>
Here is the demo
Change type attribute "button" to "reset".
<button type="reset" class="clear-btn">Clear</button>
Try below working fiddle as per your requirement.
http://fiddle.jshell.net/t7gkr8rk/1/
Related
How can I get the values in the while loop using button with js?
<?php
while ($row = mysqli_fetch_array($query))
{
echo '<input type="text" name="sample" value="'.$row['sample'].'">';
}
?>
Thanks
You can get the value of textbox when you click on a button.
<input type="text" name="sample[]" value="abc" class="valueInput">
<input type="text" name="sample[]" value="xyz" class="valueInput">
<input type="text" name="sample[]" value="pqr" class="valueInput">
<input type="button" class="getValue" value="Get Value">
Note: I have set the static input box you can make it dynamic but make sure please add the class as valueInput.
Jquery Code.
$(document).on('click','.getValue',function(){
var valArr = [];
$(".valueInput").each(function(){
valArr.push($(this).val());
});
console.log(valArr);
})
The name of the input field should indicate an array with [].
<form action="action.php" method="POST">
<?php
while ($row = mysqli_fetch_array($query)){
echo '<input type="text" name="sample[]" value="'.$row['sample'].'">';
}
?><input type="submit">
</form>
action.php: (Processes the data on submit)
<?php
echo "<pre>";
print_r($_POST['sample']);
echo "</pre>";
I wanna submit/write content to a .txt file and i have to used php to do it, but i don't wanna open the php page and wanna stay on the same page.
How can I do this?
Index.html
<form action="writer.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
writer.php
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "|| \n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo " written to file";
}
}
else {
die('no post data to process');
}
?>`
You could simply use iframes, easier alternative to AJAX.
<iframe name="panel" style="display:none;"></iframe>
<form action="writer.php" method="POST" target="panel">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
...and as everyone here is yelling, consider learning AJAX.
create a php file with following in it.
<?php
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
file_put_contents('mydata.txt', $input, FILE_APPEND | LOCK_EX);
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php if(isset($message)) echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
friends.I am stuck with some javascript function to read keyup function to a text field with id as array(qty[]).Please help me
<table id="dynamic">
<tr>
<td>
<label for="textfield">product :</label>
</td><td> <select name="product" id="product[0]">
<?php
$result=mysql_query("SELECT * FROM `product`");
while($row=mysql_fetch_array($result)){
?>
<option value=" <?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
<?php }?>
</select>
<label for="qty">Qty :</label>
<input type="text" name="qty" id="qty[0]"/>
<label for="textfield">Price :</label>
<input type="text" name="price" id="price[0]" readonly/>
<button type="button" name="add" id="add" class="btn btn-success">Add More</button>
</td> <div id="loader"></div></tr>
</table>
javascript
appending dynamic fields
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
$('#dynamic').append('<tr id="row'+i+'"><td><label for="textfield">product :</label></td><td><select name="product" id="product['+i+']"><?php $result=mysql_query("SELECT * FROM `product`");while($row=mysql_fetch_array($result)){?><option value=" <?php echo $row['id']; ?>"><?php echo $row['name']; ?></option><?php }?></select><label for="qty">Qty :</label><input type="text" name="qty" id="qty['+i+']"/><label for="textfield">Price :</label><input type="text" name="price" id="price['+i+']" readonly/></td><td><button type="button" name="remove" class="btn btn-danger btn_remove" id="'+i+'">X</button></td></td><div id="loader"></div></tr>');
i++;
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
});
</script>
here i tried to access the qty[0] variable.but its not working.When its not array working fine.
<script type="text/javascript">
$(document).ready(function()
{
var i=1;
$("#qty[0]").keyup(function()
{
var qty=$("#qty[0]").val();
var pdt=$("#product[0]").val();
i++;
$.ajax({
type:"POST",
url:"price_ajax.php",
data : { qty : qty, pdt : pdt },
beforeSend: function () {
$('#loader').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success:function(data)
{
$("#price[0]").val(data);
$("#loader").html("");
}
});
});
});
Change
$('#add').click(function() {
$('#dynamic').append('... id="product['+i+']">...');
i++;
});
To
$('#add').click(function() {
$('#dynamic').append('... id="product_'+i+'">...');
i++;
});
id="product[0]" will become id="product_0"
Apply same method to other dynamic elements and make relevant modifications in your jQuery selector.
Everything will work fine.
Also, I would like to add here that, please break up your append call. Javascript, HTML and PHP, everything is happening in one single line. Which is very confusing. Prepare an HTML in javascript variable. Once done, append it.
Update your code as mentioned below:
HTML Changes:
Add class class='qty' in input box in id="qty[0]" field
$('#add').click(function(){
$('#dynamic').append('<tr id="row'+i+'"><td><label for="textfield">product :</label></td><td><select name="product" id="product['+i+']"><?php $result=mysql_query("SELECT * FROM `product`");while($row=mysql_fetch_array($result)){?><option value=" <?php echo $row['id']; ?>"><?php echo $row['name']; ?></option><?php }?></select><label for="qty">Qty :</label><input type="text" name="qty" class="qty" id="qty['+i+']"/><label for="textfield">Price :</label><input type="text" name="price" id="price['+i+']" readonly/></td><td><button type="button" name="remove" class="btn btn-danger btn_remove" id="'+i+'">X</button></td></td><div id="loader"></div></tr>');
i++;
$( ".qty" ).each(function( index ) {
$(this).keyup(function() {
// your code here
});
});
});
Jquery Changes:
$( ".qty" ).each(function( index ) {
$(this).keyup(function() {
// your code here
});
});
I got a post form and the issue is that It errors when I try to post a link like https://www.google.com/ into an imput type text, It akts as some command I do not understand, anyway heres my code and let me know what exactly is wrong.
<?php
if(isset($_POST['add'])){
$animeid = $_POST['animeid'];
$number = $_POST['number'];
$code = $_POST['code'];
echo $animeid;
echo $number;
echo $code;
}
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="600" border="1" cellspacing="1" cellpadding="2">
<tr>
<br>
<input class="inputs" placeholder="Link" name="code" type="text" id="code">
</br>
<br>
<input class="inputs" placeholder="ID" name="id" type="number" id="id">
</br>
<br>
<input class="inputs" placeholder="Number" name="number" type="number" id="number">
</br>
<br>
<center><input class="btnExample" name="add" type="submit" id="add" value="Add Episode!"></center>
</td>
</tr>
</table>
</form>
And its a simple thing really If Id to type anything other than a link it would work but I need it to be a link and thats that , I still can't see why from dosen't post links and Id like to know how can I make it post links.!
OK I think I know how to solve It But Only Know How To I will really need help on it as I did not learn javascript but There should be a way to remove the http:// or https:// before Posting The FORM And then Ill reADD it in the PhP but Now What I got To Figure It Is How Can I make a javascript that will autoremove http:// or https:// Before posting The FORM , Thanks for the Answers You Gave Me Till Now , I will apriciate If SOmeone Will Know How to autoremove words with javascript Thanks !
Well Its Simple , I Figured that Links act as A Code that wants to be posted to so what acutally happens when You Post it Its Simple Instead of Posting It To http://anime4life.net/ it Posted It To http:// linkthatIinitiallyTypedIntheInput+anime4life.net which caused to have a very long and unsucessful end
Well The Solution Is Simple , First AutoRemove The http:// straight as Im TYping Using JsQuery Like This
$('input').change( function() {
var input = $('input');
input.val(
$('input').val().replace(/https?:\/\//gi,'')
);
});
And Then That Posts The Form
Next in the echo Use This
<?PHP
echo "https://".$_POST['code']."";
And That Puts It Back For The Display , Well Thankyou All For Trying To Help!
Action is where you're sending the data to get posted (i.e. #) for same page
You need to run a set check on each variable. i.e.:
if (isset($_POST['fromPerson']){
$fromPerson=$_POST['fromPerson'];
}
change
<form method="post" action="<?php $_PHP_SELF ?>">
in
<form method="post" action="?">
and change
<?php
if(isset($_POST['add'])){
$animeid = $_POST['animeid'];
$number = $_POST['number'];
$code = $_POST['code'];
echo $animeid;
echo $number;
echo $code;
}
?>
in
<?php
echo (isset($_POST['animeid'])) ? $_POST['animeid'] : '';
echo (isset($_POST['number'])) ? $_POST['number'] : '';
echo (isset($_POST['code'])) ? $_POST['code'] : '';
?>
your code
<?php
echo (isset($_POST['animeid'])) ? $_POST['animeid'] : '';
echo (isset($_POST['number'])) ? $_POST['number'] : '';
echo (isset($_POST['code'])) ? $_POST['code'] : '';
?>
<form method="post" action="?">
<table width="600" border="1" cellspacing="1" cellpadding="2">
<tr>
<br>
<input class="inputs" placeholder="Link" name="code" type="text" id="code">
</br>
<br>
<input class="inputs" placeholder="ID" name="id" type="number" id="id">
</br>
<br>
<input class="inputs" placeholder="Number" name="number" type="number" id="number">
</br>
<br>
<center><input class="btnExample" name="add" type="submit" id="add" value="Add Episode!"></center>
</td>
</tr>
</table>
</form>
I have a page that when a link is clicked it opens a pop up box to insert new charges. When this pop up box opens I use jquery load() function to insert a table with all the current charges (loaded from mysql) from a script on another page called load_charges.php.
The problem I am having is I can not access any of the hidden values from the content I just loaded I use:
var charge_id = $(':hidden:first', $(this)).val();
this normally gets me the value of the first hidden element but it does not work if I am trying get the info from the page load from load_charges.php. I will list my code below:
case_cpanel.php:
//HTML Table with Form elememt
<table>
<tr class="lead_hover">
<td>
<form class="calc" title="Case Expense Calculator" style="cursor:pointer;">
<input type="hidden" id="lead_id" value="21946295" />
<input type="hidden" id="final_id" value="74" />
<input name="order" type="hidden" id="final_id" value="3" />
</form>
</td>
</tr>
<tr class="lead_hover">
<td>
<form class="calc" title="Case Expense Calculator" style="cursor:pointer;">
<input type="hidden" id="lead_id" value="21978679" />
<input type="hidden" id="final_id" value="79" />
<input name="order" type="hidden" id="final_id" value="1" />
</form>
</td>
</tr>
</table>
// Jquery to load popup form box
$(".calc").click(function () {
var value = $(':hidden:eq(0)', $(this)).val();
$('input[name=lead_id]').val(value);
var value = $(':hidden:eq(1)', $(this)).val();
$('input[name=final_id]').val(value);
var value = $(':hidden:eq(2)', $(this)).val();
$('.order').val(value);
var lead_id = $(':hidden:eq(0)', $(this)).val();
var string1 = "token=<? echo $_SESSION['token']; ?>&lead_id=";
var url = "../ajax/load_charges.php";
var datastring = string1.concat(lead_id);
$('#calc_right_display').html('<div><img src="../imgs/loading4.gif" align="center" /></div>').load(url, datastring).show();
$("#calc_div").overlay().load();
});
load_charges.php:
// table that is load with new form elements
<table width="266" border="0" cellpadding="5">
<? do { ?>
<tr>
<td width="163">
<? echo $row_charges[ 'rows'][ 'title']; ?>
</td>
<td width="83">$
<? echo $row_charges[ 'rows'][ 'charge']; ?>
</td>
<td width="27">
<form class="delete_charge" title="Delete Charge" style="cursor:pointer;">
<input type="hidden" id="id1" value "<? echo $row_charges['rows']['id']; ?>">
</form>
</td>
</tr>
<? } while ($row_charges[ 'rows']=m ysql_fetch_assoc($row_charges[ 'query'])); ?>
</table>
//Jquery Code on load_charges.php
<script>
$(".delete_charge").click(function () { * *
var charge_id = $(':hidden:first', $(this)).val(); * *
var str2 = "token=<? echo $_REQUEST['token']; ?>&delete_charge=true&id=";
var str_lead_id = "&lead_id=<? echo $_REQUEST['lead_id']; ?>";
var url2 = "../ajax/cm_expenses_delete.php";
var datastring2 = str2.concat(charge_id, str_lead_id);
$('#calc_right_display').html('<div><img src="../imgs/loading4.gif" align="center" /></div>').load(url2, datastring2).show();
});
</script>
The problem is var charge_id = $(':hidden:first', $(this)).val(); is not returning any value from the new page that just loaded. Any Help would be greatly appreciated.
Thank you in advance.
Ryan
You may be having a problem retrieving the value because you are missing an equal sign.This:
<input type="hidden" id="id1" value"<? echo $row_charges['rows']['id']; ?>">
Change to:
<input type="hidden" id="id1" value="<? echo $row_charges['rows']['id']; ?>">
This would explain it because your input has no value.
I made the following test, based on your data:
$(document).ready(function(){
var charge_id = $('input:hidden:first').val();
alert(charge_id);
var charge_id = $('input[type=hidden]:first').val();
alert(charge_id);
});
Working fiddle here: http://jsfiddle.net/robertrozas/c4AHM/1/