0

I have a problem trying to work on some code

I have PHP form in MODAL.

PHP CODE:

<?php
// krerianje kataloga
if (isset($_POST['submit'])) {
$naslov = $_POST['naslov'];
$status = $_POST['status'];

if ($naslov != '' && $status != ''){
    $sql2= "INSERT INTO katalog(naslov,status) VALUES ('$naslov','$status')";
    $result2 = mysql_query($sql2,$baza);
    $poruka = '<center><hr style="color:green;width:98%"><h3 style="font-size:17px">Vaš katalog je kreiran.<center/></h3><hr style="color:green;width:98%"></center><br>';
}else {
    $poruka = '<center><hr style="color:green;width:98%"><h3 style="font-size:17px">Molimo Vas ispunite sva polja.<center/></h3><hr style="color:green;width:98%"></center><br>';
}
}

?>

And in SAME file I have modal:

<!-- Modal Structure -->
<div id="krerianjekataloga" class="modal">
    <div class="modal-content">

        <h4>Kreiranje Kataloga</h4>
        <form method="POST" name="katalog" action="index.php?stranica=katalog">
            <div class="input-field col s12">
                <label for="email" class="">Naslov Kataloga</label>
                <input id="naslov" name="naslov" class="required validate" aria-required="true" type="text">
            </div>
            <div class="input-field col s12">
                                        <div class="select-wrapper"><span class="caret">▼</span><input class="select-dropdown" readonly="true" data-activates="select-options-cde9ce7d-c5d2-8deb-ad2c-e8a5668916c4" value="Choose your option" style="" type="text"><ul id="select-options-cde9ce7d-c5d2-8deb-ad2c-e8a5668916c4" class="dropdown-content select-dropdown" style="width: 458px; position: absolute; top: 0px; left: 0px; opacity: 1; display: none;">
                                        <li class=""><span>Aktivno</span></li>
                                        <li class=""><span>Neaktivno</span></li></ul>
                                        <select id="status" name="status" class="initialized">
                                            <option value="1">Akivno</option>
                                            <option value="0">Neaktivno</option>
                                        </select></div>
                                        <label>Odaberite status kataloga</label>
                                    </div>

    </div>
    <div class="modal-footer">
       <button type="submit" style="float:right;" class="waves-effect waves-light btn m-b-xs"><i class="material-icons left">input</i>Spremi</button>
    </div>
    </form>
</div>

And that not work... why? I try with ajax and nothing work... Thanks!

2 Answers2

0

It can not work because of the the action that you have set.

Please change this action="index.php?stranica=katalog" to action="$_SERVER["PHP_SELF"]" to post to the same page.

Hope this will help.

muya.dev
  • 966
  • 1
  • 13
  • 34
0

Don't know what is type="submit2" here:

<button type="submit2" style="float:right;" class="waves-effect waves-light btn m-b-xs">
<i class="material-icons left">input</i>Spremi</button>

You can simply use <input type="submit"> field

<input type="submit" name="submit" value="Submit">

Because you are checking like:

if (isset($_POST['submit'])) { // here must need a field with name submit

Other Suggestions:

Stop using mysql_* its deprecated and closed in PHP 7, you can use mysqli_ or PDO

Or, if you still want to use <button> tag than you can check like if(count($_POST) > 0)

You also need to take of How can I prevent SQL injection in PHP? because you code is open for SQL Injection.

Community
  • 1
  • 1
devpro
  • 16,184
  • 3
  • 27
  • 38