1

I have an HTML form that contains a number of checkboxes. When the user clicks the submit button the checked array is posted and a query runs based on that result. I have a button allowing the user to save their result, when they insert their saved result into another table. But this means I need to post the form result again which it doesn't allow me to do. How would I fix this?

11
  • You can use ajax for that. Commented May 3, 2017 at 14:48
  • how would I do that? Commented May 3, 2017 at 14:49
  • Can you separate the parts which you want to call in first submit and other one for better understanding Commented May 3, 2017 at 14:51
  • just did that there now Commented May 3, 2017 at 14:55
  • b.SportID and a.Tag = '$value'" Where is $value coming from? Commented May 3, 2017 at 14:59

1 Answer 1

1

PHP Code

<?php

if (isset($_POST['submit']) && isset($_POST['sport'])) {
    $class = $_POST["sport"];
    foreach ($class As $key => $value) {
        $query = "SELECT *
FROM sport b
join sport a
on a.Tag = b.Name
where a.SportID<> b.SportID and a.Tag = '$value'";

        $result = mysqli_query($con, $query) or die("Invalid Query");

        while ($row = mysqli_fetch_assoc($result)) {

            echo "* $row[Name]\n";
        }
    }
} else if (isset($_POST['saved'])) {


    $arr_class = $_POST["sport"];


    foreach ($arr_class As $key => $newvalue) {

        $query2 = "INSERT INTO save (Username, Name)
    SELECT '$username', b.Name
    FROM sport b 
    JOIN sport a 
    on a.Tag = b.Name
    where a.SportID <> b.SportID 
    and a.Name = '$newvalue'";

        $result2 = mysqli_query($con, $query2) or die('Result could not be saved!');


    }
    echo 'Result saved!';
}

JS Code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('.submit-button').click(function (ev) {
            var $item = $(this);
            var formData  = $('form').serialize();
            $.ajax({
                data: formData+'&submit=submit',
                url:'test1.php',
                type: "POST",
                dataType: "html",
                success:function (data) {
                    console.log(data);
                    if(confirm('You want to save \n'+data+ ' as your sport')){
                        $.ajax({
                            data: formData+'&saved=saved',
                            url:'test1.php',
                            type: "POST",
                            dataType: "html",
                            success:function (data) {
                                console.log(data);
                            }
                        });
                    }
                }
            });
        });
    });
</script>

HTML Code

<form id="form" action="" method="post">
    <input type="checkbox" name="sport[]" value="Football">Football<br>
    <input type="checkbox" name="sport[]" value="Rugby">Rugby<br>
    <input type="checkbox" name="sport[]" value="Golf">Golf<br>
    <input type="checkbox" name="sport[]" value="Basketball">Basketball<br>

    <br> <input type="button" class="submit-button btn btn-info" name="submit" value="submit">
    <input type="submit" style="display:none;">
</form>
Sign up to request clarification or add additional context in comments.

9 Comments

Have you changed the url from url:'test1.php', to your one
Im being told that Notice: Undefined index: sport
on what line number?
$arr_class = $_POST["sport"];
Have you selected any sport?
|

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.