Hello guys, i've being searching for how is it possible to get value of a form element i.e selection tag that was injected by jquery be access on submitting the form.

All the form element value where posted except for the selection element that was injected with jquery. how can i append the element value to the posted query?

Recommended Answers

All 12 Replies

Please post your code.

If you're appending the element to the form it should be available on your POST without any further action. How are you adding the additional element?

<select name="sectionname" id="section-name"></select>');
$.each( data, function(key, val){
    $('#section-name').append('<option value="'+key+'">'+val+'</option>');
}

That's appending the options, what about the actual select field? Is this:

<select name="sectionname" id="section-name"></select>

already part of the form and you're just adding options or are you adding that to your form with jQuery as well, if so how?

through JQuery

$('#section-tr').append('<td >Section: </td><td class="right len"><select name="sectionname" id="section-name"></select></td>');
$.each( data, function(key, val){
$('#section-name').append('<option value="'+key+'">'+val+'</option>');
}

if that table row is between your <form> and </form> tags than it should be picked up automatically for the POST data. If that's not the case you could add a little fail safe:

$("#form-id").submit(function(){
  if ($('#section-name').length == 0) {
    //It was added, append the selected value to our form
    var the_value = $("#section-name").val();
    var new_field = "<input type='hidden' name='section' value='" + the_value + "' />";
    $("#form-id").append(new_field);
  }
});

It's not ideal or pretty but if you have that table row insdie the form tags and it's not adding it to your POST data it's a doable work-around. Just swap form-id with your actual form ID

thanks guys but i dont understand what you mean by "Just swap form-id with your actual form ID" GliderPilot.

But is it normal that what for what you append to html content not to be posted and access through script lang. (php) except through javascript? it will be of help if anybody can throw little light on this.

thanks guys but i dont understand what you mean by "Just swap form-id with your actual form ID" GliderPilot.

Change $('#form-id') to reflect the id attribute on your form. You must keep the # however.

If you post your complete code, so all of the form HTML, etc, people will be able to see exactly what your page looks like and why it might not be working.

Posting one or two lines of code isn't really helpful.

its like you are mis understanding my problem injecting code into html form and then submitting the form not through jquery but to php file that processes it is what am having problem about anything i appended to the dom with jquery doesn't come as part of the post ($_POST) data that was send to the php file. this is the problem not apppend another element to the form afterwhich dont comeup on php side. thanks but pls help

That's what I understood the problem to be too.

So if you post your complete code, we might be able to ascertain why it isn't working.

Yes that's what I understood your question to be, what I'm saying is that if the select is appended the the current form properly it should already be available in your POST data without needing to do any additional work, unless you're building your own custom AJAX call, there is no way to really modify the POST data . As blocblue stated we might be able to figure out what's going on if you post the full code.

Well thanks guys am able to resolve the issue but in different way. on the form i created the select tag without options and from jquery i appended the options to it and on submitting the form php was able to access the select post variable.

on html page

<tr id="section-tr" style=background-color:#FFFF99;>
    <td id="td1">Section: </td>
    <td id="td2" class="right len"> 
    <select name="section-name" id="section-name"></select></td>
</tr>;

then append the options with jquery

$.each( data, function(key, val){
 $('#section-name')
 .append('<option value="'+key+'">'+val+'</option>')
 });
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.