简体   繁体   English

使用Jquery将数据从javascript传递到php

[英]passing data from javascript to php using Jquery

Maybe this question has been asked before but I am struggling in doing this. 也许以前有人问过这个问题,但是我在努力做到这一点。 I have got a php file which does not include any piece of php code (might be in the future),it includes just javascript and some html. 我有一个不包含任何php代码的php文件(可能在将来),它仅包含javascript和一些html。 What I want to do is clicking a button in this php file to send some amount of data to another php file. 我想做的就是单击此php文件中的按钮,以将一些数据发送到另一个php文件。 put it this way.. 这样说..

1-I have got a saveProfile function in a.php and a button is calling this function 1-我在a.php中有一个saveProfile函数,一个按钮正在调用此函数

  function  saveProfile (){  
      var variableD = 'sample data';
      $.post("dbConn.php", { js: variableD});
  }

2-I have got another php which is called dbConn.php that receives data and stores in a database table. 2-我有另一个称为dbConn.php的php,它接收数据并存储在数据库表中。

I have found so many examples. 我发现了很多例子。 I have applied them but it still does not work and is driving me nuts. 我已经应用了它们,但是它仍然无法正常工作,并且让我发疯。 I am a java programmer but new in php. 我是一名Java程序员,但是刚接触php。 Any help is appreciated.give me some clean sample code or if you see any mistake please kindly warn me. 感谢您的帮助。请给我一些干净的示例代码,或者如果您发现任何错误,请警告我。 Thanks to all in advance... 预先感谢所有...

Regards. 问候。 Ozlem. 奥兹莱姆。

Take a look at the accepted answer to " Javascript Post Request like a Form Submit ". 看看对“ 像表单提交一样的Javascript请求 ”的可接受答案

It provides javascript for for: 它为以下对象提供了JavaScript:

function post_to_url(path, params, method) {
...
}

I think this will do what you want. 我认为这会做您想要的。

Thanks for all the answers. 感谢所有的答案。 I have solved the problem. 我已经解决了问题。 The data had being passes but I was not able to handle it properly. 数据已通过,但我无法正确处理。 I just add a dummy code to test it.It worked. 我只是添加了一个虚拟代码来对其进行测试。 I will upload the code after I have finished.Thanks to all. 完成后,我将上传代码。谢谢大家。

This function is in a PHP file, but is full of JS code. 此函数在PHP文件中,但包含JS代码。 The last line passes the data to another PHP file which saves the data into a database. 最后一行将数据传递到另一个PHP文件,该文件将数据保存到数据库中。

    function  saveProfile (){

      var _profileId    = 0;
      var _profileName  = document.getElementById('nameProfile').value;

        var queryArr=[];
       $(markersArray).each(function (index){
            //alert(markersArray[index].name);

            var _locationId = index;
            var _locName    = markersArray[index].name;
            var _markerLat  = markersArray[index].marker.getLatLng().lat();
            var _markerLng  = markersArray[index].marker.getLatLng().lng();

            var locations = {  
                                profileName: _profileName,
                                locationId:_locationId,
                                locationName:_locName,
                                lat:_markerLat,
                                lng:_markerLng  }

            queryStr = { "locations": locations} 

            queryArr.push(queryStr);


        });

        /*for ( var i=0; i<markersArray.length; i++){
            alert(queryArr[i].locations.locationId+"--"+queryArr[i].locations.locationName +"--"+queryArr[i].locations.lat);
        }*/

          $.post('dbConn.php', { opType:"saveAsProfile" , data: queryArr}, showResult, "text");

    }

This is dbConn.php, which is called by the saveProfile method. 这是dbConn.php,由saveProfile方法调用。 The data is handled as follows: 数据处理如下:

    $db_host = 'localhost';
    $db_user = 'root';
    $db_pass = '';
    $db_name = 'google_map_db';



    $opType = $_POST['opType'];

    //SAVE PROFILES WITH A PROFILE NAME
    if(!strcmp($opType, "saveAsProfile") ){

        $res = $_POST['data'];
        $connect = mysql_connect( $db_host, $db_user, $db_pass ) or die( mysql_error() );
        mysql_select_db( $db_name ) or die( mysql_error() );
        $queryString = "";

        for($i = 0; $i < sizeof($res); $i++){

            $profileName  = $res[$i]['locations']['profileName'];
            $locationId   = $res[$i]['locations']['locationId'];
            $locationName = $res[$i]['locations']['locationName'];
            $lat          = $res[$i]['locations']['lat'];
            $lng          = $res[$i]['locations']['lng'];

            $sp = " ";
            $queryString =  $queryString . "(0 ".",'".$profileName."',".$locationId.",'".$locationName."',".$lat.",".$lng.") ";

            if($i<sizeof($res)-1)
                $queryString =  $queryString . ", ";

        }


        $qInsertUser = mysql_query(" INSERT INTO `map_locations` (`profileId`, `profileName`, `locationId`, `locationName`, `lat`, `lng`)
                                     VALUES  ".$queryString." ");

        if ($qInsertUser){
            echo "successfully added!!!";
        } else {
            echo "Error";
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM