Page 1 of 1

Passing values to php

Posted: Sat Oct 08, 2011 1:30 am
by Gopesh
Hi,i have a code which creates dynamic textboxes on user clicks using jquery.it prints the value of textboxes.How can i pass these values to another php page? pls help???

Code: Select all

<html>
<head>
<title>jQuery add / remove textbox example</title>
 
<script type="text/javascript" src="jquery-1.2.3.min.js"></script>
 
<style type="text/css">
	div{
		padding:8px;
	}
</style>
 
</head>
 
<body>
 
<h1>jQuery add / remove textbox example</h1>
 
<script type="text/javascript">
 
$(document).ready(function(){
 
    var counter = 2;
 
    $("#addButton").click(function () {
 
	if(counter>10){
            alert("Only 10 textboxes allow");
            return false;
	}   
 
	var newTextBoxDiv = $(document.createElement('div'))
	     .attr("id", 'TextBoxDiv' + counter);
 
	newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' +
	      '<input type="text" name="textbox' + counter + 
	      '" id="textbox' + counter + '" value="" >');
 
	newTextBoxDiv.appendTo("#TextBoxesGroup");
 
 
	counter++;
     });
 
     $("#removeButton").click(function () {
	if(counter==1){
          alert("No more textbox to remove");
          return false;
       }   
 
	counter--;
 
        $("#TextBoxDiv" + counter).remove();
 
     });
 
     $("#getButtonValue").click(function () {
 
	var msg = '';
	for(i=1; i<counter; i++){
   	  msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
	}
    	 // alert(msg);
		  document.getElementById("s").innerHTML=msg;
     });
	 
  });
</script>
</head><body>
 <form action="send.php" method="post">
<div id='TextBoxesGroup'>
	<div id="TextBoxDiv1">
		<label>Textbox #1 : </label><input type='textbox' id='textbox1' >
	</div>
</div>
<div id="s"></div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type='submit' value='Get TextBox Value' id='getButtonValue'>
 </form>
</body>
</html>




Re: Passing values to php

Posted: Sat Oct 08, 2011 3:06 am
by social_experiment
By accessing the corresponding $_POST value if the page is submitted?

Re: Passing values to php

Posted: Sat Oct 08, 2011 3:17 am
by Gopesh
@social_experiment. I tried it using $_POST['msg'].but it doesn't work..how can i pass the value from jquery to php

Re: Passing values to php

Posted: Sun Oct 09, 2011 11:45 am
by Gopesh
should anyone know how to solve it????

Re: Passing values to php

Posted: Sun Oct 09, 2011 7:05 pm
by ouchiko
Seems like your not really thinking about the differences between PHP and JS.

From a quick browse of your code you have a few options..

1. Each time the javascript addButton function is run you are adding a new <input> element to the form. Currently you're defining it as name="textbox". If the form is submitted you should find the $_POST['textbox'] variable to be valid however when you create more than one you are essentially over writing each other. Using name="textbox[]" when you create a button will give you an array sent through the destination script which you can access textbox[0]...N

Remember, if you're not submitting the form in the standard <HTML> way the you can do in javascript with document.<someformreference>.submit();

2. You could do what you are doing and cycle through the input boxes and generate a query string which you want to send a new script:

for(i=1; i<counter; i++){
msg += "textBox"+i+"="+$('#textbox' + i).val()+"&";
}
location.href = "destinationfile.php?"+msg;

Remember that this will be a GET request which will create long ugly query strings if the inputs within the text boxes [and the quantity] are large.


I would do as follows:

Code: Select all

newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' +
              '<input type="text" name="textbox[]' + counter + 
              '" id="textbox' + counter + '" value="" >');

then

Code: Select all

$("#getButtonValue").click(function () {
 
        var msg = '';
        for(i=1; i<counter; i++){
          msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
        }
         document.getElementById("myForm").submit();
     });

and then

Code: Select all

<form action="send.php" method="post" id="myForm">

Re: Passing values to php

Posted: Tue Oct 11, 2011 10:29 am
by Gopesh
Thanks ouchiko.the problem is solved.i used a foreach loop to get the valus from the dynamic textboxes.Thanks for ur reply...