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).

3
  • you need to use preventDefault to avoid page refresh. check this stackoverflow.com/questions/20352799/…
    – bansi
    Commented Aug 20, 2014 at 10:22
  • It's okay the page is not loading. I just can't get the value...
    – Simon
    Commented Aug 20, 2014 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
    Commented Aug 21, 2014 at 10:13

1 Answer 1

1

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

5
  • Thank you but the result is the same...
    – Simon
    Commented Aug 20, 2014 at 10:56
  • what is it returning?
    – Mohit
    Commented Aug 20, 2014 at 12:04
  • "Poor" from the "else" statement. If I remove this statement then nothing happens...
    – Simon
    Commented Aug 20, 2014 at 12:39
  • name: cname should be radios: cname
    – bansi
    Commented Aug 21, 2014 at 11:20
  • Oh my god... thank you so much! I can't believe it was something so obvious...
    – Simon
    Commented Aug 22, 2014 at 7:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.