dongwei4103
dongwei4103
2018-05-16 07:05

如何使用AJAX将代码发送到数据库

  • database
  • php
  • ajax
  • javascript
  • sql

I tried to save a HTML and also Javascript codes to Database by Ajax, but don't know how to optimize this way. I can't just write all codes like Strings for the variable. Do you have another idea to make it simpler ?

<script type="text/javascript">
  $(document).ready(function(){
    $("#add_code").click(function(){
        var addCode = "Here is Javascript code";
        $.get("includes/dashboard/addcode.php", {addCode:addCode
        },function(data){
        });
    });
  });
</script>

And this is the addcode.php

<?php
 require('database/connect_database.php');
  session_start();
   $addCode = $_GET['addCode']; 
   $codename = $_SESSION['codename'];
   $stmt = $pdo->prepare("INSERT INTO code (codename, w_code) VALUES ('$codename','$addCode')");
   $stmt->execute();
?>
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换