Jump to content

Strings printing as Ints

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
18 replies to this topic

#1
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Hi all,

I thought I'd try my hand at some PHP. Am creating a simple-ish web-form, that could be used to place an order. It then sends out confirmation/order emails to the correct places.

It all seems to be working fine, apart from one thing. Whenever I'm trying to print text, rather than an int from a variable, it gives me a strange int... I can't even really work out where is is coming from.

I'm using the POST method from my html form, grabbing the data as below:


$firstName = $_POST['firstName'];

$lastName = $_POST['lastName'];

$email = $_POST['email'];

$telno = $_POST['telno'];

$add1 = $_POST['addressL1'];

$add2 = $_POST['addressL2'];

$town = $_POST['town'];

$county = $_POST['county'];

$postCode = $_POST['postCode'];

$selUnit = $_POST['unitMaterial'];

$quantity = $_POST['quantity'];


However, when I try and print those variables, the ones that are ints ("quantity") appear ok, but the others seem to give int values too. Here is an instance (trying to print address):


<?php 

	echo $add1 . "<br />";

	echo $add2 . "<br />";

	echo $town . "<br />";

	echo $county . "<br />";

	echo $postCode . "<br />";

?>


Now to me that's just standard, really simple sort of stuff (I think I'm doing the adding of the two strings ok for PHP).

But all that gives an output in my email of "40000". I'm just not quite sure, as I'm not comfortable in debugging in PHP yet, why it would be giving the int value.

Any suggestions would be awesome, thanks. :)

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Try echoing out the direct post data:
<?PHP
//print_r($_POST);
var_dump($_POST);

Oh and:
<?PHP
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$telno = $_POST['telno'];
$add1 = $_POST['addressL1'];
$add2 = $_POST['addressL2'];
$town = $_POST['town'];
$county = $_POST['county'];
$postCode = $_POST['postCode'];
$selUnit = $_POST['unitMaterial'];
$quantity = $_POST['quantity'];

/*
Try this:
*/
foreach($_POST as $id=>$value) {
		$$id = $value;
}


#3
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Thanks for the reply. :)

Would you be able to explain your second lot of code to me please?

Using the first one gets me closer, except it prints added stuff I don't want (size of string). Thanks though!

EDIT: Just used print_r, that works great. Thanks mate! :)

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
The first one does get you closer. If you know its a string or an int you will know if its converting it wrong. Since they were infact strings you might be getting the names mixed up.

The second bit of code is pretty weird looking if you did not start with PHP. Because variable all start with $ you can identify them better a variable variable is unique:
$code = "call";
$call = "hello world";
echo $$code; //prints out "hello world"

The foreach is pretty much a while loop but helps you assign variable a bit better.
<?PHP
$arg['one'] = 1;
$arg['two'] = 2;
$arg['code'] = "call";
foreach($arg as $id=>$value) {
//$arg is the variable name
//$id would be the array key (one, two, code)
//$value is the value (1,2, "call")
$$id = $value; //makes $one = 1;$two=2;$code="call";
}


#5
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Ah, great! Thanks so much for that explanation man. Makes sense now! Cheers! :D

#6
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Is your form still printing out weird stuff? Can you show me or give me the full code?

#7
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts

BlaineSch said:

Is your form still printing out weird stuff? Can you show me or give me the full code?

Sure I can. It's printing almost correctly, except it appears to be putting '1' at the end of some of the lines.

It's not finished elsewhere (there are errors) but I was fixing this part first.

Here is the script code:

<html>

<head></head>

<body>


<?php

$firstName = $_POST['firstName'];

$lastName = $_POST['lastName'];

$email = $_POST['email'];

$telno = $_POST['telno'];

$add1 = $_POST['addressL1'];

$add2 = $_POST['addressL2'];

$town = $_POST['town'];

$county = $_POST['county'];

$postCode = $_POST['postCode'];

$selUnit = $_POST['unitMaterial'];

$quantity = $_POST['quantity'];


$d_fullName = $_POST['d_name'];

$d_add1 = $_POST['d_addressL1'];

$d_add2 = $_POST['d_addressL2'];

$d_town = $_POST['d_town'];

$d_county = $_POST['d_county'];

