0

I have a form (#searchform) that I want to submit multiple times without reloading the page, so I use jQuery and the post-method. I want to store which checkboxes are checked in a variable named target (array, list or whatever) and which radio button is selected in the variable diff.

The variables also need to be available in PHP. I tried passing them as ftar and fdiff in so many ways, but nothing worked.

Any help is much appreciated!

JQuery in submit.js:

$(document).ready(function () {
    var target = new Array();
    $('#searchform input:checked').each(function() {
        target.push($(this).attr('value'));
    });

    var diff = $('#searchform').find("input[class='diff']").val();

    $.post('index.php', {ftar: target, fdiff: diff});
});

HTML and PHP in index.php:

<!DOCTYPE html>
<html lang="en-US">
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script src="js/submit.js"></script>
</head>

<body>

<?php 

    ini_set('display_errors',1); 
    error_reporting(E_ALL);
    $link = new mysqli("localhost", "root", "password", "mytable");

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $target = $_POST['ftar'];
    $diff = $_POST['fdiff'];
?>

<div id="right">
    <div class="content">
        <form id="searchform" method="POST" action="/">
            <input type="checkbox" value="Check1">
            <input type="checkbox" value="Check2">
            <input type="checkbox" value="Check3">

            <input type="radio" class="diff" value="Radio1">
            <input type="radio" class="diff" value="Radio2">
        </form>
    </div>
</div>

</body>
</html>
2
  • You aren't doing anything with the $target and $diff variables. HOw are you checking that there is an issue? Commented Jun 14, 2013 at 17:00
  • try to build a valid HTML first and validate it. Then you can go to the javascript part Commented Jun 18, 2013 at 12:34

3 Answers 3

1

You should use

JSON.stringify

$.post('index.php', JSON.stringify({ftar: target, fdiff: diff}));
Sign up to request clarification or add additional context in comments.

Comments

1

Did you tried to use $(#searchform).serialize();?

$.post('index.php', $(#searchform).serialize());

You should also add a name to your fields in the form

<form id="searchform" method="POST" action="/">
        <input name="check[]" type="checkbox" value="Check1">
        <input name="check[]" type="checkbox" value="Check2">
        <input name="check[]" type="checkbox" value="Check3">

        <input name="radio[]" type="radio" class="diff" value="Radio1">
        <input name="radio[]" type="radio" class="diff" value="Radio2">
    </form>

Fetch field as an array (only marked fields will be passed)

    $target = $_POST['check'];
print_r($target)
$target = $_POST['radio'];
print_r($target)`

1 Comment

I did try serialize, but find it unneccessarily complicated to retrieve informations about which checkboxes are checked from the string that is returned. Didn't solve the post problem either.
0

You should add name="ftar" and name="fdiff" to according html inputs. Then serialize, JSON.stringify() or just submit.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.