0

I need to upload an image and add it to my slideshow and give it related newsTitle in front of my uploaded picture. I'm a new in PHP and trying to learn how to send data from my admin.php file to my index.php file and add more image with a <form> in html.

My problem is that I can upload images but can't get my newsTitle printed to my home page which is index.php.

This is my PHP code in index.php:

<?php
    if (isset($_POST['send_object'])) {
     $file_name = $_FILES['image']['name'];
     $file_type = $_FILES['image']['type'];
     $file_tmp_name = $_FILES['image']['tmp_name'];
     //$newsTitle = $_POST['newsTitle'];
     $newsImage = $_POST['newsImage'];
     echo '<h2><?php echo 'htmlspecialchars($_POST['newsImage']);'';
     echo'<h2'.'>'.htmlspecialchars($newsImage["newsImage"]).'</h2>';

     if (move_uploaded_file($file_tmp_name,"uploader/$file_name")) {

     }
     }
     $folder = "uploader/";
     if (is_dir($folder)) {
     if($handle = opendir($folder)) {
      while (($file = readdir($handle)) != false) {
       if ($file ==='.' || $file=== '..') continue;
        echo '<img class="slider mySlides" width="100" src="uploader/'.$file.'" alt="">';
     }
      closedir($handle);
     }
    }
    ?>

This is my html code in admin.php:

<form action="index.php" method="post" enctype="multipart/form-data">
        <br><br>
        <tr>
          <td>  NewsTitle: </td>
          <td> <input type="text" name="newsTitle" placeholder="newsTitle"> </td>
        </tr>
        <br><br>
        Select image to upload:
        <input type="file" name="image">
        <br><br>
        <br><br>
        NewsText: <textarea name="newsImage" placeholder="newsImage" rows="5" cols="40"></textarea>
        <br><br>
        <input type="submit" value="Send" name="send_object">
    </form>

I'm trying to do this without connection to the database, just to my apache server. I have tried with another global variable $_REQUEST but it didn't work. What I know it can use for $_POST , $_GET and $_COOKIES

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • I can't see you echoing newsTitle in your index.php. – Matz Oct 07 '16 at 23:22
  • *"Im trying to do this without connection to the database"* - What does that have to do with uploading files? – Funk Forty Niner Oct 07 '16 at 23:27
  • 1
    `echo '

    '.htmlspecialchars($newsImage["newsImage"]).'

    ';` that alone should be throwing you a parse error http://php.net/manual/en/function.error-reporting.php and the opening ` echo'

    ` is incorrect.

    – Funk Forty Niner Oct 07 '16 at 23:29
  • `//$newsTitle = $_POST['newsTitle'];` so why is that commented out and where are you trying to use/echo `$newsTitle`? – Funk Forty Niner Oct 07 '16 at 23:31
  • possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Funk Forty Niner Oct 07 '16 at 23:32
  • I know that it give me error. The point is that to relate the text to

    wich a have class for and then have propertys how it should look

    –  Oct 09 '16 at 15:37

1 Answers1

0

Firstly, if you are trying to make each news with a text you collect it separately with the $_POST , but note once you refresh the page the parameters are gone cause the form processes everything so there is no space for output in text but if you use the get the parameters remain because you are not storing both the post method and get method in the database. Try this

 <?php
    if (isset($_POST['send_object'])) {
     $file_name = $_FILES['image']['name'];
     $file_type = $_FILES['image']['type'];
     $file_tmp_name = $_FILES['image']['tmp_name'];
     //$newsTitle = $_POST['newsTitle'];

     if (move_uploaded_file($file_tmp_name,"uploader/$file_name")) {

     }
     }
     $folder = "uploader/";
     if (is_dir($folder)) {
     if($handle = opendir($folder)) {
      while (($file = readdir($handle)) != false) {
       if ($file ==='.' || $file=== '..') continue;
        echo '<img class="slider mySlides" width="100" src="uploader/'.$file.'" alt="">';
     }
      closedir($handle);
     }
    }
    ?>

<?php
          $newsImage = $_POST['newsImage'];
    //this would give a parse error    echo '<h2><?php echo 'htmlspecialchars($_POST['newsImage']);'';

try
          echo <?php echo $newsimage; ?> 

    ?>
chiefo
  • 281
  • 1
  • 5
  • 15