1

I want to give a name to my dynamically created textbox on a specific event. I have written the following code where the function GenerateTextBox returns the name of the textbox and the value "". The textbox is generated by but the name does not get assigned.

This is to use the name as a reference to the textbox in another php file.

Jquery code for generating textbox:

function GenerateTextbox(value,name1) { 
    return '<input name = "'+ name1 + '" type="text" value = "' + value + '" /> ';
}  

Calling the function:

$("#t11, #t12").click(function(){
    var div = $("<div />"); 
    div.html(GenerateTextbox("", c1));  
    $("#TextBoxContainer").append(div);
});

The php output file is showing the error that c1 is an undefined index...

How do I solve this problem?

4
  • What is c1 in your code? Commented Aug 24, 2018 at 15:18
  • 3
    maybe try "c1" Commented Aug 24, 2018 at 15:23
  • c1 is a global variable? Commented Aug 24, 2018 at 15:31
  • just put c1 in quotes: "c1" Commented Aug 24, 2018 at 15:57

1 Answer 1

1

Change c1 to "c1". c1 refers to a variable named c1 (which you have not defined) whereas "c1" refers to a String.

div.html(GenerateTextbox("", "c1"));  

Working Code:

function GenerateTextbox(value,name1) { 
return '<input name = "'+ name1 + '" type="text" value = "' + value + '" />';
}
$("#t11, #t12").click(function(){
var div = $("<div>"); 
div.html(GenerateTextbox("", "c1"));  
$("#TextBoxContainer").append(div);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="t11">Create Textbox</button>
<div id="TextBoxContainer"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

c1 is the name of the textbox now, but when I write the php POST command like this <?php echo $_POST["c1"]; ?> It says that the index is undefined. Is it because the textbox is not initially a part of my original page, and that is it dynamically created?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.