$d_postCode = $_POST['d_postCode'];

include (thankyou.php);

?>


//SEND TO CUSTOMER

<?php

//define the receiver of the email

$to = $email;

//define the subject of the email

$subject = 'SFL Materials - ORDER CONFIRMATION'; 

//create a boundary string. It must be unique 

//so we use the MD5 algorithm to generate a random hash

$random_hash = md5(date('r', time())); 

//define the headers we want passed. Note that they are separated with \r\n

$headers = "From: order@sflmaterials.co.uk\r\nReply-To: info@sflmaterials.co.uk";

//add boundary string and mime type specification

$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 

//define the body of the message.

ob_start(); //Turn on output buffering

?>

--PHP-alt-<?php echo $random_hash; ?>  

Content-Type: text/plain; charset="iso-8859-1" 

Content-Transfer-Encoding: 7bit


Testing. PLAINTEXT EMAIL.


$email

$telno

$firstName

$lastName


--PHP-alt-<?php echo $random_hash; ?>  

Content-Type: text/html; charset="iso-8859-1" 

Content-Transfer-Encoding: 7bit


<b>Dear <?php echo print_r($_POST['firstName']); ?>,</b><br /><br />


Thank you for your order with SFL Materials. Your order is as follows:<br /><br />


<?php echo $quantity; ?> x <?php echo print_r($_POST['unitMaterial']); ?><br />

<b>Total: 

<?php

$total = 48.50*$quantity;

echo "£" . $total;

?>

</b><br /><br />

This will be dispatched once payment is received. Payment should be sent within 7 days of placing this order via bank transfer.

The details for this are as follows:<br /><br />


<b>NatWest Bank plc<br />

Account Number: 55720641<br />

Sortcode: 55-50-05</b><br /><br />


Once that payment has been received, your item(s) will be dispatched to the following address:<br /><br />


<?php 

	echo print_r($_POST['firstName']) . print_r($_POST['lastName']) . "<br />";

	echo print_r($_POST['addressL1']) . "<br />";

	echo print_r($_POST['addressL2']) . "<br />";

	echo print_r($_POST['town']) . "<br />";

	echo print_r($_POST['county']) . "<br />";

	echo print_r($_POST['postCode']) . "<br />";

?>


//else {

	//echo $d_add1 + "<br />";

	//echo $d_add2 + "<br />";

	//echo $d_town + "<br />";

	//echo $d_county + "<br />";

	//echo $d_postCode + "<br />";

//}


<br /><br />


If you have any queries about this order, please feel free to contact us by calling <b>01939 270768

</b>, or alternatively send us an email to <a href="mailto:info@sflmaterials.co.uk">info@sflmaterials.co.uk</a>.

<br /><br />

Thank you once again for your order,<br /><br />

SFL Materials

<a href="http://sflmaterials.co.uk">www.sflmaterials.co.uk</a><br /><br />

_________<br /><br />

<p>Registered in England. Registration number: OC349287. Registration address: As above.<br />

<a href="http://sflmaterials.co.uk/PP.pdf" target="_blank">Privacy Policy</a> | 

<a href="http://sflmaterials.co.uk/TC.pdf" target="_blank">Terms and Conditions of Sale</a></p><br />




--PHP-alt-<?php echo $random_hash; ?>--

<?

//copy current buffer contents into $message variable and delete current output buffer

$message = ob_get_clean();

//send the email

$mail_sent = @mail( $to, $subject, $message, $headers );

//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 

echo $mail_sent ? "Mail sent" : "Mail failed";

?>


//SEND TO SFL








?>

</body>

</html>


Basically, it is buffering this, so it can also send a plain text email as well. But like I say, it's a long way from finished - one part at a time! :D

Here is the HTML form code:

