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

发布于 2024-09-29 01:24:26 字数 519 浏览 3 评论 0原文

也许这个问题以前已经被问过,但我正在努力做到这一点。我有一个 php 文件,其中不包含任何 php 代码(可能在将来),它只包含 javascript 和一些 html。我想要做的是单击此 php 文件中的按钮将一些数据发送到另一个 php 文件。 这样说吧..

1-我在 a.php 中有一个 saveProfile 函数,并且有一个按钮正在调用该函数

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

2-我有另一个名为 dbConn.php 的 php,它接收数据并存储在数据库表中。

我已经找到很多例子了。我已经应用了它们,但它仍然不起作用,让我发疯。我是一名 java 程序员,但对 php 很陌生。 感谢任何帮助。给我一些干净的示例代码,或者如果您发现任何错误,请警告我。提前感谢大家...

问候。 奥兹莱姆。

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. What I want to do is clicking a button in this php file to send some amount of data to another php file.
put it this way..

1-I have got a saveProfile function in a.php and a button is calling this function

  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.

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.
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×纯※雪 2024-10-06 01:24:26

查看已接受的答案“< a href="https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit">Javascript Post 请求,如表单提交”。

它提供了 javascript for:

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

我认为这会做你想要的。

Take a look at the accepted answer to "Javascript Post Request like a Form Submit".

It provides javascript for for:

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

I think this will do what you want.

梦与时光遇 2024-10-06 01:24:26

感谢所有的答案。我已经解决了这个问题。数据已通过,但我无法正确处理。我只是添加了一个虚拟代码来测试它。它有效。完成后我会上传代码。谢谢大家。

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.

吃素的狼 2024-10-06 01:24:26

该函数位于 PHP 文件中,但充满了 JS 代码。最后一行将数据传递到另一个 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");

    }

这是 dbConn.php,由 saveProfile 方法调用。数据处理如下:

    $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";
        }

    }

This function is in a PHP file, but is full of JS code. The last line passes the data to another PHP file which saves the data into a database.

    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. 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";
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文