【问题标题】:How to store a passed variable into database when 'Add' button is clicked?单击“添加”按钮时如何将传递的变量存储到数据库中?
【发布时间】:2018-11-08 06:53:45
【问题描述】:

我最近才开始编码,因此我们将不胜感激。我正在练习一个在线读书俱乐部,并使用谷歌图书为每本书获取一个 ISBN 代码。

<?php 
$ISBN = $_GET['isbn'];
?>

通过从前一页传递的 ISBN 代码显示书籍及其信息。

<script>
//script to find the book details for the ISBN passed
function findBook(postedISBN)
{ 
var findISBN = postedISBN;



//if ISBN not passed, display error
if(findISBN == null)
{
    alert("No ISBN!");
}
//else if ISBN passed correctly, find its details using the Google Books API and display them
else
{   
    //JQuery call to Google Books API passing in the ISBN
    $.get("https://www.googleapis.com/books/v1/volumes?q=isbn:" + findISBN,function(response)
    {
        for(i=0;i<response.items.length;i++)
        {
            //get values needed and store in variables - this is just a small selection of all available - you should modify this to get the details you want to display on the page
            var title=response.items[i].volumeInfo.title;  
            var subtitle=response.items[i].volumeInfo.subtitle;  
            var publishedDate=response.items[i].volumeInfo.publishedDate;  
            var description=response.items[i].volumeInfo.description;
            var averageRating=response.items[i].volumeInfo.averageRating;
            var imageLinks= response.items[i].volumeInfo.imageLinks;
            var infoLink = response.items[i].volumeInfo.infoLink

            //set values of HTML fields to the values found in API
            document.getElementById("isbn").innerHTML = findISBN;
            document.getElementById("title").innerHTML = title;
            document.getElementById("subtitle").innerHTML = subtitle;
            document.getElementById("publishedDate").innerHTML = publishedDate;
            document.getElementById("description").innerHTML = description;
            document.getElementById("averageRating").innerHTML= averageRating;
            document.getElementById("infoLink").innerHTML= infoLink;




        }
    }); 
}//else 
}//findBook

</script>

我现在想允许用户在点击“添加到我想阅读”按钮时通过 ISBN 代码将图书存储到 MySQL 数据库中。

数据库表名为bookswantstoread; bookwantstoreadID,用户ID,ISBN。 bookdetails.php 页面的代码

  <!-- call the findBook function when the page loads -->
    <script> window.onload=findBook(<?php echo $ISBN;?>); </script>


<!--HTML elements in which book values go -->

    <div style="font-family: arial" class="container">  

<p id="label1"><b>ISBN: </b></p>
<p id="isbn"></p>
<p id="label2"><b>Title: </b></p>
<p id="title"></p>
<p id="label3"><b>Subtitle: </b></p>
<p id="subtitle"></p>
<p id="label4"><b>Published Date: </b></p>
<p id="publishedDate"></p>
<p id="label5"><b>Description: </b></p>
<p id="description"></p>
<p id="label6"><b>Average Rating</b></p>
<p id="averageRating"></p>


<a class="btn btn-primary btn-xl text-uppercase js-scroll-trigger" href="wanttoread.php">Add to 'Want to Read'</a>



</body>

</html>

我的问题基本上是当用户单击“添加到“想要阅读”按钮时,我如何将传递的变量 (ISBN) 存储到数据库表 (bookswantstoread) 中。

【问题讨论】:

  • 您需要阅读数据库/表格。查看mysqliPDO
  • 视情况而定。如果您提交表单,您可以获得传递的值并更新/插入到您的表中。如果您希望用户留在页面上,您将使用 AJAX 将值发送到服务器上的脚本。在您进行更多研究之前,还有很多事情要传达。谷歌 mysqli 函数和 AJAX。我建议也看看 jQuery

标签: php html mysql database


【解决方案1】:

请在您的代码中进行此编辑。 (我添加了一个类并更新了href)

<a class="btn btn-primary btn-xl text-uppercase js-scroll-trigger ajax-link" href="wanttoread.php?id=<?php echo $ISBN;?>">Add to 'Want to Read'</a>



</body>

</html>

然后在 wanttoread.php 中,编写插入查询。假设你有 jquery,添加这个脚本。

$( '.ajax-link' ).on( 'click', function( e) {
  e.preventDefault();
  $.ajax({url: $(this).href, success: function(result){
            $(this).html("Added to List!");
        }});
});

【讨论】:

  • 我玩jquery已经有一段时间了...所以如果有任何错误,请在这里发布,我会解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多