user2413763
user2413763

Reputation: 81

issue when insert text to db using js

i try to submit text to db, but in the db field not inserted any text.

can you analyze where my false?

i try like this.

Thanks for your help.

<script type="text/javascript">
$(document).ready(function() {
    $('#myForm').submit(function() {
        var namaklasifier = document.getElementByName("tfklasifier").value;
        var inputpos= document.getElementByName("tacaripos").value;
        var inputnet = document.getElementByName("tacarinet").value;
        var inputneg = document.getElementByName("tacarineg").value;    
   $.ajax({
        type: 'POST',
        url:  $(this).attr('action'),
        data: $(this).serialize(),
        success: function(data) {
        $('#resultcoba').html(data);}
    })
        return false;
  });
    })
</script>    
<form id="myForm" method="post" action="proses.php">
<td align="center" valign="top" style="width:420px"><textarea style="width:420px" name="tacaripos" rows="4" cols="70"><?PHP echo htmlspecialchars($_POST['tacaripos']);?></textarea></td>
<td align="center" valign="top" style="width:420px"><textarea style="width:420px" name="tacarinet" rows="4" cols="70"><?PHP echo htmlspecialchars($_POST['tacarinet']);?></textarea></td>
<td align="center" valign="top" style="width:420px"><textarea style="width:420px" name="tacarineg" rows="4" cols="70"><?PHP echo htmlspecialchars($_POST['tacarineg']);?></textarea></td>
</tr>
<td align="center"><input type="submit" name="btklasifier" id="btklasifier" value="Buat" /></td></form>

proses.php

    <?php    
       $namaklasifier=$_POST['namaklasifier'];
$jadipos=$_POST['inputpos'];
$jadinet=$_POST['inputnet'];
$jadineg=$_POST['inputneg'];

$link=mysqli_connect("localhost", "root", "","skripsi");
mysqli_select_db($link,"skripsi");
mysqli_query($link,"INSERT INTO namaklasifier(username,datapos,datanet,dataneg,user) VALUES('$namaklasifier','$jadipos','$jadinet','$jadineg','{$_SESSION['username']}')"); ?>

Thanks :)

Upvotes: 0

Views: 61

Answers (3)

GautamD31
GautamD31

Reputation: 28763

They are POST values not the GET ones

$jadipos=$_POST['inputpos'];
$jadinet=$_POST['inputnet'];
$jadineg=$_POST['inputneg'];

And try to use mysqli_* functions or PDO statements,instead of mysql_* functions due to they are deprecated

EDIT:also try like

var namaklasifier = document.getElementByName("tfklasifier").value;
    var inputpos= document.getElementByName("tacaripos").value;
    var inputnet = document.getElementByName("tacarinet").value;
    var inputneg = document.getElementByName("tacarineg").value;    
 $.ajax({
      type: 'POST',
      url:  $(this).attr('action'),
      data: {inputpos:inputpos,inputnet:inputnet,inputneg:inputneg},
      success: function(data) {
         $('#resultcoba').html(data);}
     })
    return false;
 });

Upvotes: 1

Insyte
Insyte

Reputation: 2236

You are trying to access the GET data under the variable names you have declared here:

var namaklasifier = document.getElementByName("tfklasifier").value;
var inputpos= document.getElementByName("tacaripos").value;
var inputnet = document.getElementByName("tacarinet").value;
var inputneg = document.getElementByName("tacarineg").value;

Yet you are sending them using $(this).serialize() which means you must access the GET variables using the input name as this is what serialize does (creates an object from the form elements using their names). e.g.

$jadipos=$_POST['tacaripos'];
$jadinet=$_POST['tacarinet'];
$jadineg=$_POST['tacarineg'];

Also you need to change $_GET to $_POST as that is the method you are using.

Upvotes: 2

Jaime Gris
Jaime Gris

Reputation: 1126

In proses.php, change the $_GET into $_POST.

Since you are using POST method to submit data, naturally, you have to use the $_POST variables.

Upvotes: 1

Related Questions