카테고리 없음

[javascript] javascript varible을 php 파일로 보내고 데이터베이스에 저장하는 방법 [닫힌]

행복을전해요 2021. 3. 1. 12:42

The data must be key value pairs, use:

$.ajax({
    url : 'insert.php',
        type : 'POST',
            data : {name: name}, // or data: "name="+name,
                success : function(response) {
                        console.log(response);
                            }
                            });
                            

And the php side:

$name = $_POST['name'];
-------------------

Consider using something like:

    var name = "test";

    $.ajax({
            url : 'insert.php',
                    type : 'POST',
                            data : {'name' : name},
                                    success : function(response) {
                                                alert(response);
                                                        }
                                                            }).done(function() {
                                                                alert( "success" );
                                                                  }).fail(function() {
                                                                      alert( "error" );
                                                                        }).always(function() {
                                                                            alert( "complete" );
                                                                              });
                                                                              

and then in php check for $_POST['name'];

-------------------
var name = "test";

$.ajax({
    url : 'insert.php',
        type : 'POST',
            data : name, // your javascript variable
                success : function(response) {
                
                        alert(response);
                            }
                            });
                            

그리고 insert.php

<?php
$myVar = $POST['name'] ;

db code here
if(success)
{ 
echo "success";
}
else {
 echo "error";
 }
 ?>
 
-------------------

이렇게하면 {및}를 사용하여 더 많은 값을 게시 할 수 있습니다.

$.ajax({
    url : 'insert.php',
        type : 'POST',
            data : {
                    name: name,
                            variable2: variable2
                                },
                                    success : function(response) {
                                            alert(response);
                                                }
                                                });
                                                

그리고 PHP에서 :

$name = $_POST['name'];
$variable2 = $_POST['variable2'];

그런 다음 변수로 원하는 것을 할 수 있습니다.



출처
https://stackoverflow.com/questions/22089814