954,582 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Retrieving the values on the same page

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

tarunfuture
Newbie Poster
21 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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

tarunfuture
Newbie Poster
21 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Ardav Can you tell me how can we achieve it.

tarunfuture
Newbie Poster
21 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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['addop'] 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!

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: