dsmvovm27249 2014-11-11 12:08
浏览 26
已采纳

通过提交按钮将PHP转换为JavaScript

I am working on an app for personal dev and have ran into some trouble. Fairly new to php and javascript so help is appreciated.

It's a simple form with an input and sumbit button. Once the user inputs an ISBN number and clicks search, a div should appear below showing a Google Books results containing title, author and description.

The way I am approaching this is to use the contents of var $isbn in my javascript. This could be the complete wrong way to do it, which is why I'm here. Basically I want to use the inputted ISBN number and 'attach' this to the end of the Google Books search (see below);

var url='https://www.googleapis.com/books/v1/volumes?q='[USER ISBN INPUT HERE];

If I manually set the var $isbn to '0276427343' - I do receive book results and see the div contents successfully. Just not when they are posted by the form to var $isbn.

I will show my code as is now;

HTML Form

<form name="form" method="post" action="">               
<input name="isbn_search" id="isbn_search" type="text">                
<button id="submit" name="submit">search</button>      
</form>

PHP

if(isset($_POST['isbn_search'])){ 
$isbn = $_POST['isbn_search'];
}

JaveScript

$(document).ready(function() {
var isbn = <?php echo $isbn; ?>;
var url='https://www.googleapis.com/books/v1/volumes?q='+isbn;
$('#submit').click(function() {
    $.getJSON(url,function(data){
        $('.result').empty();
        $.each(data.items, function(entryIndex, entry){
            var html = '<div class="results well">';                    
            //html += '<h3>' + entry.id + '</h3>';
            html += '<h3>' + entry.volumeInfo.title + '</h3>';                  
            html += '<div class="author">' + entry.volumeInfo.authors + '</div>'; 
            html += '<div class="description">' + entry.volumeInfo.description + '</div>';  
            $('.result').append(html);
        });                        
    });
    return false;
    });
});

Any help and/or suggestions are welcome.

  • 写回答

5条回答 默认 最新

  • donglu1971 2014-11-11 12:20
    关注

    I think in this case you don't need to use PHP.

    but simply try this :

    <div>               
       <input id="isbn_search" type="text">                
       <button onclick="do_search();" id="submit" name="submit">search</button>      
    </div>
    <div class="result"></div>
    
    
    <script>
    function dosearch(){
    
    var isbn = document.getElementById('isbn_search').value; //$('#isbn_search').val(); : if you like jquery :D
    var url='https://www.googleapis.com/books/v1/volumes?q='+isbn;
    
    $.getJSON(url,function(data){
            $('.result').empty();
            $.each(data.items, function(entryIndex, entry){
                var html = '<div class="results well">';                    
                //html += '<h3>' + entry.id + '</h3>';
                html += '<h3>' + entry.volumeInfo.title + '</h3>';                  
                html += '<div class="author">' + entry.volumeInfo.authors + '</div>'; 
                html += '<div class="description">' + entry.volumeInfo.description + '</div>';  
                $('.result').append(html);
            });  
    
            //here we send this query to database (thanks to AJAX):
            //=====================================================
            $.ajax({
                 type: 'POST',
                 url: './db_insert.php',
                 data: {'isbn' : isbn },
            });                      
        });
    
    }
    </script>
    

    if you want to save the search in a database,

    we create a php file : db_insert.php

    <?php
    
    // first : init access to data base :
    //====================================
    $user="root";   //user of database
    $pass="";       //password of database
    $db="test";     //name of database
    $host = "localhost";   //host name
    
    $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
    $bdd = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass, $pdo_options);
    $bdd->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $bdd->query("SET NAMES 'utf8'");
    
    
    
    //second : insert in a table named "all_search" in this case :
    ===============================================================
        $req = $bdd->prepare('INSERT INTO all_search(isbn, date_in) VALUES(:isbn, :date_in)');
        $req->execute(array(
            'isbn'      => $_POST['isbn'],
            'date_in'   => date('Y-m-d H:i:s')
        ));
    
    
    //emm... I think thats all, Enjoy :D
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办