Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to sumbit a form in a jquery without loading the result?

Posted on 2011-09-27
8
Medium Priority
?
298 Views
Last Modified: 2012-05-12
Hi All Im trying to submit a form using jquery
and to send the form into a php page that will update the db without sending any result back to the element (I guess using ajax)


fElement.submit(function(e) {
                                          //fElement.attr('flag','1');
                                          e.preventDefault();
                                    //"mark" the input name =flag as spam
                                        $(":hidden[name='flag']","#"+popup_form_id).val("on");
                                          fElement.toggle();
                                          //hoe to post/get the form $.ajax(
                                          
<form id = "popup_form_{$row['id']}"  action ="" method ="post" style="background-color:white; border:2px solid blue;padding:4px; width:250px; display :none; position:absolute"  onsubmit = "return false;">

			<input type ="hidden" name="flag" value = "no" >

			<input type="hidden" name="id" value ="{$row['id']}">

			<input type="hidden" name ="email" value ="{$row['email_address']}">

			<input type="submit" value="SpamIt" name="spamIt" style="margin-right:15px;">

			<input type="button" value="Close" onclick="closePopup()"/>

			

			</form>

    </tr>

Open in new window

0
Comment
Question by:Nura111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 6

Accepted Solution

by:
GregArnott earned 1400 total points
ID: 36714228
There are numerous ways you can achieve this goal of submitting a form, changing to a new page without returning data back to an element.

It looks like you'd prefer not to use a serverside PHP solution, and to have it completely run via javascript and ajax.

jQuery Ajax documentation is the page for structuring the name value pairs, and to achieve the method of operation you desire. You'll probably want to trigger this ajax event with an event handler that applies a javascript controlled submit.

The raw bones of the code ends up something like:
$('#myForm').submit(function() {
  $.ajax({
  url: 'myphpsubmitpage.php',
  success: function( ) {
    //do nothing
  },
  complete: function() {
    // replace form with another element code, else code to redirect to another page as desired
  }
});
});

Open in new window


0
 
LVL 6

Assisted Solution

by:mattjp88
mattjp88 earned 600 total points
ID: 36715694
Greg's answer covers it mostly, but isn't clear in describing how you'd prefer to "not to  use a serverside PHP solution".

You'll still need PHP, since javascript cannot interact with the DB.  Using jquery's ajax method is the proper solution.  jQuery also provides a nice function for serializing a form into a data structure you cvan submit via jQuery's ajax.  http://api.jquery.com/serialize/  

So you can do something like:

$('#myForm').submit(function(){
$.ajax({
  data: $(this).serialize(),
  url: 'formSubmit.php'
});

Open in new window


Alternatively, there are nice jquery plugins that do most of this already.  Check out http://www.jainaewen.com/files/javascript/jquery/iframe-post-form.html or http://jquery.malsup.com/form/

-Matt
0
 
LVL 6

Expert Comment

by:GregArnott
ID: 36716436
"and to send the form into a php page that will update the db"

I was talking about it being a standard javascript handler into a standard PHP receipt page (alas - url: 'myphpsubmitpage.php') that only takes name||value pairs, instead of using an old standard HTML form submit to a PHP page of POST retrieval of each data element to achieve your name||value pairs.

I felt the OP has shown enough aptitude in the code presented to understand classic PHP solutions, and wanted to highlight the jQuery structure alternative.

By all the logic being generated in javascript - data retrieval, validation, sending to server, processing success and completion - makes it not a server side PHP solution.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Nura111
ID: 36718292
How is the 'formSubmit.php will access the variables (in data)
If I would use POST or GET the script will go to the $_POST array what do I need to do here?

Can I use .post() or .get() to do it? what will be the differences?

Thank you!

0
 

Author Comment

by:Nura111
ID: 36718736
Im trying to do




$(popup_form_id).post is not a function
function toggleLeadBody(id) {													
					var fieldName1 = "#spam_"+ id + "_body";
					var fieldName2 = "#sr_" + id + "_body";
					var popup_form_id = "popup_form_" + id;

					var url ="sem_page.php?page=leads&sub=manage";
					 
					var element = $(fieldName2);
                    element.toggle();   
					
					var fElement = $(fieldName1);
				//checking to see if its not been marked as spam	
					if ($(":hidden[name='flag']","#"+popup_form_id).val() =="no"){
					 fElement.toggle();	
					}
    //sumbitting the form			   	    
					fElement.submit(function(e) {
                         //   alert("Hello world!");
							//fElement.attr('flag','1');
							e.preventDefault();
						//"mark" the input name =flag as spam 
						    $(":hidden[name='flag']","#"+popup_form_id).val("on");
							fElement.toggle();
							//$.ajax(
							alert(url);
							$(popup_form_id).post("sem_page.php?page=leads&sub=manage");
								
							
					})

Open in new window

0
 

Author Comment

by:Nura111
ID: 36718746
Oh sorry so Im tring to do it in the way I added it and I keep getting the firebug error:
$(popup_form_id).post is not a function
0
 

Author Comment

by:Nura111
ID: 36718779
I didnt use it right sorry its $.post
0
 

Author Comment

by:Nura111
ID: 36718933
please help I have a problem with the data im transfering to the page Im using .post()

and sending to a php page (that was originally handling a regular form submit with method post (attached is the )code

isn't the variable of the form is going to be in Post like when you are submitting The form what am I missing here its doesn't work as expected

Thank you!


$("#"+popup_form_id).submit(function(e) {
                         //   alert("Hello world!");
							//fElement.attr('flag','1');
							e.preventDefault();
						//"mark" the input name =flag as spam 
						    $(":hidden[name='flag']","#"+popup_form_id).val("on");
							fElement.toggle();
							//$.ajax(
							alert($(this).serializeArray());
							$.post(url,$(this).serialize());
								
							
					})

Open in new window

if (isset($_POST['spamIt'])){
		//echo "try";
		 $id = $_POST['id'];
		 $email = $_POST['email'];

		 $sql= BaseModel::getSqlObject();
		
		 $query = "SELECT * FROM BlackList WHERE email_address = '$email'";	
	     
		if (($result=$sql->Query($query)) === FALSE)
           die(ShowError("Server Query Error"));
		$num = $result->num_rows;
	   
	//check if email is not already exist in BlackList
	    if ($result->num_rows == 0){
   			 $insert_query = "INSERT INTO BlackList(email_address) VALUES ('$email')";
			 
			 if (($result=$sql->Query($insert_query)) === FALSE)
           	die(ShowError("Server Insert Query Error"));
		}
		
		$insert_query =	"UPDATE ServiceRequests SET spam = 1 WHERE id = '$id'";
		if (($result=$sql->Query($insert_query)) === FALSE)
           	die(ShowError("Server Insert Query Error service"));


			die ("ok");
         
	
		
 
		
		}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question