0

I have a page where there are two radio button on clicking one it shows one div and on clicking another radio button it shows another div. In both div i have to insert the data into the database but only fist submit button is working the second button on second div is not working.Below is my code:

<body>
     <center>
             <h2>Choose the type </h2>
              <div style="padding:25px;width: 100px;">
                   <input id="id_radio1" type="radio" name="name1" value="1" />Dynamic<br/>
                   <input id="id_radio2" type="radio" name="name0" value="0" />Image
              </div>
              <div align="center" style="padding:25px;width: 300px;">
                   <div id="div1">

                    <form name="frm" method="post" action="">
                        Caption:<input type="text" name="caption" /><br />
                        Dynamic:<input type="text" name="dynamictext" /><br/>
                        Time    <input type="text" name="time" /><br />
                        <input type="submit" name="submitbtn1" value="Submit" />
                    </form>


                   </div>
                   <div id="div2">
                    <form enctype="multipart/form-data" action="saveimage.php" method="post" name="changer">
                        Path:<input name="image" accept="image/jpeg" type="file" height="50px" width="50px"><br/>
                        Caption:<input type="text" name="caption" />
                        Time:<input type="text" name="caption" />
                        <input type="submit" name="submitbtn0" value="Save" />

                    </form>

                   </div>
              </div>
     </center>
</body>
</html>





<?php
if(isset($_POST['submitbtn1']))
{

        include'conn.php';
        $select_db = mysql_select_db('test',$connection);
        //$type1=$_POST['name1'];
        $sql="INSERT INTO tbl_content(caption,dynamictext,time)VALUES('".$_POST['caption']."', '".$_POST['dynamictext']."', '".$_POST['time']."')";

    if (!mysql_query($sql,$connection))
    {
        die('Error: ' . mysql_error());
    }


}
else if(isset($_POST['submitbtn0']))
{
        include'conn.php';
        $select_db = mysql_select_db('test',$connection);
        if ($_FILES["image"]["error"] > 0)
  {
     echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
     echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";
   }
   else
   {
     move_uploaded_file($_FILES["image"]["tmp_name"],"image/" . $_FILES["image"]["name"]);



     $file="image/".$_FILES["image"]["name"];
     $sql="INSERT INTO tbl_content (id,path,caption,time) VALUES ('','$file','".$_POST['caption']."','".$_POST['time']."')";

     if (!mysql_query($sql))
     {
        die('Error: ' . mysql_error());
     }
     echo "Picture is saved into database";

   }
}   
?>
user3132068
  • 35
  • 1
  • 7
  • This is more like HTML, JavaScript instead of PHP. Do you have fiddle? – Iqbal Fauzi May 29 '14 at 04:12
  • "second div is not working" so what happend ? any error ? whats the output of `var_dump($_POST)`? – Exlord May 29 '14 at 04:13
  • Is the code you posted `saveimage.php`? Why do the two forms have different actions? – Mark Miller May 29 '14 at 04:13
  • the first div insert the caption,dynamictext and time which when i submit the button it is inserted.Likewise in second div i have to upload photo,insert time and caption which is not working when i submit the button thats the problem. – user3132068 May 29 '14 at 04:17
  • 2
    An important side note: **DO NOT** use `mysql` functions anymore. They have been depreciated and are no longer standard. Use either `mysqli` functions or `PDO` functions. – David Corbin May 29 '14 at 04:17
  • and if you want to enable mysqli see my answer here: http://stackoverflow.com/questions/23918072/how-to-delete-multiple-rows-from-mysql-with-checkbox/23918668#23918668 – Janaka R Rajapaksha May 29 '14 at 04:21
  • Thank u for your suggestion....Can u plzz help me with second div its taking the default value of first submit button whenever i refresh the page. – user3132068 May 29 '14 at 04:21
  • you mentioned name="caption" for three input fields – Ezhil May 29 '14 at 04:56
  • You mention the radio buttons, however, those radio buttons are not mentioned in the PHP code, so we can ignore the associated commentary, except to say they are not actually radio buttons because they do not have the same name. – user3629249 May 31 '14 at 12:59
  • The first submit button (submitbtn1) has the action of re-displaying this same page. The second submit button (submitbtn0) has the action of changing to the saveimage.php page. I – user3629249 May 31 '14 at 13:01
  • as mentioned by @Mark, The first submit button (submitbtn1) has the action of re-displaying this same page. The second submit button (submitbtn0) has the action of changing to the saveimage.php page. Im assuming that the php code, you supplied, is part of this page and not part of the saveimage.php page. IF they are both the same page, then the second 'action' needs to be updated to "". – user3629249 May 31 '14 at 13:09

1 Answers1

0

I think your saveimage.php file seeking a value for $_POST['time'] but second form has no value for that. Because your second form has Time:<input type="text" name="caption" /> and there is another input for that name too. So change

Time:<input type="text" name="caption" />

to

Time:<input type="text" name="time" />

in your second form and you may success.

also sanitize your data before enter these data to database. I think best way is use prepared statements with mysqli.

asprin
  • 9,579
  • 12
  • 66
  • 119
Janaka R Rajapaksha
  • 3,585
  • 1
  • 25
  • 28