Jump to content

Help with the evil slashes


Recommended Posts

OK, here's my problem. I have a php form that submits an email with the text the user specifies, and it works fine except that all of the \ symbols and ' symbols have a \ before them. Is there any way to stop this? It happens with any form element submitted to a form. For example:

This is part of "form.html"


<form action="example.php" method="post">
<input type="text" value="" id="example1" name="example1" />
<input type="submit" value="Submit" id="example2" name="example2" />
</form>

The text field "example1" gets filled in with

Example: \ '

And then there's example.php:


<?php echo $_POST["example1"] ?>

Which will display:

Example: \\ \'

How can I stop this???

Link to comment
Share on other sites


well there is more code that you are leaving out, i need to see that instead of the examples.

my guess is that you are doing

<input value=$_POST["example1"]  />

and this could cause problems with the slash, make sure to do

<input value="$_POST['example1']"  />

<?=$_POST["example1"] ?>
<form action="<?=$_SERVER[php_self]?>" method="post">
<input type="text" value="" id="example1" name="example1" />
<input type="submit" value="Submit" id="example2" name="example2" />
</form>

this works perfectly for me..

Edited by ripken204
Link to comment
Share on other sites

I'm not sure I understand your reply. This happens with any form element getting processed in any way by php. Here is a full-document example.

page1.html:

<html>
<head>
<title>Example</title>
</head>
<body>
<p>This is an example.</p>
<form action="example.php" method="post">
Your name is: <input type="text" value="" id="yourname" name="yourname" />
</form>
</body>
</html>

example.php

<html>
<head>
<title>My name is <?php echo $_POST["yourname"] ?></title>
</head>
<body>
Hello, <?php echo $_POST["yourname"] ?>. Welcome to this example page!
</body>
</html>

Now, say for example, someone's name is John O'rielly. Well, example.php would display the html code as follows:

<html>
<head>
<title>My name is John O\'rielly</title>
</head>
<body>
Hello, John O\'rielly. Welcome to this example page!
</body>
</html>

What I want to do is prevent it from showing that backslash. Does that explain it better? I hope?

Link to comment
Share on other sites

oh now why didnt u just say O'Reilly before! lol. i had the same problem when i was doing a database with a name entry system a few months ago :)

Sorry. :blushing: I guess I didn't give a good example. Oh well, thanks for trying anyway!

..And no, my name is not O'Rielly. :rolleyes:

--------------

Now I have a new question. How can I make a link to a file so that it will download the file rather than open it in the browser? For example, if there's a PDF document on a site and I link to it using the standard <a href="... method, it just opens in the window specified by target=. Is there a value for target= that will specify download, or some other option like that? Thanks in advance. ;)

Edited by Idontwantspam
Link to comment
Share on other sites

Well, I don't think so, actually. For example, if someone uploads an attachment to MSFN that is in .html format, clicking on the link to it opens the download dialog; it doesn't open the html file. I'll go look at the source for some pages that offer download rather than direct links and see if I can figure it out.

Link to comment
Share on other sites

That can be done through php with the use of Header() andn changing the content disposition. The info for that and more can be found at http://www.php.net/manual/en/function.header.php

Here is one of the examples that may be relevant to your needs.

Example 1578. Download dialog

If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

Be sure to read some of the user contributed notes for more hints and ideas on how to use this method to it's fullest with other file formats as well. :)

Edited by Chozo4
Link to comment
Share on other sites

Just another useful note - what Chozo4 posted can also be used to prevent hotlinking of files. Checking wether or not a session variable has been set (you set it from the main page) within that download.php file (or whatever you call it) will prevent hotlinking.

Link to comment
Share on other sites

well it would be best to make a function out of it so that you could reuse it

you would call it by doing:

<a target="_blank" href="<?=download('file.pdf');?>">text</a>

then the function would be:

<?
function download($file){
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=$file');
readfile($file);
}
?>

edit:

now you will have to make the link open a new page as well because when you do header() it has to be the first thing on the page, nothing can display on the page before you call it. so i added in the target=_blank

this should work, i just wrote it out here and didnt test it..

Edited by ripken204
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...