通过ajax发送php var

时间:2018-10-04 20:03:49

标签: php ajax post

为什么这样做:

<script>
$(document).ready(function(){
    $(".div1").click(function(){
        var table = "test";  
        $.post( "clicked.php", {tablename1:table},function( data ) {
              $( ".result" ).html( data );
        });
    });
});
</script>

但这不是吗?

<script>
$(document).ready(function(){
    $(".div1").click(function(){
        var table = <?php echo $tablename; ?>;  
        $.post( "clicked.php", {tablename1:table},function( data ) {
              $( ".result" ).html( data );
        });
    });
});
</script>

这不是通过ajax发送php var的方式吗?

2 个答案:

答案 0 :(得分:0)

JavaScript字符串需要用引号(“ string”)括起来。代码var table = <?php echo $tablename; ?>;将导致(假设表名等于foobar)-var table = foobar,这是一个不包含在引号中的字符串。我希望您能看到进展...

尝试以下方法作为解决方案:

var table = "<?php echo $tablename; ?>"; 

答案 1 :(得分:0)

如评论中所述,您在值周围缺少引号。

您应该使用import UIKit import Vision class ViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() // 1. Load Default Image guard let image = UIImage(named: "anuj.jpeg") else {return} imageView.image = image imageView.contentMode = .scaleAspectFit self.detectFaces(img: image) } @IBAction func modeSwitch(_ sender: UISwitch) { if sender.isOn{ print("Camera Mode ON !!") } else{ print("Camera Mode OFF !!") } } // ******************************************************************* // 2. Function to detect faces in Image and retrun Bounding Box Values // ******************************************************************* func detectFaces(img: UIImage){ // Get Image Scaled Height let imageScaledHeight = view.frame.width / (img.size.width) * (img.size.height) // Create Face Detection Request let request = VNDetectFaceRectanglesRequest { (req, err) in if let err = err{ print("Failed to detect faces !! \(err)") return } print("Request: \(req)") req.results?.forEach({ (res) in // Get face observations guard let faceObservation = res as? VNFaceObservation else {return} // Print detected face bounding box values print("Face Observations: \(faceObservation.boundingBox)") // Create Bounding Box let x = self.view.frame.width * faceObservation.boundingBox.origin.x let width = self.view.frame.width * faceObservation.boundingBox.width let height = imageScaledHeight * faceObservation.boundingBox.height let y = imageScaledHeight * (1 - faceObservation.boundingBox.origin.y) - height // Show bounding Box let redView = UIView() redView.backgroundColor = .red redView.frame = CGRect(x: x, y: y, width: width, height: height) redView.alpha = 0.4 self.view.addSubview(redView) // Crop Face in Red View i.e. in Bounding Box and show in Image View let crop = CGRect(x: x, y: y, width: width, height: height) let image = img.cgImage?.cropping(to: crop) let cropImageView = UIImageView(image: UIImage(cgImage: image!)) self.view.addSubview(cropImageView) }) } // Convert Image to cgImage and pass to request handler let cgImage = img.cgImage let handler = VNImageRequestHandler(cgImage: cgImage!, options: [:]) // Perform vision request do{ try handler.perform([request]) } catch let reqErr{ print("Failed to perform request: \(reqErr)") } } } 将PHP值转换为相应的JavaScript文字语法。

json_encode()

除了引用字符串,如果需要,它还将转义。此方法可用于大多数类型的值。