Link to home
Start Free TrialLog in
Avatar of RationalRabbit
RationalRabbit

asked on

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

SOLUTION
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RationalRabbit
RationalRabbit

ASKER

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.
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?
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.
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.
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">'
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.
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.
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.
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.
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.
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep, that was it. Sorry for all the confusion. Thanks for hanging in there, Scott.