Jump to content

input looping


old_blueyes

Recommended Posts

Hi,

 

I am having a small issue with a front end html form, which through a series of looped input fields will update multiple WordPress posts at once, on submisson.

 

The issue i'm having currently, is on submission it's only updating 1 post. I have found a solution to my problem, here:

 

http://wordpress.stackexchange.com/questions/206372/how-to-handle-dynamic-form-data-with-repeating-fields

 

But as i am still a bit of a novice, I have tried to apply the max_id solution to my code below, so far i've not got it to work.

 

Using the solution on the above link, I don't suppose someone could show/tell me, what the code should be in it's entirety??

 

Including the foreach section, thanks!

foreach( $testArray as $value ) {

        $post_information = array(
        'post_title' =>  $value,
        'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
        'post_type' => 'predictions'  //'post',page' or use a custom post type if you want to
    );

    //update post
   wp_update_post($post_information);
Link to comment
Share on other sites

Forget about the StackExchange thread, it's nonsense.

 

PHP uses an array-like syntax to denote complex form values:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    var_dump($_POST);
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Complex form values</title>
    </head>
    <body>
        <form method="post">
            <input type="text" name="test[3]">
            <input type="text" name="test[5]">
            <input type="text" name="test[10]">
            <input type="submit">
        </form>
    </body>
</html>

When the above form is submitted, $_POST['test'] will be an array which maps the keys 3, 5 and 10 to the corresponding field values in the form.

 

This allows you to easily update any number of posts. Simply use the post ID as the key and then iterate over the array:

<?php

function html_escape($raw_input, $encoding)
{
    return htmlspecialchars($raw_input, ENT_QUOTES | ENT_COMPAT | ENT_SUBSTITUTE, $encoding);
}

// test data; load the real post IDs here
$post_ids = [12, 45, 69];

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    foreach ($_POST['post_title'] as $post_id => $post_title)
    {
        echo 'Post '.html_escape($post_id, 'UTF-8').' will get the title: '.html_escape($post_title, 'UTF-8').'<br>';
    }
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Complex form values</title>
    </head>
    <body>
        <form method="post">
            <?php foreach ($post_ids as $post_id): ?>
                <fieldset>
                    <legend>Post <?= html_escape($post_id, 'UTF-8') ?></legend>
                    <label>Title: <input type="text" name="post_title[<?= html_escape($post_id, 'UTF-8') ?>]"></label>
                </fieldset>
            <?php endforeach; ?>
            <input type="submit">
        </form>
    </body>
</html>
Edited by Jacques1
Link to comment
Share on other sites

Thanks very much Jacques!! I don't suppose you know how the wp_update_post() function would be implemented into your solution??

 

Thanks

 

 

Forget about the StackExchange thread, it's nonsense.

 

PHP uses an array-like syntax to denote complex form values:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    var_dump($_POST);
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Complex form values</title>
    </head>
    <body>
        <form method="post">
            <input type="text" name="test[3]">
            <input type="text" name="test[5]">
            <input type="text" name="test[10]">
            <input type="submit">
        </form>
    </body>
</html>

When the above form is submitted, $_POST['test'] will be an array which maps the keys 3, 5 and 10 to the corresponding field values in the form.

 

This allows you to easily update any number of posts. Simply use the post ID as the key and then iterate over the array:

<?php

function html_escape($raw_input, $encoding)
{
    return htmlspecialchars($raw_input, ENT_QUOTES | ENT_COMPAT | ENT_SUBSTITUTE, $encoding);
}

// test data; load the real post IDs here
$post_ids = [12, 45, 69];

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    foreach ($_POST['post_title'] as $post_id => $post_title)
    {
        echo 'Post '.html_escape($post_id, 'UTF-8').' will get the title: '.html_escape($post_title, 'UTF-8').'<br>';
    }
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Complex form values</title>
    </head>
    <body>
        <form method="post">
            <?php foreach ($post_ids as $post_id): ?>
                <fieldset>
                    <legend>Post <?= html_escape($post_id, 'UTF-8') ?></legend>
                    <label>Title: <input type="text" name="post_title[<?= html_escape($post_id, 'UTF-8') ?>]"></label>
                </fieldset>
            <?php endforeach; ?>
            <input type="submit">
        </form>
    </body>
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.