jQuery submit to processing form (non-Ajax) urlencoded

I have a standard HTML form with a jQuery onclick function which does several processes, depending on the submit. All functions are working fine, but the simplest section of this jQuery code just does a normal submit. However, this form data on this submit goes to a PDF creation file and needs to be URLencoded. I cannot figure out how to do this without listing every field in a very long form. I've searched the Web but not found an answer. Seems there should be a simple way to url encode this submit. Here is the current submit function:
<script>
    $(document).on('click', '.ClickCheck', function(e)
    {
        var Form = $('#TheForm');
        $(Form).attr('action', './PDFResultPage.php');
        $(Form).submit();
    });
</script>

Open in new window

LVL 1
RationalRabbitAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
Why don't you use the .serializeArray() method? I think it's easier to manipulate the elements inside the form and their data.Check this.
serializeArray() method
0
RationalRabbitAuthor Commented:
I originally thought of using serialize. serializeArray is probably better, but I am confused as to how I would send that forward using the code I've described above. encodeURIComponent(url) ?
And maybe this is overkill anyway. My main problem is getting rid of escape codes and I could just stripslashes in the receiving file and be done with it. The escapes do not appear until after the submit.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
If you submit using the jquery code you have or directly in a form, the data will be exactly the same for your receiving page PDFResultPage.php.

PDFResultPage.php will not know the difference.   If you have the following

<form method="get" action="PDFResultPage.php">
<textarea name="test">testing one two three using "double" and 'single'</textarea>
<button type="submit">Submit</button>
</form>

Open in new window

You end up with PDFResultPage.php?test=testing%20one%20two%20three%20using%20%22double%22%20and%20%27single%27

You can then grab the data using

$data = $_GET['test'];
echo $data;
// output will be:  testing one two three using "double" and 'single'

Open in new window


If it is a post, you don't see the urlencoded data

<form method="post" action="PDFResultPage.php">
<textarea name="test">testing one two three using "double" and 'single'</textarea>
<button type="submit">Submit</button>
</form>

Open in new window


And the output from PDFResultPage.php will be the same.
$data = $_POST['test'];
echo $data;
// output will be:  testing one two three using "double" and 'single'

Open in new window


Does this clear things up, or do you need the output to be urlencoded so either the name of the file, content or just the url is urlencoded for spaces etc?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

RationalRabbitAuthor Commented:
That makes sense to me. However, the PDF text was coming out escaped. Thinking the PDF class was causing it, I put one of the query string variables at the top of the page, before the class was instantiated. It's been a few days now. I'll have to go back and test again to be absolutely sure, but that, as I recall, brought me here, thinking that, somehow, jQuery was doing this escaping.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
It could be the pdf class like you said.

If you are going to accept data to your database, you may use http://php.net/manual/en/function.htmlspecialchars.php and that will convert things like spaces to %20.  If you have data in a field that looks like, "testing%20123" it will output to a web page as, "testing 123" if you are echoing directly to html. However, if you end up echoing that out lets say to a text file, the text file will contain the %20.  Then you would need to decode http://php.net/manual/en/function.htmlspecialchars-decode.php

What you may need to do in your class that generates the textual content is decode.
0
RationalRabbitAuthor Commented:
It has Been a while before I could get back to this. So here is the situation -
  • Contradicting what I said previously, characters ARE appearing escaped at the top of the receiving file.
  • Database has nothing to do with it. This is just about how to properly remove escaped data sent to a PHP class using the jQuery code at the top of this post.
  • I could use stripslashes(), but I hear a lot about that being bad practice, although I have never seen an explanation as to why.
  • Seems curious that this is even happening. The form declaration is simply this '<form id="FillBL" name="BLForm" method="post">'
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I think before you come up with possible solutions, let's take a step back and redefine the question.

What class or library are you using to generate the pdf?

Can you create a short example page and data that we can use to recreate your issue.
0
RationalRabbitAuthor Commented:
Despite my original speculation, the class has nothing to do with it. I could just as well send it to a file with nothing but <?PHP echo($_POST[var]); ?> and, if there are escapable characters, such as quotes or apostrophes, the variable will still be escaped. So the problem seems to be in jQuery's submit function.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I'm trying to help. When I am asking for some details it is to do just that. I have shown in my previous post that submitting by jquery or a direct post does not make a difference.

The way we can assist here is by duplicating your issue.
0
RationalRabbitAuthor Commented:
I think, by posing the question in the first place, I am probably creating a problem where none exists. Seems the escape is standard http procedure, no?
Normally, if I am posting, The post is going into a database table and runs through a sanitize routine that handles the backslashes, so I wasn't expecting it. Please pardon my ignorance. I should know better.
I appreciate your attempts to help.  Unless you can give me a reason NOT to use stripslashes() (and I would certainly consider that an answer), I'll just do that and be done with it.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
What's happening is you are second guessing and giving partial information. Cart before the horse if you will.

Going back to what I said about posted data is it does not matter if you are doing a direct post or ajax, the data gets url encoded already.  What I should have mentioned in that post is that the default encrypt type (not data encryption) is application/x-www-form-urlencoded  .   All of that I explained.  If your form is multipart/form-data  which would indicate some type of upload, then that may not be happening.

There are some moving parts here and that is why I am asking to see full example code and to know which class you are using.  Without details like this all we are doing is taking wild guesses.  The best way to get an answer is to be able to start with a good question free of solutions and only state the problem.  Then we can work on the solution from the ground up. That may sound like a little more work on the front end, but makes it faster in the long run.
0
RationalRabbitAuthor Commented:
This is on a production server, not accessible publicly.
I can make it real simple. If I run this file:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Lang" content="en">
<title>Untitled</title>
</head>
<body>
   <form name="MyForm" id="MyForm" method="post" action="receive.php">
      <input name="Name" id="Name" type="text" /><br />
      <input type="submit" name="Submit" value="Create" />
   </form>
</body>
</html>

Open in new window

To this receiving file:
<?php
  echo($_POST[Name]);
?>

Open in new window

And I type "Tom O'Rorke" in the input box, the result will be "Tom O\'Rorke".
If I run the same on my server, the slash is not there. I am running PHP 5.4.45 on my server, so Magic Quotes is not even available, so what is normal behavior and what isn't? And if a non-escaped result is the normal behavior, what could be causing this? That's the question.

[EDIT] I just checked the development server and found that someone had turned magic_quotes_GPC ON. It was previously set to OFF.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Magic quotes should be off.

On my server I get

Tom O'Rorke

Open in new window


If you turn it off, do you get the expected results?
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RationalRabbitAuthor Commented:
Yep, that was it. Sorry for all the confusion. Thanks for hanging in there, Scott.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.