Solved

PHP variables in form submitted to a new window, $_POST single quotes or double quotes

Posted on 2008-10-21
12
531 Views
Last Modified: 2012-05-05
Hello Experts

I have a form that posts information to another page which is in a new window and I was wondering whether I should be using double quotes or single quotes in that next page

ie.

$var = $_POST['var'];
OR
$var = $_POST["var"];

what is the difference between using either and why does my page that the form is submitted to in the new window sometimes not show the variables ? mostly the first time I submit the form, click back and do it again and it works fine ?

Currently I am using double quotes

Thanks in Advance
0
Comment
Question by:Delacourt
  • 3
  • 3
  • 3
  • +1
12 Comments
 
LVL 8

Accepted Solution

by:
MatthiasVance earned 167 total points
ID: 22765190
There is no difference between using single quotes or double quotes, except that for strings no special content is being parsed when you use single quotes.

Kind regards,

Matthias Vance
0
 
LVL 3

Assisted Solution

by:Mr_Splash
Mr_Splash earned 167 total points
ID: 22765259
Like Matthias said it shouldn't make any differnce which you use.

Using doubles allows you to parse special content like viables or line breaks etc.

i.e.
$colour = "blue";
echo "The Sky is $colour"; = The Sky is Blue
echo 'The Sky is $colour'; = The Sky is $colour

The cause of the POST data not being sent must be elsewhere in your code. If you could post some more details we may be able to help more.
0
 
LVL 3

Expert Comment

by:Mr_Splash
ID: 22765262
Of course I mean 'Variables' not 'Viables' just to be clear.
0
 
LVL 1

Author Comment

by:Delacourt
ID: 22765275
Thanks so far, this is the code I have
//page that sends the form

<form action="prov_email.php" method="POST" target="_blank">

<?
 

print "<input type=\"hidden\" name=\"res_number\" value=\"$res_number\">";

print "<input type=\"hidden\" name=\"res_name\" value=\"$res_name\">";

print "<input type=\"hidden\" name=\"res_arrive\" value=\"$res_arrive\">";

print "<input type=\"hidden\" name=\"res_depart\" value=\"$res_depart\">";

print "<input type=\"hidden\" name=\"res_apt\" value=\"$res_apt\">";

print "<input type=\"hidden\" name=\"res_rate\" value=\"$res_rate\">";

print "<input type=\"hidden\" name=\"res_vl_agent\" value=\"$res_vl_agent\">";

?>

<p><input type="submit" value="Send Provisional Email in New Window" /></p>

</form>
 

//prov_email.php page form is being sent to

<?

//dbconnection

include"serv.inc";

error_reporting(0);
 

$res_number = $_POST["res_number"];

$res_name = $_POST["res_name"];

$res_arrive = $_POST["res_arrive"];

$res_depart = $_POST["res_depart"];

$res_apt = $_POST["res_apt"];

$res_rate = $_POST["res_rate"];
 

print "$res_number : res_number var<br />"; 

//the above does not always print the variable the first time, most times afterwards it does though ?

Open in new window

0
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22765307
In your initial page, where do you get the variables from?
Also, when experiencing problems with your code, you could add error_reporting(E_ALL) to the top of both your files.

Kind regards,

Matthias Vance
0
 
LVL 3

Expert Comment

by:Mr_Splash
ID: 22765343
On your first page, do all the values appear correctly when you view page source?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Delacourt
ID: 22765471
variables are received from another form (built in the exact same way except not sent to a new window and it works every time no problem)

values are always there and are correct

register globals are set to on - if this helps, i know the code doesnt need it on though
0
 
LVL 7

Assisted Solution

by:gemdeals395
gemdeals395 earned 166 total points
ID: 22766155
Hey Dela,
    What you need to do is make sure session_start() is fired off on the first page and whenever you have a problem on variables showing up the second time around what happens is that when you first called the page the variables get set and registered and given a value which is why when you refresh the page it works. Now You can fix it in 2 ways. One you can actually call the variable and give it a blank value $variable = ''; or Just use the $_SESSION superglobal as I do then you can switch register globals off. If you do it that way just set up the session variables when the page is called $_SESSION['res_number'] then just check when the form posts and you can then save to the session if(!empty($_POST['res_number'])) { $_SESSION['res_number'] = $_POST['res_number']: } . Then when you have all of the _SESSION vars populated correctly save it to the db and kill them in the session unset($_SESSION['res_number']); . Now I prefer to use signle quotes when calling arrays and everything not sent to the parser. Also on your code above there is no need to set a superglobal such as _GET _POST _SESSION or _COOKIE to a new variable like $res_number = $_POST["res_number"]; , Just in your script access it directly with $_POST['res_number']. Another way to make this easier is instead of having a page with a form and a page to process just have the page post to itself and adjust your output on something like $_SESSION['emailStep'] so like when the user first calls the page set emailStep to 1 then when it submits and process's just make it step 2 for output then you can kill the variables. Also on your form you can keep each line like this to only send the variable to the parser.

print "<input type=\"hidden\" name=\"res_number\" value=\"$res_number\">";

echo('<input type="hidden" name="res_number" value="'."$_SESSION[res_number]".'">');

Now when you use the style I did you will have to leave off single or double quotes around the name to prevent an error. Now on your page where you wanna check what wariables are set a much easier way to debug is right off the super global array like for example to check all post fields:

foreach($_POST as $key => $var) {
    echo("$key = $var<br>");
}

Then just change it to what your checking and if it exists it will display whats in the array, You can use that with $_REQUEST, $_GET, $_POST, $_SESSION, $_COOKIE, $_SERVER and make your debugging much easier.

Anyway hope that helps ;)
0
 
LVL 1

Author Comment

by:Delacourt
ID: 22766292
wow, ok need to read that again - thanks so far - out of interest is there a way to do it without sessions ?
0
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22766327
Yes, you can append them to your query string (ie. index.php?key1=value&key2=value) and then retrieve them with $_GET['key1'] etc.

Kind regards,

Matthias Vance
0
 
LVL 7

Expert Comment

by:gemdeals395
ID: 22766637
On your first page just try first naming each variable and giving it a value of nothing above your form then as long as the session is going and the variable is registered then on the second page it will display because it is now set up and exists in the session. ;)
0
 
LVL 7

Expert Comment

by:gemdeals395
ID: 22766665
then to print on the test page just say:

echo("$_POST[res_number]");

or

foreach($_POST as $key => $var) {
    echo("$key = $var<br>");
}

Then you can check every posted variable. Then in your code to say save everything just refer to the variables as $_POST[your_posted_variable]
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now