dsg56465 2015-03-10 18:48
浏览 45
已采纳

ajax和php:如何从数据库中选择变量并使用ajax插入数据库

I really have never done this before and I am getting frustrated because I'm not sure how it fits together. I have a function that I want to call my php (one php file selects info from a database and the second inserts into the database)... I need to use ajax in the way my site is setup but I don't know how to pass data from and to the php files.

In first .js file:

q1LoadVar();

This is my ajax function in second .js file that I have so far (not working):

//ajax code has been edited here since original post:

function q1LoadVar() {
alert("called"); //works!
$.get( "q1LoadVar1.php", function( data ) {

console.log(data); //nothing happens!
// alert(data); //nothing happens!

}, "json" );

}

And here is the code I have in q1LoadVar1.php that I want to select data back from and be able to populate a text area in my html:

/*works when I type this file path directly into the url; 
but the file is not communicating back to the ajax function on the 
.js file that is calling it*/ 

<?php   
$config = parse_ini_file('../config.ini'); 
$link = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);

if(mysqli_connect_errno()){  
echo mysqli_connect_error();  
}  

echo '<script type="text/javascript">alert("working from php!");</script>';

$query = "SELECT * FROM Game1_RollarCoaster";
$result = mysqli_query($link, $query);

while ($row = mysqli_fetch_array($result)) {
$newRow[] = $row;
}

$json = json_encode($newRow);
echo $json; //works on php file directly!

/*while ($row = mysqli_fetch_array($result)) {
echo $row[Q1_AnswerChoosen];
}*/

mysqli_free_result($result);
mysqli_close($link);

?> 

Can someone help me understand how to make this all work together? Thank you, Kristen

  • 写回答

1条回答 默认 最新

  • dqsw7529 2015-03-11 10:01
    关注

    You can retrieve post data from ajax in php with

    $_POST['action']
    //in your case will return: test
    

    To return data to ajax you need to use echo

    If the success: callback function doesnt get called try to remove datatype: 'json'

    I also think that you need to echo $newrow instead of $row.

    If this still doesnt work you can catch the error with the error: callback function to see what is wrong.

    Try to start with a simple request and work from there.

    $(document).ready(function() {
    
        $.ajax({
            type: "POST",
            url: "yourphp.php",
            data: {simplestring: "hi"},
            success: function(result){
                alert(result);
            }
        });
    });
    

    and yourphp.php

    <?php
    $simplestring = $_POST['simplestring'];
    echo $simplestring;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?