WordPress.org

Ready to get started?Download WordPress

Forums

Save data without AJAX (1 post)

  1. mvanboordt
    Member
    Posted 1 year ago #

    Hi,

    I have a question about saving data to the database without AJAX.
    It currently does this with a form I got from the internet.

    Since I want to use a for loop I can't get it to save the data so I want to get rid of the AJAX part/

    I'm building an admin page that get's data with a for loop
    like this:

    $sunday = strtotime("next sunday");
    for ($i=1; $i<=12; $i++)
    {
    $output .= '<td>Output: <input type="text" id="output'.date('d-m-y',$sunday).'" value="'. get_option('output'.date('d-m-y',$sunday)) .'" /></td>';
    $sunday = strtotime("next sunday", $sunday);
    }
    $output = "";

    This generates 12 <input> fields with the ID: output followed by a date for each sunday.

    The problem is to save the data that is used in the form to the database.

    I normaly do this with the $update(); function from WordPress.

    $save_sunday = strtotime("next sunday");
    for ($i=1; $i<=12; $i++)
    {
    if(get_option('output'.date('d-m-y',$save_sunday)) == ""){update_option('output'.date('d-m-y',$save_sunday));}// Save data to option fields
    if(! empty($_POST)){
    $output = $_POST['output'.date('d-m-y',$save_sunday)];
    // save the form values to the option fields
    update_option('output'.date('d-m-y',$save_sunday), $output);
    $save_sunday = strtotime("next sunday", $save_sunday);
    }
    }

    Now the option part is where it get's stuck (this is in AJAX)

    <script type="text/javascript" >
     jQuery(document).ready(function() {
    jQuery('#social-button').click(function() {
    jQuery('#savemessageslider').html('Loading...');
    //this is the ajax for general settings
    var output = jQuery('#output').val();		
    
    var data = {
    action: 'slider_my_action',
    teetime: teetime,
    };
    
    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(ajaxurl, data, function(response) {
    jQuery('#savemessageslider').html('Saved!').hide(2000);
    });
    });
    });
    </script>

    The way I see it there are two possibilities:
    1. I need to find a way to get a loop with my data in the AJAX

    2. I need to get rid of the AJAX code and save with PHP

    Either way I'm not sure how to do this.

    Anyone who has an oppinion on this or an idea?

    M.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags