0

im using ajax to query my mysql to my database. But im stock at issue with my php generated html form input - javascript/jquery will simply not pick up the value. From normal html is no issue of course.

php (works fine, all echos are good)

<?php
function getAge() {
    $age = "<select name='age'>";
    $result = $mysqli->query("select * from ages");
    while ($row = $result->fetch_row()) {
        $age.="<option value=" . $row[0] . ">". $row[1] ."</option>";
    }
    $age.="</select>";

    return $age;
}
?>

html

<form id="myform">
   <input name='name' value='Nick'>
   <input name='sport' value='Football'>
   <?php echo getAge(); ?>
   <input type='submit'>
</form>

javascript

$("form#myform").on('submit', function(e){
    e.preventDefault();
    var json = {}
    $.each(this.elements, function(){
        json[this.name] = this.value || '';
    });
}

Everything works well except it wont get the value of the <select>. If i make a normal html select it works.. ?! Also anybody know how to delete the submit button from the json object? :-)

8
  • 2
    did you try to serialize the form? Have a look at Jquery Form Serialization Commented Apr 11, 2014 at 11:25
  • 1
    What's the generated HTML? Put in some console.debug(); and trace down the issue. Commented Apr 11, 2014 at 11:26
  • There is no difference between a normal html select and a php generated html select. Whatever the problem is, it is unlikely to have anything to do with the PHP. Commented Apr 11, 2014 at 11:27
  • Do you get proper value in $age Commented Apr 11, 2014 at 11:28
  • yes i do get proper value Commented Apr 11, 2014 at 11:32

2 Answers 2

1

Any dynamically generated HTML will not have the events applied to them, as those events are applied on page load. So if you apply the events to the document, you will be able to pull values from dynamically generated html. Like so:

var json = {};
$(document).on('submit', 'form#myform', function(e){
   $('*', this).each(function(){    
        json[$(this).attr('name')] = $(this).val();
   });
});

Hope this helps!

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

9 Comments

this makes sense! but the function wont fire now?
Had a typo, forgot a comma.
i did see that, still nothing :)
made a quick change, see if that gives you any output in the console.
OH i does! seems like i get the value!
|
1

change this line:

$age.="<option value=" . $row[0] . ">". $row[1] ."</option>";

to this:

$age.="<option value='" . $row[0] . "'>". $row[1] ."</option>";
   //----------------^---------------^------put quotes

And i think you can make use of .serializeArray() which does the same you want but in a different way like multiple objects with multiple [{ name : value}] pairs:

$(function(){  //<-----put this block too
   $("form#myform").on('submit', function(e){
      e.preventDefault();
      var json = $(this).serializeArray();
   });    //<----checkout the closing
}); //<---doc ready closed.

6 Comments

ill give that a shot! in debugger i do have quotes around my value i DOM.
.serializeArray() wont get the select value aswell
I'm not sure about, but var json = {} makes the variable local scope inside the function? Change it to json = {}.
@MacLuc as i see your question your jquery code is missing the closing of click event, see if that might be the issue and is it wrapped in doc ready block. Also lookout for any js errors in your browser's console.
its wrapped in doc ready and i did close it right in production code :)
|

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.