Hello, I am using Jquery's ajax $.POST to insert a form into the database.I'm having 2 issues: For some reason the radio input on the gender won't insert into the database.The other input fields do work. And then II can't display the data after insertion into a div .
The Javascript:

<script type ="text/javascript">
function put(){
$.post('data2.php', { iname:form.iname.value, age:form.age.value, gender:form.gender.value },

function (putout) { 
('#idata').html(putout).show();
});}
</script>

The page data2.php

<?php
$iname=$_REQUEST['iname']; 
$age=$_REQUEST['age']; 
$gender=$_REQUEST['gender']; 

mysql_connect("localhost", "root" , "") or die(" Can't connect !");
mysql_select_db("test");
$insert = mysql_query ("INSERT INTO users (name , age , gender) VALUES ('$iname' , '$age' , '$gender')");
$dbname = mysql_query ("SELECT * FROM users WHERE '$iname' = name");

while($row = mysql_fetch_assoc($dbname))
{echo $row['name'];}

And the HTML :

Name <input type = "text" name = "iname"/></br>
Age <input type = "text" name = "age"/></br>
Gender <input type="radio" name="gender" value="male"/> : Male
       <input type="radio" name="gender" value="female"/> : Female <br/>
<input type = "button" value = "Insert" onclick = "put();"/>
<div align = "left" id = "idata">
</div>

Recommended Answers

All 3 Replies

Hi SMode55,
In your sample code:

<script type ="text/javascript">
function put(){
$.post('data2.php', { iname:form.iname.value, age:form.age.value, gender:form.gender.value },
function (putout) { 
('#idata').html(putout).show();
});}
</script>

You cannot get a radio button value in this way form.age.value as form.age would always return a NodeList and not the specific checked or selected element, and the way you're getting the value would always result to "undefined".

There are many ways of getting the checked value of a radio button, such as looping over the nodelist and checking for the element witht he checked value. Since you're using jQuery, one way to get the value is this $("input[name=gender][checked]").val(). Incorporating that to your sample code, you'll end up with this one:

<script type ="text/javascript">
function put(){
    $.post(
        'data2.php', 
        {
            iname:form.iname.value, 
            age:form.age.value, 
            gender: $("input[name=gender][checked]").val()
        },
        function (putout) { 
            ('#idata').html(putout).show();
        }
    );
}
</script>

Hope that helps..

Thanks gon1387, it's not working , I am still getting an empty result, plus there's still the issue where I can't display the result in the div.

I missed the missing dollar sign in your code, here's an updated one:

<script type ="text/javascript">
function put(){
    $.post(
        'data2.php', 
        {
            iname:form.iname.value, 
            age:form.age.value, 
            gender: $("input[name=gender][checked]").val()
        },
        function (putout) {
            //you missed the dollar sign here
            $('#idata').html(putout).show();
        }
    );
}
</script>

Can you please dump "$_POST" and check what values your post has. It would be better if you paste it here too, so we can assess where can the problem be.

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.