submit a value from span field

风格不统一 提交于 2019-12-19 11:18:06

问题


i have a from with a submit button

after using jquery the page will have :

for(var i=0 ; i <10 ; i++){
    '<span class="ioAddConcept">'+ currentConcept[i] +'</span>\n\
                  with\n\
                  <span class="ioAddRelation">'+ currentRelation[i] +'</span>\n\'
}

(that piece of code is just an example)

the variables currentConcept[] and currentRelation[] i got its values from database using Ajax

**i am using PHP**

and my question how to submit the page with these two variables ?

i mean in the server i hope something to be like this

 $concepts = $_POST['currentConcept[]']

回答1:


Get these values in jQuery like this:

var ioAddConcept = $(".ioAddConcept").html();
var ioAddRelation = $(".ioAddRelation").html();

Now you can set these values into form text boxes:

$('input.ioAddConcept').text( ioAddConcept );
$('input.ioAddRelation').text( ioAddRelation );

OR if you are submit form via AJAX request:

    $.ajax({
        url     : '/path/to/action.php',
        type    : 'POST',
        data    : 'value1=' + ioAddConcept '&value2=' + ioAddRelation,
        success : function( data ) {
                    alert(data);
                  }
    });

Get these values on server side:

print_r( $_POST );



回答2:


Add an ID to your span element

<span id="ElementID"> your variable text here</span>

Then use jquery to get the text out

var spanText = $('#ElementID').html();



回答3:


Something like that

var currentConcept = $('currentConcept').html();

And then you can send this var as a param in you request to server

Update

$("form").submit(function() {
     var currentConcept = $('.currentConcept').html();   // it's an Array
     var currentRelation = $('.currentRelation').html(); // it's an Array

   $.ajax({
     url     : // your URL,
     data    : 'currentConcept=' + currentConcept '&currentRelation=' + currentRelation,
     success : function( html ) {
             // write your code here
              }
});

}

// server side (php)
$currentConcept  = $_GET('currentConcept');
$currentRelation = $_GET('currentRelation');


来源:https://stackoverflow.com/questions/10674277/submit-a-value-from-span-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!