-2

I'm having a problem with my code. the html form wont post the values with AJAX, when pressing the "indsend feedback" button. The jquery code works fine when only including a button with a value inside the form, but when i try to include multiple elements, it no longer works and starts posting an empty array.

The code works fine, when used standalone outside the project.

HTML CODE HERE FOR THE FORM and WHILE LOOP.

        <tbody>
        <?php
 $query = "SELECT * FROM psv2_orders_lines, psv2_orders WHERE psv2_orders_lines.order_id = '$orderid' AND psv2_orders.id = '$orderid'";
 $result = mysqli_query($dbCon, $query) or die(mysqli_error($dbCon));
 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
 ?>
        <tr>
            <td class="text-center">
                <!-- // Der laves en echo, hvor der udskrives ordrebilledernes destinationer.. -->
                <span class='zoom ex2'>
                    <img src="img/ordrer/<?php echo $orderid,"-", $rowcompany['company'], " ", $row['product'],"/", $row['style_1'],"1",".jpg";?>"
                        alt="..." class="img-fluid imgsize">
                </span>
                <span class='zoom ex2'>
                    <img src="img/ordrer/<?php echo $orderid,"-", $rowcompany['company'], " ", $row['product'],"/", $row['style_1'],"2",".jpg";?>"
                        alt="..." class="img-fluid imgsize">
                </span>
            </td>
            <td class="text-center"><?php echo $row['line'];?></td>
            <td>
                <ul class="list-group">
                    <li class="list-group-item">
                        <h5>Style 1</h5>
                        <?php echo $row['style_1'];?>
                    </li>
                    <li class="list-group-item">
                        <h5>Style 2</h5>
                        <?php echo $row['style_2']; ?>
                    </li>
                    <li class="list-group-item">
                        <h5>Style 3</h5>
                        <?php echo $row['style_3']; ?>
                    </li>
                    <li class="list-group-item">
                        <h5>Style 4</h5>
                        <?php echo $row['style_4']; ?>
                    </li>
                </ul>
            </td>
            <form method="post" action="feedback.php" class="feedbackknap">
                <td class="text-center">
                    <div class="container">
                        <div class="form-group">
                            <textarea class="form-control" rows="10" cols="50" placeholder="Skriv problemet her..."
                                name="orderrejectcomment"></textarea>                 
                        </div>
                    </div>
                </td>
                <td class="text-center">
                    <button type="submit" name="style1" value="hej" class="btn btn-dark">Indsend
                        feedback</button>
                </td>
                </form>
        </tr>
        <?php
 }
 ?>

    </tbody>

Here is the jQuery code:

$(document).ready(function() {
$('.feedbackknap').on('submit', function(){
    var that = $(this),
    url = that.attr('action'),
    type = that.attr('method'),
    data = {};

    that.find('[name]').each(function(index, value) {
        var that = $(this),
        name = that.attr('name'),
        value = that.val();
        data[name] = value; 
    });

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response) {
            console.log(response);
        }
    });

return false;
});});

PHP action folder:

<?php print_r($_POST); ?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
fachze
  • 1
  • 1
  • It is a very bad idea to use `die(mysqli_error($$conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Dec 11 '20 at 14:04
  • Thanks for the input guys, will try to fix it. – fachze Dec 11 '20 at 14:18
  • wouldnt tihs function prevent corrupt data? – fachze Dec 11 '20 at 14:18
  • No, that function would corrupt the data. Don't use such functions. – Dharman Dec 11 '20 at 14:20
  • So in theory i should delete all or die statements in my code? And find a way to secure my inputs, so that i dont recieve corrupt data? – fachze Dec 11 '20 at 14:26
  • Yes. Enable proper error reporting and use prepared statements. – Dharman Dec 11 '20 at 14:27

1 Answers1

0

Maybe you should use serializeArray() to serialize in array your entire form like this.

 $.ajax({
        url: url,
        type: type,
        data: $(this).serializeArray(),
        success: function(response) {
            console.log(response);
        }
    });