PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Oct 19, 2017 4:10 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Upload picture from form
PostPosted: Fri Mar 17, 2017 6:19 am 
Offline
Forum Newbie

Joined: Fri Mar 17, 2017 6:10 am
Posts: 1
Hi

I'm not a hardcore developer, but can get som stuff to work :D
I have a wordpress installation where I have made a custom template on a page. It works except from the picture upload. I need help to implemente this as I really don't get it.

1. It needs to upload the picture to a specifik hardcodet foldername
2. I't needs to control the name (if more with same name is uploaded)
3. It needs to save the path to the image in a variable so I can store that in my database

I know there is a function i wordpress called wp_handle_upload but I cannot wrap my head around how to do this.

This is my working code without the picture part working

Syntax: [ Download ] [ Hide ]
<?php /* Template Name: Storfanger_indberet */ ?>
<?php
if ( ! defined( 'ABSPATH' ) ) exit;

get_header(); ?>

    <div id="primary" <?php generate_content_class();?>>
        <main id="main" <?php generate_main_class(); ?>>
            <?php do_action('generate_before_main_content'); ?>
           
                      <?php
                        if(!is_user_logged_in())$getOut=true;//Check if visitor is even logged in
            elseif(current_user_can('editor') || current_user_can('administrator')){//Is user administrator or editor?
            $getOut=false;//User can stay and view the content (even process scripts)
            }
            if($getOut){//Should the user be kicked out?
            echo"Du skal være logget ind for at se denne side";
            }else while ( have_posts() ) : the_post(); ?>


            <?php get_template_part( 'content', 'page' ); ?>

                <!--My code begin -->

                <?php
    If($_POST['Submit'])
    {
        // run validation if you're not doing it in js
        global $wpdb;

        $medlemsnr=$_POST['medlemsnr'];
        $navn=$_POST['navn'];
        $baadnavn=$_POST['baadnavn'];
        $art=$_POST['art'];
        $vaegt=$_POST['vaegt'];
        $billede=$_POST['billede'];

        if($wpdb->insert(
                            'ct_storfanger_indberetninger',
                            array(
                            'Medlemsnr' => $medlemsnr,
                            'navn' => $navn,
                            'bådnavn' =>$baadnavn,
                            'art' =>$art,
                            'vægt' =>$vaegt,
                            'billede' =>$billede
                            )
                        ) == false) wp_die('Der var en fejl i indsendelsen. Kontakt venligst webmaster'); else echo 'Tak for din indsendelse. Den vil fremgå af listen så snart den er godkendt af juryen!<p />';

?>

                    <!-- <a href="" onClick="return false;" id="form">Indberet endnu en fangst.</a> -->

<?php
    }
    else // else we didn't submit the form, so display the form
        {
?>
               
                <form action="" method="post" id="form">
                <table style="border:none;">
                <tr>
                <td style="border:none;width:25%">Medlemsnr</td>
                <td style="border:none;"><input name="medlemsnr" type="text" value="" /></td>
                </tr>
                <tr>
                <td style="border:none;">Navn</td>
                <td style="border:none;"><input name="navn" type="text" value="" /></td>
                </tr>
                <tr>
                <td style="border:none;">Bådnavn</td>
                <td style="border:none;"><input name="baadnavn" type="text" value="" /></td>
                </tr>
                <tr>
                <td style="border:none;">Art</td>
                <td style="border:none;">
                <?php
                    global $wpdb;
                    $retrieve_data = $wpdb->get_results( "SELECT Art FROM ct_storfanger_arter");
                ?>
                <SELECT NAME="art"><option value=""></option>
                <?php
                    foreach ($retrieve_data as $retrieved_data){
                        echo "<option value='" . $retrieved_data->Art . "'>" . $retrieved_data->Art . "</option>";
                    }
                ?>
                </SELECT></td>
                </tr>
                <tr>
                <td style="border:none;">Vægt i gram</td>
                <td style="border:none;"><input name="vaegt" type="text" value="" /> gram</td>
                </tr>
                <tr>
                <td style="border:none;">Billede</td>
                <td style="border:none;"><input name="billede" type="file" value="" /></td>
                </tr>
                </table>
                <table style="border:none;">
                <tr>
                <td style="border:none;"><input type="submit" name="Submit" id="formsubmit" value="Indsend" /></form></td>
                </tr>
                </table>
               
                </form>
               
           
<?php
        } // end else no post['submit']
?>               
               
                <!--My code ends -->
               
                <?php
                // If comments are open or we have at least one comment, load up the comment template
                if ( comments_open() || '0' != get_comments_number() ) : ?>
                    <div class="comments-area">
                        <?php comments_template(); ?>
                    </div>
                <?php endif; ?>

            <?php endwhile; // end of the loop. ?>
            <?php do_action('generate_after_main_content'); ?>
        </main><!-- #main -->
    </div><!-- #primary -->

<?php
do_action('generate_sidebars');
get_footer();
?>


Top
 Profile  
 
PostPosted: Tue Mar 21, 2017 10:37 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13434
Location: New York, NY, US
https://codex.wordpress.org/Function_Re ... dle_upload

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group