OutFront Forums
     Home    Register     Search      Help      Login    

Follow Us
On Facebook
On Twitter
RSS
Via Email

Recent Posts
Todays Posts
Most Active posts
Posts since last visit
My Recent Posts
Mark posts read

Sponsors
Shopping Cart Software
Ecommerce software integrated into Frontpage, Dreamweaver and Golive templates. No monthly fees and available in ASP and PHP versions.
Website Templates
We also have a wide selection of Dreamweaver, Expression Web and Frontpage templates as well as webmaster tools and CSS layouts.
Frontpage website templates
Creative Website Templates for FrontPage, Dreamweaver, Flash, SwishMax

 

Javascript Replace problem

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
Printable Version 

All Forums >> Web Development >> General Web Development >> Javascript Replace problem
Page: [1]
 
kt

 

Posts: 199
Joined: 11/3/2004
Status: offline

 
Javascript Replace problem - 11/2/2009 21:25:34   
Hi everyone

I have a javascript which I use to pass information to a database. I need to replace all the ampersands and apostrophes in the data, otherwise the script breaks.

But I'm having a little trouble getting the replace function to work properly.

Here's the whole script - I realise it's not very elegant and could no doubt be improved in other ways, but I'm new to Javascript, and my concern now is the replace function.


$(function() {
  $('.error').hide();
  			  $('.sub_spinner').hide();
  			  
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button").click(function() {

    $('.error').hide();
		
     var companypre = $("input#company").val();	 = 
     var company = companypre.replace(/"&"/g, "%26");

      if (company== "") {
      $("label#company_error").show();
      $("input#company").focus();
       return false;
       }

      var citypre = $("input#city").val();
      var city = citypre.replace(/"&"/g, "%26");

   var country = $("select#country").val();
		
       var detailspre = $("textarea#details").val();
       var detailspre2 = detailspre.replace(/(\r\n|\r|\n)/g, '<br />')
       var details = detailspre2.replace(/"&"/g, "%26");

       if (details== "") {
      $("label#details_error").show();
      $("textarea#details").focus();
      return false;
    }

		$('#test_null_submit').hide();
		$('.sub_spinner').show();

		var dataString = 'company=' + company.replace(/( )/g, '%20') + '&city=' + city.replace(/( )/g, '%20') + '&country=' + country.replace(/( )/g, '%20') + '&details=' + details.replace(/( )/g, '%20');
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "/includes/process.asp",
      data: dataString,
      success: function() {
      
                       $('.sub_spinner').hide();
                       
        $('#contact_form').html("<p class='conf_message' style='height:200px;margin-left:5px;color:#363636'></p>");
        $('.conf_message').html("<br />Thank you.")

        .hide()
        .fadeIn(350, function() {
          $('.conf_message');
        });
      }
     });
    return false;
	});
});


This works correctly to replace carriage returns with <br> in the textarea, and to replace spaces with %20 in every field (done in the datastring).

What doesn't work is replacing the ampersands.

As it is, it replaces all of the ampersands from the company field, but only the first one in the city field, and then the country field doesn't get saved at all. if there are no ampersands in the fields, of course it works fine.

I've tried also /(&)/g and /("&")/g but it makes no difference.

Can anyone tell me why the global function only works for the first variable?

Many thanks for your help.

kt
bobby

 

Posts: 11479
Joined: 8/15/1969
From: Seattle WA USA
Status: offline

 
RE: Javascript Replace problem - 11/3/2009 19:14:47   
/&/g

_____________________________

Talk to your kittens about catnip, or someone else will.


:)

(in reply to kt)
kt

 

Posts: 199
Joined: 11/3/2004
Status: offline

 
RE: Javascript Replace problem - 11/4/2009 15:38:25   
Thanks!

(in reply to kt)
bobby

 

Posts: 11479
Joined: 8/15/1969
From: Seattle WA USA
Status: offline

 
RE: Javascript Replace problem - 11/4/2009 19:23:58   
no problem

_____________________________

Talk to your kittens about catnip, or someone else will.


:)

(in reply to kt)
Page:   [1]

All Forums >> Web Development >> General Web Development >> Javascript Replace problem
Page: [1]
Jump to: 1





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts