开发者

Javascript Text Fields and PHP variable Declaration

开发者 https://www.devze.com 2023-04-01 05:47 出处:网络
$(document).ready(function(){ var skillctr = 1; var experiencectr = 1; var educationctr = 1; var achievementctr = 1;

$(document).ready(function(){

var skillctr = 1;
var experiencectr = 1;
var educationctr = 1;
var achievementctr = 1;
var historyctr = 1;

/*
Add Skills
*/ 

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

if(skillctr>10){
        alert("WOW! But 10 skills are enough.");
        return false;
}   

var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'SkillDiv' + skillctr);

newTextBoxDiv.html('<label>Skill No. '+ skillctr + ' : </label>' +
      '<input type="text" name="skill' + skillctr +
      '" id="skill' + skillctr + '" value="" >');

newTextBoxDiv.appendTo("#SkillsBoxesGroup");


ski开发者_StackOverflow中文版llctr++;
 });

so how can i save the generated fields in my table in database now? how am i gonna declare the field in php like.. $_POST['skill']. i need help here. please


Don't use name="skill' + skillctr + '", use name="skill[]".
That way PHP will helpfully create an array that you can loop though:

var_dump($_POST);

foreach ($_POST['skill'] as $skill) {
    // save $skill to database
}


You just need to make sure that the input elements you are adding with that javascript are inside a <form>. Then when you submit the form to a php page, you can access the values with $_POST like you wrote above.

0

精彩评论

暂无评论...
验证码 换一张
取 消