AJAX Data insertion on load

php jquery mysql ajax

53 观看

1回复

1 作者的声誉

I am trying to insert data into database using php but i don't know how to do this in fact i have search google as well but no proper solution.

My Ajax code is:

if(window.location == "path/to/file") {
    $(window).on('load',function() {
        var id = $("#id").val();
        var cd = $("#cd").val();
        var ed = $("#ed").val();
        var status = $("#status").val();

        if(ed == cd){
            status = "Stopped";
            console.log(status);
            $.ajax({
                type: "POST",
                url: "wba.php",
                data: "status=" + status,
                success: function(data) {
                   alert("sucess");
                }
            });
        }
        else{
            console.log('Running');
        }
    });
}

Now i don't know that how to get data from ajax and insert it into my db using php and my DB table structure is

INSERT into table_name(id, cd, ed, status) VALUES();

My WBA.php File Code is mentioned below in this file i am getting data from table first and then when the current time become equals to ending time i want this page to automatically change status of ad from running to stopped and update value in database without refreshing page and without even clicking on any button.

<div id="main-content">
<div class="casteform">
<?php
    $query = "SELECT * FROM advertisements WHERE ad_category = 'weddingbellsad'";
    $run = mysqli_query($con, $query);
    if(mysqli_num_rows($run) > 0){
        while ($row = mysqli_fetch_assoc($run)) {
            $ad_id = mysqli_real_escape_string($con, $row['ad_id']);
            $ad_company = mysqli_real_escape_string($con, $row['ad_company']);
            $ad_start_date = mysqli_real_escape_string($con, $row['ad_start_date']);
            $ad_duration = mysqli_real_escape_string($con, $row['ad_duration']);
            $ad_reminder_days = mysqli_real_escape_string($con, $row['ad_reminder_days']);
            $ad_reminder_date = mysqli_real_escape_string($con, $row['ad_reminder_date']);
            $ad_end_date = mysqli_real_escape_string($con, $row['ad_end_date']);
            $ad_payment_method = mysqli_real_escape_string($con, $row['ad_payment_method']);
            $ad_payment_amount = mysqli_real_escape_string($con, $row['ad_payment_amount']);
            $ad_category = mysqli_real_escape_string($con, $row['ad_category']);
            $ad_status = mysqli_real_escape_string($con, $row['ad_status']);
            $ad_current_date = date('Y-m-d h:i:s');
        ?>
            <div class="adrow">
                <h4>Company Name: <?php echo $ad_company . " ( " . $ad_id . " ) "; ?> <small><?php echo $ad_status ?></small></h4>
                <ul>
                    <li><span>Starting Date:</span> <?php echo $ad_start_date; ?></li>
                    <li><span>Duration:</span> <?php echo $ad_duration . " Days"; ?></li>
                    <li><span>Reminder:</span> <?php echo "Before ". $ad_reminder_days . " Day(s)"; ?></li>
                    <li><span>Reminder Date:</span> <?php echo $ad_reminder_date; ?></li>
                    <li><span>Ending Date:</span> <?php echo $ad_end_date; ?></li>
                    <li><span>Payment Method:</span> <?php echo $ad_payment_method; ?></li>
                    <li><span>Payment Amount:</span> <?php echo "Rs - " . $ad_payment_amount; ?></li>
                    <li><span>Ad Category:</span> <?php echo $ad_category; ?></li>
                </ul>
                <form>
                    <input type="text" id="id" value="<?php echo $ad_id; ?>" />
                    <input type="text" id="cd" value="<?php echo $ad_current_date; ?>" />
                    <input type="text" id="ed" value="<?php echo $ad_end_date; ?>" />
                    <input type="text" id="status" value="<?php echo $ad_status; ?>" />
                </form>
            </div>
        <?php
        }
    }
    else{
        header("location: view-wedding-bell-ad.php?error=".urlencode("No advertisements found so far."));
        exit();
    }
?>
</div>

作者: Syed Muddasir 的来源 发布者: 2017 年 12 月 27 日

回应 1


1

3510 作者的声誉

To pass multiple values through ajax, you'll need to adjust your .ajax call as such (note the data change):

$.ajax({
    type: "POST",
    url: "wba.php",
    data: {
        "id" : id,
        "cd" : cd,
        "ed" : ed,
        "status" : status
    },
    success: function(data) {
        alert("sucess");
    }
});

And then on PHP's side in your wba.php you get at those values with:

$_POST['id']
$_POST['cd']
$_POST['ed']
$_POST['status']

As for all the other database interaction and such, we would need you to make an attempt at that yourself, and figure out your goals and needed results. Its a bit broad and out of scope for StackOverflow for someone else to write all of that for you at this point. You can always ask another question to cover specific walls you run into if you need further help.

HOWEVER IMPORTANTLY: be sure you utilize "Prepared Statements" ! As that will help protect you from one of the most common attack vectors.

An example starter (http://php.net/manual/en/mysqli-stmt.bind-param.php):

$stmt = $mysqli->prepare("INSERT into table_name(id, cd, ed, status) VALUES(?, ?, ?, ?)");
$stmt->bind_param('ssss', $_POST['id'], $_POST['cd'], $_POST['ed'], $_POST['status']);
$stmt->execute();
作者: IncredibleHat 发布者: 2017 年 12 月 27 日
32x32