Jump to content

Ajax form submission issue


andrew_biggart

Recommended Posts

I am trying to design a simple ajax form so I can learn ajax. I know the php page I have written works so the issue is definitely with the ajax. I think the problem is with the .submit any ideas? At the moment its not reaching the php page.

 

<script type="text/javascript">
$(document).ready(function() {

//when submitting form
$('#addclientform').submit(function() {
	send_form();
});

});
//function to sent the form
function send_form(){

	//get the client name and code	
	var clientname = $('#clientname').val();
	var clientcode = $('#clientcode').val();

	//use ajax to run the check
	$.post("add-client-info.php", { clientname: clientname, clientcode : clientcode },
		function(result){
		//if the result is 1
		if(result == 1){
			//show that the username is available
			$('#addclient').html('<h1 class="success">' +clientname + ' has been added to the list.</h1>');
		}
		else if(result == 2){
			//show that the username is NOT available
			$('#addclient').html('<h1 class="fail">Please make sure you add both a client name and a client code.</h1>');
		}
		else if(result == 3){
			//show that the username is NOT available
			$('#addclient').html('<h1 class="fail">' +clientname + ' has not been added to the list.</h1>');
		}
	});
}   
</script>

 

 

The form

 

            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="addclientform">
            	<input type="submit" name="addclientbtn" id="addclientbtn" class="addclientbtn" value="Add Client" />
            	<div id="client-code-wrap"> 
                    <div id="client-code"><input type='text' id='clientcode' name='clientcode' class="client-input" value="Client Code" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" /></div>
                    <div id='client-code-result'></div>
                </div>
                <div id="client-name-wrap">
                    <div id="client-name"><input type='text' id='clientname' name='clientname' class="client-input" value="Client Name" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" /></div>
                    <div id='client-name-result'></div>
            	</div>
            </form>

Link to comment
Share on other sites

Your submit function looks straightfoward. Have you tried troubleshooting your send_form function.

Also, something I noticed is you are using $_SERVER['PHP_SELF'] as your form action. This should be avoided due to CSS vulnerability. Read here for elaboration.

Link to comment
Share on other sites

Instead of passi variables. Try passing simple strung data to the server.

		$.post("add-client-info.php", { clientname: "clientname", clientcode : "test" }
		function(result){

Also, is your add-client-info.php in the sane dir as your executing script?

Link to comment
Share on other sites

To make sure at the function is actually being called upon submitting, I would simply have an alert() inside of your send_form() function and nothing more. Then once you are sure that the function is being triggered upon the form being submitted, you can be sure that something in the function is faulty and cam troubleshoot from there

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.