Jquery .onclick POST var使用ajax并从mysql接收数据

时间:2017-06-12 03:11:51

标签: javascript php jquery mysql


我试着这样做:
- 点击按钮获取 this.data-id 作为数据库选择中的id - 这些数据将以模态

动态显示

我不知道如何使用ajax,任何人都可以帮忙吗?

HTML

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var watch = require('gulp-watch');
var batch = require('gulp-batch');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var webserver = require('gulp-webserver');

var paths = {
  sass: ['src/scss/**'],
  js: ['src/ng/_declare.js', 'src/ng/**'], //_declare.js needs to be first, so it gets added to bundle first.
  static: ['src/js/**'],
  core: ['./td_frontend_shared/**']
};

gulp.task('default', ['watch-dev']);

gulp.task('publish-dev', function(done){

});

gulp.task('sass', function(done) {
  gulp.src('src/scss/import.scss')
    .pipe(sass())
    .on('error', function(err){
      console.log('error building sass', err);
      done();
    })
    .pipe(gulp.dest('public/dist/'))
    .pipe(minifyCss({
      keepSpecialComments: 0
    }))
    .pipe(rename({ extname: '.min.css' }))
    .pipe(gulp.dest('public/dist/'))
    .on('end', done);
});

.PHP

<a data-toggle="modal" data-id="'.$method['id'].'" title="Visualizar" class="itemview btn btn-info btn-mini" href="#MethodView">Visualizar</a>

的.js

    if(!empty($_POST)){
        if (isset($_POST['id']) && $_POST['id'] > 0){
            $id=$_POST['id'];
            GetPaymentMethodView();
        }
    }
    function GetPaymentMethodView() {
        global $db;
        try{
            $query = $db->query("SELECT * FROM payment_methods WHERE id=$id");
            $row=$query->fetch(PDO::FETCH_ASSOC); 
            $result['success'] = true;
            $result['result'] = $row;
            echo json_encode($result);
        return true;
        } catch (PDOException $pe) {
            return false;
        }
    }

2 个答案:

答案 0 :(得分:0)

在您要显示结果的页面中输入类似的内容

<div class="result"></div>

尝试小修改

try{
            $query = $db->query("SELECT id, name, bank_info FROM payment_methods WHERE id=$id");
            $row=$query->fetch(PDO::FETCH_ASSOC); 
            $result['success'] = true;
            $result['result'] = $row;
            echo json_encode($result);
            exit;
        return true;
        }

在js小编辑中

success: function (data) {
            if (data.success) {
                //How to show rows in php?
               console.log(data.result);
               console.log(data.result.id);

                var result = data.result;
                //prepare markup
                var resultHtml = '';
                resultHtml += '<p>Id = ' + result.id +'</p>';
                resultHtml += '<p>Name = ' + result.name +'</p>';
                resultHtml += '<p>Bankinfo = ' + result.bank_info  +'</p>';

                //Put markup in div
                $(".result").html(resultHtml)


            } else {
                alert("error");
            }
        }

在浏览器控制台中查看以了解。

答案 1 :(得分:-1)

由于两个代码都在同一页面中,因此您可以在没有js / jQuery的情况下执行此操作,因为只需将$method['id']的值直接添加到<input>

见下文了解

$method['id']代码

中删除<a>的值
<a data-toggle="modal" data-id="" title="Visualizar" class="itemview btn btn-info btn-mini" href="#MethodView">Visualizar</a>

将值直接添加到<input>代码

<input type="text" name="methodid" id="methodid" value = "<?php echo $method['id']; ?>" />

没有js就解决了问题。 :)所以删除js的东西。