Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法使用JQuery循环XML解析_Php_Jquery_Xml_Parsing_Post - Fatal编程技术网

Php 无法使用JQuery循环XML解析

Php 无法使用JQuery循环XML解析,php,jquery,xml,parsing,post,Php,Jquery,Xml,Parsing,Post,我正试图以这样的方式从XML中提取文本:[text][choice in text][text continues][other text][other choice][other text continues] 我已经部分地做到了这一点,所以它提取了第一段和第一段的选项,但是它没有提取第二段的选项,不知道为什么。在本例中,第一段和第二段的选项相同 此外,我正在使用AJAX将所选选项的索引发布到php和mysql,但我不知道如何将这些选项分开。比如:第一选择->1;第二选择->3;等等 我的代码

我正试图以这样的方式从XML中提取文本:[text][choice in text][text continues][other text][other choice][other text continues]

我已经部分地做到了这一点,所以它提取了第一段和第一段的选项,但是它没有提取第二段的选项,不知道为什么。在本例中,第一段和第二段的选项相同

此外,我正在使用AJAX将所选选项的索引发布到php和mysql,但我不知道如何将这些选项分开。比如:第一选择->1;第二选择->3;等等

我的代码是:

function parseXml(xml)
{
var myArray = [];
var i = 0;

//Megkeressük az összes paragrafust
$(xml).find("Paragraph").each(function()
{   
    $(this).find("text").each(function(){
    {

        $("#wrapper").append('<div id="text">' + $(this).text()) + '</div><br />';
    $("#text").append('<div id="choice"></div>');   

    }       
});         
});

//Each paragraph
$(xml).find("Paragraph").each(function()
{  //Megkeressük az összes választékot  
$(this).find("choice").each(function()
{
    myArray.push($(this).text());

 });
});

 $("#choice").append('&nbsp;' + myArray[i] + '&nbsp;');
    sendValue(i); 


$("#choice").click(function() {

    if(i + 1 >=  myArray.length)
    {
            i=0;
            $("#choice").html('&nbsp;' + myArray[i] + '&nbsp;');
            sendValue(i);   
    }       

    else
    {       i++;
            $("#choice").html('&nbsp;' + myArray[i] + '&nbsp;');        
            sendValue(i); 
    }

});

function sendValue(str){

// post(file, data, callback, type); (only "file" is required)
$.post(

"ajax.php", //Ajax file

{ sendValue: str },  // create an object will all values

//function that is called when server returns a value.
function(data){
    $('#i').text(data.returnValue);
}, 

//How you want the data formated when it is returned from the server.
"json"
);

}

}
PHP:

<?php 

//Get Post Variables. The name is the same as 
//what was in the object that was sent in the jQuery
if (isset($_POST['sendValue'])){
$value = $_POST['sendValue'];   
}else{
$value = "";
}

//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.
 echo json_encode(array("returnValue"=>"This is returned from PHP : ".$value));  

?>
XML:

<?xml version="1.0" encoding="utf-8" ?>
<Page1>
<Paragraph>
<text>This is the first text in the</text><choice>paragraph</choice><choice>book</choice><choice>mádörfákör</choice><choice>asd</choice><text>after which the text continues.</text>
</Paragraph>
<Paragraph>
<text>This is the SECOND text in the</text><choice>paragraph</choice><choice>book</choice><choice>mádörfákör</choice><choice>asd</choice><text>after which the second text continues.</text>
</Paragraph>
</Page1>

像这样的怎么样

$(xml).find("Paragraph").each(function() {
    var paragraph = $('<div class="paragraph"></div>');
    paragraph.appendTo('#wrapper');

    var choiceCount = 0;
    $(this).find("text,choice").each(function() {
        if ($(this).get(0).tagName == 'TEXT') {
            $('<span class="paragraph-text"></span>').appendTo(paragraph).text($(this).text());
        } else {
            var choice = $('<span class="paragraph-choice"></span>').appendTo(paragraph).text($(this).text());
            (function(choiceCount) {
                choice.click(function() {
                    sendValue(choiceCount);
                });
            })(choiceCount);
            choiceCount++;
        }
    });
});

保存并在此处工作:

这实际上看起来相当不错,谢谢,唯一的问题是,它将选项放在相邻的位置,而不是一个单独的div,它会在单击时更改文本。。循环通过一个数组,我真的需要这个数组,因为这个索引将被发送到mysqlI,我保留了这个索引,我称之为choiceCount。如果打开JSFIDLE页面,我甚至会在单击选项时提醒该索引。至于样式,当然你可以随心所欲地编写CSS规则。添加显示:块;如果他们需要街区水平,或者如果你愿意的话,甚至可以换成a。对不起,伙计,我不能让它工作。复制了你的代码,但它甚至没有显示段落文本。我在考虑计算段落元素,然后将它们放入for循环。听起来可能有一些错误。如果您在JSFIDLE或任何类似的站点上重新创建您的设置,那么诊断起来就会更容易。我上面的示例确实可以运行,至少使用您提供的示例XML。