<form action="orderProc.php" name="orderForm" method="post" onSubmit='return validate(this)'>

				<p class="contentText"><b>Contact Details<br /><br /></b>First Name: </p>

				<input type="text" name="firstName" size="20" maxlength="40" /><br /><br />

				<p class="contentText">Last Name: </p>

				<input type="text" name="lastName" size="20" maxlength="40" /><br /><br />

				<p class="contentText">Email: </p>

				<input type="text" name="email" size="30" maxlength="40" /><br /><br />

				<p class="contentText">Telephone No: </p>

				<input type="text" name="telno" size="15" maxlength="20" /><br /><br />

				<p class="contentText">Address Line 1: </p>

				<input type="text" name="addressL1" size="50" maxlength="100" /><br /><br />

				<p class="contentText">Address Line 2: </p>

				<input type="text" name="addressL2" size="50" maxlength="100" /><br /><br />

				<p class="contentText">Town/City: </p>

				<input type="text" name="town" size="30" maxlength="50" /><br /><br />

				<p class="contentText">County: </p>

				<input type="text" name="county" size="30" maxlength="50" /><br /><br />

				<p class="contentText">Post Code: </p>

				<input type="text" name="postCode" size="8" maxlength="9" /><br /><br /><br />

				<p class="contentText"><b>Order Details</b><br /><br />

			  <table width="560" border="0" cellspacing="10" style="margin-left:-10px;">

                  <tr>

                    <td width="281"><p class="contentText">Select Material:</p></td>

                    <td width="61"><p class="contentText">Quantity:  </p></td>

                    <td width="100"><p class="contentText">Unit Price:</p></td>

                  </tr>

                  <tr>

                    <td><select id="unitMaterial" />

							<option>Reporting Accidents and Incidents at Work: Unit 1</option>

							<option>World of Work: Unit 1</option>

						</select></td>

                    <td><input type="text" name="quantity" size="4" maxlength="6" /></td>

                    <td><input name="txtPrice" disabled="true" value="£48.50" size="6" /></td>

                  </tr>

                </table><br /><br />

				<p class="contentText"><b>Delivery Address</b> (leave blank if same as above)<br /><br />

				<p class="contentText">Full Name: </p>

				<input type="text" name="d_name" size="40" maxlength="40" /><br /><br />

				<p class="contentText">Address Line 1: </p>

				<input type="text" name="d_addressL1" size="50" maxlength="100" /><br /><br />

				<p class="contentText">Address Line 2: </p>

				<input type="text" name="d_addressL2" size="50" maxlength="100" /><br /><br />

				<p class="contentText">Town/City: </p>

				<input type="text" name="d_town" size="30" maxlength="50" /><br /><br />

				<p class="contentText">County: </p>

				<input type="text" name="county" size="30" maxlength="50" /><br /><br />

				<p class="contentText">Post Code: </p>

				<input type="text" name="d_postCode" size="8" maxlength="9" /><br /><br />

				<p class="contentText"> <input type="checkbox" name="accept" value="no"> I accept the <a class="ip" href="http://sflmaterials.co.uk/TC.pdf" style="text-decoration:underline;" target="_blank">terms and conditions of sale.</a></p><br /><br />

				<input type="submit" name="placeOrder" value="Place Order" />

				</form>


Thanks for all your help, mate. :) Appreciate it!

#8
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
I found your issue.
echo print_r($var);
Print_r is something that will output already, so it will return a true/false value which echo is printing out as a 1

echo print_r($var,true); //turns output to a string


#9
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts

BlaineSch said:

I found your issue.
echo print_r($var);
Print_r is something that will output already, so it will return a true/false value which echo is printing out as a 1

echo print_r($var,true); //turns output to a string

Ahh great!! That works perfectly. Thank you very much :)

#10
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Actually, one last question if you don't mind.

How do I grab the string that is contained within the select box (unitMaterial)? I can manage to get the index of it, but how do I get what was actually in it, at that index? CAN I do that?

#11
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Select boxes don't return the entire string only the value:

<select id="unitMaterial" />
<option value="1">Reporting Accidents and Incidents at Work: Unit 1</option>
<option value="2">World of Work: Unit 1</option>
</select>

$unitMaterial = ($unitMaterial==1)? 'Reporting Accidents and Incidents at Work: Unit 1':'World of Work: Unit 1';

OR
<select id="unitMaterial" />
<option value="Reporting Accidents and Incidents at Work: Unit 1">Reporting Accidents and Incidents at Work: Unit 1</option>
<option value="World of Work: Unit 1">World of Work: Unit 1</option>
</select>

Hope that helps!

#12
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Brill! Thanks again! I've learned a lot tonight :D