4

I want to make a form that sends a feedback (good/average/poor) in a mysql database without loading the page I'm on. It's working fine except that it doesn't recognize if wich value I'm sending (if it's good, average or poor that was sent).

Here my form :

<form method="post" id="radio_fb" style="margin-top:-90px;">
    <p>
        <input type="radio" name="radios" id="poor" value="poor" /><label for="poor">Poor</label>
        <input type="radio" name="radios" id="average" value="average" checked="checked" /><label for="average">Average</label>
        <input type="radio" name="radios" id="good" value="good" /><label for="good">Good</label>
        <input type="hidden" name="url" value="<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?> "/>
        <input type="submit" name="submitter" value="Send" id="submit" />
    </p>
</form>

And here is my jQuery :

jQuery(document).ready(function($) {
    $("#radio_fb").submit(function() {
        cname = this.radios.value;
        curl = this.url.value;
        submitter = this.submitter;

        var data = {
            name: cname,
            url: curl
        };
        $.post("ajax.php", data, function() {
            submitter.value="Sent";
            submitter.disabled=true;            
        });

        return false;

    });
});

And here are the functions that store the value in the database:

<?php
include_once('config.php');

if ($_POST['radios'] == 'good') {
    $url = $_POST['url'];
    $bdd->exec('INSERT INTO feedback(id, url, avis) VALUES ("", "'. $url .'", "Good")');
} else if ($_POST['radios'] == 'average') {
    $url = $_POST['url'];
    $bdd->exec('INSERT INTO feedback(id, url, avis) VALUES ("", "'. $url .'", "Average")');
} else {
    $url = $_POST['url'];
    $bdd->exec('INSERT INTO feedback(id, url, avis) VALUES ("", "'. $url .'", "Poor")');
}

?>

Any idea how to fix this? I looked through all the answers but nothing is working: my function keeps returning the else statement (or nothing if I remove it).

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Simon
  • 337
  • 1
  • 3
  • 11
  • you need to use [preventDefault](http://api.jquery.com/event.preventdefault/) to avoid page refresh. check this http://stackoverflow.com/questions/20352799/ajax-form-submit-with-preventdefault – bansi Aug 20 '14 at 10:22
  • It's okay the page is not loading. I just can't get the value... – Simon Aug 20 '14 at 10:56
  • I checked all the other answers and nothing is working so is it possible not to mark it as duplicate? It's important and I really need to solve this problem. Thank you! – Simon Aug 21 '14 at 10:13

1 Answers1

1

Try something like this : var cname = $('input:radio[name=radios]:checked').val();

Mohit
  • 65
  • 7