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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应