ONLINE TELEKOM ONLINE TELEKOM - 1 month ago 8
HTML Question

PHP + Modal + FORM + MYSQL

I work on some code and i have problem, i try with google but nothing.

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="submit2" 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!

Answer

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.