2011-03-30 112 views
-2

如果可以从文本输入变量?PHP:将输入值结果转换为字符串

$price = 10; 
$shipping ="<input type=\"text\" id=\"shipping\" value=\"$row[price]\">"; <---- sring? 

,并显示新的变量:

$total = $price+$shipping; 
+0

是否要从字符串中提取$ row ['price']或将附加到总变量? – Belinda 2011-03-30 13:01:11

+1

我不明白你在做什么?你能详细说明并添加更多代码吗? – Ant 2011-03-30 13:01:44

+0

为什么这个新问题?我们在这里已经解决了这个问题http://stackoverflow.com/questions/5486205/php-onchange-option-select-update-total它没有什么与数据类型转换和所有与PHP执行有关JS之前终止甚至被执行。 – Treffynnon 2011-03-30 13:03:55

回答

0

你的代码行是没有意义的。

你需要做的

$total = $row['price'] + $shipping; //assuming you actually have numericals in here 

此外,您可能希望只使用'(单引号),让您的HTML输入

$shipping ="<input type='text' id='shipping' value='{$row['price']}'>"; 

更少的逃脱参与和PHP变量较多可见。这只是一个建议,当然要归功于个人偏好:)

+0

请参阅我对上述问题的评论,以减轻您对OP问题的困惑。 :) – Treffynnon 2011-03-30 13:11:23

+0

@Treffynnon ohhhh!投票结束,谢谢你的抬头 – JohnP 2011-03-30 13:13:08

0

那么运输成本并没有通过这里。 您正在为“shipping”分配一个实际上是html的字符串。

你应该做的是

<html> 
    <form> 
    <input type="text" name="cost" value="10" /> //this should not be entered by the user 
    <select name="shipping"> // choose a shipping method 
    <option value="1.99">$1.99</option> 
    <option value="10.99">$10.99</option> 
    <option value="109.90">$109.90</option> 
    </select> 
    </form> 
</html> 

<?php 
    if (isset($_POST['cost']) && isset($_POST['shipping'])) { 
    $grand_total = $_POST['cost'] + $_POST['shipping']); 
    } 
?> 

你并不需要在这里投下任何东西,因为PHP是搞清楚你想要的东西很不错。它会将您的字符串转换为浮点数或整数来进行计算并将其分配给$ grand_total