I have a site that appends a textbox to a div when the user clicks a button. The textbox works great. Now, I would like to generate a unique number for the id of each box. The following code is giving me 0 as the id of each box. I'm trying to get them to go 1,2,3,4...
Code:
<?php $x=0; ?>
$("#add").click(function(){
$('#boxes').append(<input type="text" id="<?php echo $x; $x++; ?>">);
});
PHP code is excuted earlier before the javascript code is executed. But in your code example you try to execute both at the same time. That is just not the case.
Instead of a PHP variable you need to use a javascript variable:
var x = 0;
$("#add").click(function() {
$('#boxes').append('<input type="text" id="' + ++x + '">');
});
You can use PHP variable as base for your javascript variable or just plain javascript.
//note, $x can be anything e.g. 1
var boxID = '<?php echo $x; ?>';
$("#add").click(function(){
//insert new box, increase boxID
$('#boxes').append('<input type="text" id="'+boxID+'">');
boxID++;
});
Related
I am new to PHP development. I wan to echo the selected value in a container and show it as and 'echo'. In the code <div id="selected_mters_container"></div> have the serial number. A jquery call is made and the selected meter serial number is displayed like shown below
Now i want to save this serial number in a variable. Below is the JQuery
jQuery('#the-mter-id').on('typeahead:selected', function (e, datum) {
$('#selected_mters_container').html('');
$('#metersinventorystore-store_id').val('');
$('#metersinventorystore-historic').val('');
var html = '<div class="selcted-meters"><input type="hidden" name="selected_meters[]" value="'+datum.id+'" />'+datum.meter_serial+'<a onclick="$(this).closest(\'.selcted-meters\').remove()">X</a></div>';
$('#selected_mters_container').append(html);
$('#metersinventorystore-store_id').val(datum.store_id);
$('#the-mter-id').typeahead('val','');
$('#metersinventorystore-historic').val(datum.historic);
});
As a newbie I don't know how to do it
Any help would be highly appreciated
Try :
var theVALUE = $('#selected_mters_container').html();
alert(theVALUE);
hi guys i need another idea how to make this happend i got a loop on my script that shows id for each member and i want to pass a variable for each member this is an example
<?php
$loopvalue = $playerCount;
$i=1;
while ($i <= $loopvalue) {
$uid = $data['players'][$i-1]['avatar']['userId'];
?>
<input class="user_id_imput" type="hidden" name="id" value="<?php echo $uid;?>" />
<?php echo $userName;?></span>
<?php
$i++;
};
?>
and using jquery to show the variable with this
$(".Send_user_id").click(function() {
var uid = $(".user_id_imput").val();
console.log('id: ' + uid );
});
normaly i do this when not using a loop bu in this case i cant think of a diferent way to do it
using this will give me the id of the first result for all the results any idea how i can pass the id var for each result?
thanks for you help
Assuming that your <a> tag and <input> tags are back to back; you can use following approach:
$(".Send_user_id").click(function() {
var uid = $(this).prev(".user_id_imput").val();
console.log('id: ' + uid );
});
$.prev() Get the immediately preceding sibling
thanks for the help you gave me an idea instead of using the input using data-id to pass the variable
so the result code will be
php
<a data-id="<?php echo $uid;?>" href="#" class="Send_user_id"><?php echo $userName;?></a></span>
jquery
$(".Send_user_id").click(function() {
var uid = $(this).attr('data-id');
console.log('id: ' + uid );
});
i have a form where two fields are dynamically generated through java script when a button is clicked.when the button is clicked each time,the two text field will generate again and again.now i have got the count of text field generated in a hidden field in JavaScript.How can i get the value of hiddenfield in controller and insert the values of text fields in database,by appending comma in data when the text box value is entered each time.please help me.
my javascript is
<script>
var countbox=0;
var textbox1=0;
var textbox2=0;
function getField()
{
var newtextbox1="name1"+countbox;
var newtextbox2="name2"+countbox;
document.getElementById('renderDiv').innerHTML+='<br/><input type="text" id="'+newtextbox1+'" name="'+newtextbox1+'" /><br/><input type="text" id="'+newtextbox2+'" name="'+newtextbox2+'" />';
document.getElementById('renderDiv').innerHTML+='<br/><input type="hidden" id="hiddentextField" name="hiddentextField" value="'+countbox+'" />';
countbox +=1;
}
</script>
my html code is
<input type="button" id="button1" onclick=getField();/>
<div id="renderDiv">
</div>
inside this div the two textfield is generated along with the hidden field
i am not getting the value of textfield in controller while submitting and i am not getting the count of textfield.i tried like $hiddenfield=$this->input->post('hiddentextField');
you will have to post the variable inorder to pass this to the server
<script>
var countbox=0;
var textbox1=0;
var textbox2=0;
function getField()
{
var newtextbox1="name1"+countbox;
var newtextbox2="name2"+countbox;
document.getElementById('renderDiv').innerHTML+='<br/><input type="text" id="'+newtextbox1+'" name="'+newtextbox1+'" /><br/><input type="text" id="'+newtextbox2+'" name="'+newtextbox2+'" />';
document.getElementById('renderDiv').innerHTML+='<br/><input type="hidden" id="hiddentextField" name="hiddentextField" value="'+countbox+'" />';
countbox +=1;
window.location.href = "index.php?name=" + countbox-1;
//change index.php to your page name
}
</script>
then in the same page
<?php
$hiddenfield=$_GET["name"];
?>
I had the same problem before, what I did is I insert those text box inside a table, lets say, tableSample
then I use jQuery find
var _content = '';
var _findcontent = $("#tableSample");
_findcontent.find("tr").each(function(){
$(this).find("td").each(function(){
$(this).find("input").each(function(){
_content += $(this).val+'~'+$(this).attr("id")+'|';
});
});
});
Then use ajax to pass it to your PHP
$.post("<?php echo site_url('controller_name/method_name'); ?>",
{
content : _content
}
,function( _data){
jAlert("alert",_data,"alert");
});
In your PHP, you can use explode to get the desired values,
$content_from_page = $this->input->post("content");
$explode_string = array();
$explode_string = explode("|",$content_from_page );
$explode_arr = array()
for($x=0;$x<count($explode_string)-1;$x++)
{
$explode_arr[] = explode("~",$explode_string[$x];
}
then print_r($explode_arr); to check
*Note: The only problem with this approach is that, if the character that is inserted into the textbox is ~ or |, coz that is the delimiter used.
I was wondering how it would be possible to attach an HTML form's text box value to a link. For instance, I have 3 links and each link goes to a different PHP file, however, I need to pass a variable. Because of the design of my page, I cannot use buttons. So, I created a form with a text field. How do I send that Text Fields value with the links?
EG:
<form>
<input type="text" name="test1" id="test1"></input></form>
Link 1
Link 2
Link 3
I hope this makes sense as to what I am trying to do.
EDIT:
I tried to do this dynamically, but:
OK, I made a function like this:
<script>
function awardcode()
{ var awamount = document.getElementById("awardamount");
document.write('<?php $awardvar = ' + awamount.value + '; ?>');
};
</script>
Then, I made the link via PHP that looks like this:
echo '<a id=awlink3 name=awlink3 href="index.php?siteid=gmaward&type=xp&post=' . $posts_row['posts_id'] . '&handle=' . $posts_row['handle'] . '&varamount=' . $awardvar . '">Award XP</a>';
However, that didn't work. This is my input box code:
<form><input type=text name='awardamount' id='awardamount' onchange='awardcode()' style:'width:10px;'></form>
When I put the number and then tab, it loads an empty page.
How can I adjust that?
You'll need to dynamically change the link using JavaScript. Here's one approach:
$('a').on('click',function(e) {
e.preventDefault();
var url = $(this).attr('href').split('$')[0];
window.location = url + $('#test1').val();
});
But it might be better to add an onchange event to the textbox itself, so that the HREF changes instantly and the user can see the intended destination on mouseover:
$('#test1').on('change', function () {
var val = $(this).val();
$('a[href*=\\?id\\=]').each(function (i, el) {
$(el).attr('href', function (j, str) {
return str.split('?')[0] + "?id=" + val;
});
});
});
How to add the value of php variabele to jquery variable and adds them,i have a form that will take a value and post to second page where second page contains a div panel where the form checkboxes values are adding and subtracting,basically i want to add the submitted value from page1 to page2.Here is the code.I have 3 form values which will be redirected one by one.Whenever user submits the respective button
if($_POST['submit'])
{
$beg=$_POST['basic'];
}
function refreshPrices() {
var currentTotalValue = 0;
var beg=?????
$("#results div").each(function() {
if (!isNaN(parseInt($(this).find("span").text().substring(1)))) {
currentTotalValue += parseInt($(this).find("span").text().substring(1));
}
});
$("#totalValue").text("$" + currentTotalValue)
}
var beg=<?php echo $beg; ?>
Try this:
var beg = '<?php echo $beg ;?>';
if you want to add to the total value you can do this:
currentTotalValue = currentTotalValue + parseInt(beg);
its better to do it this way :
var js_var = '<?=json_encode($php_var);?>';
This way you will be able to transfer any type of data to js (arrays, objects, strings, etc.) and access it easily like this:
js_var['element']
var beg = '<?php echo $beg ;?>';
But this can be only one once during the load of the page. After that we cant assign a server side script to js.
Retrieve value by input class or id like dat
var beg = $("#basic").val();