I have a PHP variable which I am trying to get to appear onto an input field without using the 'value' attribute but I can't seem to get that to work.
I tried a jQuery code I found first to 'append' the variable to the input field but that didn't work.
var first_name = "<?php echo $current_user['first_name']; ?>";
alert(first_name);
$('#first_name').val($('#first_name').val() + first_name);
I then tried using getElementById but it also didn't work.
var first_name = "<?php echo $current_user['first_name']; ?>";
alert(first_name);
document.getElementById('first_name').value = first_name;
Any suggestions would be greatly appreciated!
EDIT:
This is the HTML form
<form name="personal_form" action="includes/editpersonal.php" method="POST">
<fieldset>
<legend><h2>Personal Details</h2></legend>
<table class="personalform">
<!--if there is an error on first name, the class name 'form_error_row' will be input into the following tr-->
<!--allows me to colourise the text field to indicate an error on this row-->
<tr class="<?php echo form_row_class("first_name") ?>" >
<th><label for="first_name"><p>First Name: </p></label></th>
<td>
<input type="text" name="first_name" id="first_name" size="40" />
<!--if there is an error it will print out a div with the error message-->
<?php echo error_for('first_name') ?>
</td>
</tr>
</table>
</fieldset>
<input type="submit" class="reg-button" value="Update Records" />
</form>
Try assigning value in document.ready to ensure html elements are ready for to be accessed by the script.
$(document).ready(function(){
$('#first_name').val($('#first_name').val() + first_name);
});
Related
Hi I try to sum two data using AJAX and display it into hidden text input after that, I will pass the variable using PHP post method.
There are no problems in my jQuery AJAX code but the problem is after I submit the button, the value I retrieve from textbox total_rec[] is blank.
Here's my code below.
HTML:
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<table>
<thead>
<tr><th>Test</th></tr></thead>
<tbody>
<tr><td>
<input type="hidden" name="last_item_rec[]" value="<?php echo $row->rec_qty; ?>">
<input type="text" name="item_rec[]" id="txt" disabled="">
<input type="hidden" name="total_rec[]" value="">
</td><tr>
</tbody>
</table>
</form>
JQUERY AJAX:
<script>
$(document).ready(function(){
$("input[name=item_rec\\[\\]]").on('keyup',function(){
var one = $(this).parents('tr').find('input[name=last_item_rec\\[\\]]').val();
var two = $(this).parents('tr').find('input[name=item_rec\\[\\]]').val();
sum = parseInt(one) + parseInt(two);
$(this).parents('tr').find('input[name=total_rec\\[\\]]').val(sum);
});
});
<script>
PHP CODE: (recitem_insert.php)
$total_rec = $_POST['total_rec'];
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$query=mysqli_query($con,"UPDATE tblstock
SET
rec_qty='$total_rec[$j]'
WHERE id = '$check[$i]'
")or die(mysqli_error($con));
}
}
As you told that the item contain 2 or more value. So you can use class instead of name.
HTML
<input type="hidden" class="last_item_rec" name="last_item_rec[]" value="23" />
<input type="text" class="item_rec" name="item_rec[]" id="txt" />
<input type="hidden" class="total_rec" name="total_rec[]" value="" />
jQuery
$(function(){
$(".item_rec").on('keyup',function(){
var one = $(this).parents('tr').find('.last_item_rec').val();
var two = $(this).parents('tr').find('.item_rec').val();
sum = parseInt(one) + parseInt(two);
console.log(sum);
$(this).parents('tr').find('.total_rec').val(sum);
});
});
I have this piece of code:
<form action = "save.php" method = "post" onsubmit = "return submitForm()">
<tbody id = "foo">
<input type="text" name="inputField" id="inputField" value = "<?php echo $someValue ?>">
</tbody>
<input type="submit" value="Save configuration" />
</form>
My question is: How to prevent submitting variable $someValue to save.php page?
I tried with JS:
document.getElementById("inputField").readOnly = true;
document.getElementById("inputField").disabled = true;
Which disables the field, yet does not prevent from submitting content of the input field.
Remove all name attribute from your foo section.
By example
<tbody id="foo">
<input type="text" id="thisInputWillNotBeSubmited1" value = "<?php echo $someValue ?>">
<input type="text" id="thisInputWillNotBeSubmited2" value = "<?php echo $someOtherValue ?>">
</tbody>
Or if you prefer jQuery (after your DOM is loaded)
$("#foo > input").each(function() {
$(this).removeAttr( "name" );
});
$("form").submit(function() {
$("input[name='inputField']").remove();
});
I am trying to get a input textbox value in a PHP variable and then update a table based on this variable value in my custom WordPress page. I have written following code.
It is working but not updating the text input value. What is wrong with my code?
<form method="post" enctype="multipart/form-data">
<td>
<table> <tr><td> </td><input type="text" name="offeramt" style="height:15px"/> <td>
<input type="hidden" name="pid" value= "<?php echo $retrieved_data->id; ?>">
<input type="hidden" name="textamount" value="<?php echo htmlspecialchars($_POST['offeramt']);?>">
<input type="submit" name="update" value="update" /></td> </table></td>
</form>
<?php
if (isset($_POST['update'])) {
$sellamt=$_POST['textamount'];
if (empty($sellamt)){
echo "pls input a value in text box";
}
else{
$myid = $_POST['pid'];
?> <?php
$wpdb->update('wp_share', array('status'=>'onsell','offervalue'=>$sellamt),array('id'=>$myid));
}}
}?>
I was updating the text input value to a hidden field ["textamount"]then trying to get that hidden field ["textamount"] value to $sellamt. However I have removed the hidden field and took the value directly from textbox like this
$sellamt=$_POST['offeramt'];
where ['offeramt'] is the textbox itself
and its working.Thanks
I am a new to this type of coding so I was wondering if someone could help me.
Here's what I want to achieve, an input field where the user can enter text and have another text appended to this. So for example, when the user enters text e.g "My Name!!", upon posting there would be another hidden text appended to this, so the server would receive "Hidden Text!","My Name!!".
Here's an image explaining this in an easier way.
Here is my code so far..
<form method="post" action="jumpin.php">
<label>Desired Username:</label>
<div>
<label id='labletext'><?php echo $_SESSION['user_name_custom']; ?></label>
<input type="text" id="userid" name="userid" />
<input type="submit" value="Check" id="jumpin" />
</div>
<script>
$('#userid').keyup(function(){
$(this).css('color','#000');
});
$('#userid').blur(function(){
var value = $('#labletext').text()+$(this).val();
$(this).val(value);
});
</script>
</form>
This code doesn't seem to be working, all the server receives is the text the user submitted and not the "labletext".
You can use a hidden input field.
<input type="hidden" name="extra_label" value="<?php echo $_SESSION['user_name_custom']; ?>" />
Not visible to your users, but the data is passed to your server.
In your server-side code, you'll access the variable like $_REQUEST['extra_label'].
<input type="hidden" name="label" value="yourvalue" />
And in your submission you can do this
if(isset($_POST['submit']))
{
$text = $_POST['text'];
$label = $_POST['label'];
$string = $label.$text;
}
You have to put the concatenated value into the form field so it will be submitted to the server when you post the form.
$('#userid').blur(function(){
var value = $('#labletext').text().trim() + ' ' +$(this).val();
$(this).val(value);
});
I have an appendchild-function that adds form elements. In IE, everything works fine; the process.php is able to $_POST it. But in firefox, it doesnt send the data.
Here is my code.
<script type="text/javascript">
var i=0;
function addElement()
{
var ni = document.getElementById('org_div1');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newDiv = document.createElement('div');
var divIdName = num; newDiv.setAttribute('id',divIdName);
newDiv.innerHTML = '<input type="text" name="work" /><input type="file"
class="fileupload" size="80" name="file' + (num) +'" onclick="addElement()"/> <a
class="removelink" onclick=\'removeElement('+divIdName+')\'>Remove This File</
a>';
// add the newly created element and it's content into the DOM
ni.appendChild(newDiv);
}
function removeElement(divNum)
{
var d = document.getElementById('org_div1');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
</script>
<td>
<div class="file_input_wrapper">
<input type="hidden" value="1" id="theValue" />
<div id='org_div1'>
<input type="file" class="fileupload" name="file1" size="80" onclick="addElement()" />
</div>
</td>
Solved the problem...
Basically, I had this..
<div>
<form>
</div>
</form>
And changed to this..
<form>
<div>
</div>
</form>
Seems firefox doesnt like invalid html.
You can use PHP's field name array functionality to get around having to keep track of your field names. Simply name the field like this:
<input type="file" name="files[]" ... />
^^--- array notation
and PHP will handle each file box as a separate member in the $_FILES array after the form's submitted. This frees you up from all the extra overhead of keeping track of how many boxes there are and hidden form fields to store the value.
You may want to reconsider having the file element's onclick be the thing that adds a new file input. What happens if someone clicks on the "browse" button to add a file? They'll get a new file input box, even though they may only have wanted one. If they choose the wrong file or change their minds later and click browse again to change the file selection, they'll get yet another input box.
Consider having a dedicated "add another box" button instead.
Just want to ask where your form tag is located? Before or after table tag? I had similar problem and my form tag was inside table. When I put form tag outside of table everything worked fine.
Site doesn't work anymore.
Here is working example. I used your code. Only two thing I changed is
<input type="text" name="work[]" /> instead of <input type="text" name="work" />
and you was missing one </div> closing div tag
here is code (tested on IE7, IE8, FF and google chrome)
<?php
if (!empty($_POST['btnProsledi'])){
print_r($_POST);
echo "<br />";
print_r($_FILES);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
var i=0;
function addElement()
{
var ni = document.getElementById('org_div1');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newDiv = document.createElement('div');
var divIdName = num; newDiv.setAttribute('id',divIdName);
newDiv.innerHTML = '<input type="text" name="work[]" /><input type="file" class="fileupload" size="80" name="file' + (num) + '" onclick="addElement()"/> <a class="removelink" onclick=\'removeElement(' + divIdName + ')\'>Remove This File</a>';
// add the newly created element and it's content into the DOM
ni.appendChild(newDiv);
}
function removeElement(divNum)
{
var d = document.getElementById('org_div1');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
</script>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
<table>
<tr>
<td>
<input name="proba" type="text" id="proba" value="" />
</td>
</tr>
<tr>
<td>
<div class="file_input_wrapper">
<input type="hidden" value="1" id="theValue" />
<div id='org_div1'>
<input type="file" class="fileupload" name="file1" size="80" onclick="addElement()" />
</div>
</div>
</td>
</tr>
<tr>
<td>
<input name="btnProsledi" type="submit" id="btnProsledi" value="Submit" />
</td>
</tr>
</table>
</form>
</body>
</html>