Posting forms with Ajax and PHP

Started on the old forum in November 2009 · view the original in the archive · old links redirect here

1 reply · 465 views · started 17 years ago · 2 min read

synergyx2009 Developer · 86 posts #1

Hi Guys

I have hit a bit of a snag.

I will explain the setup. Firstly once i have loaded the web page, the form is then fetched and loaded automatically via AJAX without the page refreshing and loads the form into a div tag on my web page....this works 100%

After filling the form and i click on the send button, AJAX then posts the data to an external php file where the info is processed and added to the database.... this also works 100% but i have one problem. If the user uses & or & in any of the form fields, the info for just that field is not sent via AJAX and reads as a NULL value.

I know how to over come this with PHP but not Javascript. Simple. info sent -> Ajax / Javascript -> PHP -> DB

Here is the code used to post.

 <script type="text/javascript" >
	$(function()
		{
		$(".update").click(function()
			{
			var user_email = $("#user_email").val();
			var user_url = $("#user_url").val();
			var user_mobile = $("#user_mobile").val();
			var land_line = $("#land_line").val();
			var address = $("#address").val();
			var facebook_group = $("#facebook_group").val();
			var facebook_profile = $("#facebook_profile").val();
			var uid = $("#uid").val();
			var doupdate = $("#doupdate").val();
			
			var dataString = 'user_email='+user_email+ '&user_url='+user_url+ '&user_mobile='+user_mobile+ '&land_line='+land_line+ '&address='+address+ '&facebook_group='+facebook_group+ '&facebook_profile='+facebook_profile+ '&uid='+uid+ '&doupdate='+doupdate;
			
			$.ajax(
					{
					type: "POST",
					url: "<?php echo bloginfo('template_directory'); ?>/inc/insertajax.php?faction=update_profile",
					data: dataString,
					cache: false,
					success: function()
						{
						ajax_saveContent('profile_contact_information','<?php echo bloginfo('template_directory'); ?>/inc/external_1.php?action=profile_contact_information');
						}
					});
			return false;
			});
		});
	</script>

Now you will notice "var dataString" carries all the info of each form field and they are seperated by the & sign. This is where the problem is but i have no idea how to fix it.

Can anyone help?

|Dont forget to say thanx|

synergyx2009 Developer · 86 posts #2

i think i managed to figure it out, it seems to work

var about_me1 = $("#about_me").val();
			if (about_me1==null)
				{
				var about_me = "";
				}
			else
				{
				var about_me2 = about_me1.replace(/&/g, "and"); //g = searches for all & signs
				var about_me = about_me2.replace(/'/g, "\"");
				}

|Dont forget to say thanx|

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close