Hello Friends I am using the two forms on asingle php page form1 and form2. i am inserting the value from form1 and retrieving it into form2 into a dropdown. but in form2 it shows me second last inserted data rather than last inserted data. but after refresh the page i get the last inserted data. i don't want to refresh the page. how can we get the values. pls help me friends

Member Avatar for diafol

You can do this via javascript - or if you have to involve a DB to insert the data from form1 before it goes to form2, you'll need to use ajax. Either way, it should only involve a couple of handfuls of js lines, if that.

If using jQuery:

<head>
<script src=" https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<form>
    <input name="text1" id="text1" />
    <select name="drop1" id="drop1">
        <option>first</option>
    </select> 
    <input name="butt" id="butt" type="button" value="ClickMe" />
</form>
<script>
	$('#butt').click(function(){
		var text1 = $("#text1").val();
		var drop1 = $('#drop1');
		alert(text1);
		drop1.append($('<option> </option>').val(1).html(text1));
        });
</script>
</body>

Alternatively you could wrap the js at the bottom with a document.ready and place it in the head section.
Although I've placed both controls in the one form, you could place the dropdown anywhere in the page and it should be updated.

Can you tell me how can i use the ajax for sending the data to the form2

Ardav Can you tell me how can we achieve it.

Member Avatar for diafol

OK, I'm procrastinating wrt marking my science homework, so may as well:

<script>
	$('#butt').click(function(){
		var text1 = $("#text1").val();
		$.ajax({
  			type: "POST",
  			url: "myajax.php",
  			data: "addop=" + text1,
			cache: false,
  			success: function(rtn){
    			   if(rtn != ''){
		              $('#drop1').append($('<option> </option>').val(1).html(rtn));
			   }else{
		              alert("Could not add option");
			   }
                        }
		});
	});
</script>

***Not tested***

You'll need to pick up the $_POST in the myajax.php file, check and clean it and add it to the DB. If all that is successful, return the text1 data like this:

echo $cleanText1;

THis will be the 'rtn' text in the 'success' option.

Remember to turn off any warnings/errors in the php file, or that output will be included in the dropdown as well!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.