如何使用“if(!empty($ _ POST ['....'])){$ .... =' - ';}”?

时间:2012-04-24 18:57:35

标签: php forms post undefined

我不明白为什么我的代码“似乎”不正确。

我得到了我的.html表单,如下:

我的表格的一部分:

<input type="radio" name="going_to_order" value="Yes" id="going_to_order_yes"/> Ja
<input type="radio" name="going_to_order" value="No" id="going_to_order_no"/> nee

部分我的.js(Jquery)

$.post('mail.php',  
{going_to_order:$("input[name=going_to_order]:checked").val()},

我的mail.php的一部分

$going_to_order = htmlspecialchars($_POST['going_to_order']);

if(!empty($_POST['going_to_order'])){$going_to_order = 'xxx';}

$message .=  "<tr><td><strong>Is going to order?</strong> </td><td>" . $going_to_order . "</td></tr>";

如果我注释掉if(!empty))行,我会得到正确的值YesNo,但是当将单选按钮留空时,我会收到undefined个帖子。< / p>

启用if(!empty))行后,无论是否选中单选按钮,我都只会获得xxx值。

请帮我正确使用if(!empty)),以便在选择广播时获取值,但如果留空,我看不到undefined


编辑:PHP代码:

<?php

// Collect 'Algemeen'
$algemene_rating = htmlspecialchars($_POST['algemene_rating']); 
$gevonden_wat_u_zocht = htmlspecialchars($_POST['gevonden_wat_u_zocht']);
$gaat_u_bestellen = htmlspecialchars($_POST['gaat_u_bestellen']);
$wat_zocht_u = htmlspecialchars($_POST['wat_zocht_u']);
$feedback_motivatie = htmlspecialchars($_POST['feedback_motivatie']);

// Collect 'Fout'
$fout_op_website = htmlspecialchars($_POST['fout_op_website']);
$url_van_foutpagina = htmlspecialchars($_POST['url_van_foutpagina']);
$fouturl_op_website = htmlspecialchars($_POST['fouturl_op_website']);
$feedback_fout = htmlspecialchars($_POST['feedback_fout']);

// Collect 'Compliment'
$compliment_over_website = htmlspecialchars($_POST['compliment_over_website']);
$feedback_compliment = htmlspecialchars($_POST['feedback_compliment']);

// Collect 'Suggestie'
$suggestie_geven = htmlspecialchars($_POST['suggestie_geven']);
if ($_POST['suggestie_dienstverlening'] == "true")  {$suggestie_onderwerp_01 .= "<li> Dienstverlening </li>";}
if ($_POST['suggestie_opmaak'] == "true")           {$suggestie_onderwerp_02 .= "<li> Opmaak </li>";}
if ($_POST['suggestie_functionaliteit'] == "true")  {$suggestie_onderwerp_03 .= "<li> Functionaliteit </li>";}
if ($_POST['suggestie_site_inhoud'] == "true")      {$suggestie_onderwerp_04 .= "<li> Site inhoud </li>";}
if ($_POST['suggestie_anders'] == "true")           {$suggestie_onderwerp_05 .= "<li> Anders.. </li>";}
$feedback_suggestie = htmlspecialchars($_POST['feedback_suggestie']);

// Collect 'Inhoud'
$feedback_over_inhoud = htmlspecialchars($_POST['feedback_over_inhoud']);
$feedback_over_inhoud_url = htmlspecialchars($_POST['feedback_over_inhoud_url']);
$feedback_over_inhoud_url_klopt = htmlspecialchars($_POST['feedback_over_inhoud_url_klopt']);
if ($_POST['inhoud_onjuiste_tekst'] == "true")      {$inhoud_onderwerp_01 .= "<li> Onjuiste Tekst </li>";}
if ($_POST['inhoud_schending_copyright'] == "true") {$inhoud_onderwerp_02 .= "<li> Schending Copyright </li>";}
if ($_POST['inhoud_ontbrekende_tekst'] == "true")   {$inhoud_onderwerp_03 .= "<li> Ontbrekende Tekst </li>";}
if ($_POST['inhoud_anders'] == "true")              {$inhoud_onderwerp_04 .= "<li> Anders.. </li>";}
$feedback_inhoud_tekst = htmlspecialchars($_POST['feedback_inhoud_tekst']);

// Collect 'Producten'
$feedback_producten = htmlspecialchars($_POST['feedback_producten']);
$feedback_over_inhoud_url_klopt = htmlspecialchars($_POST['feedback_over_inhoud_url_klopt']);
if ($_POST['product_onjuiste_ontbrekende_beschrijving'] == "true")  {$product_onderwerp_01 .= "<li> Onjuiste of ontbrekende tekst </li>";}
if ($_POST['product_fout_op_de_productpagina'] == "true")           {$product_onderwerp_02 .= "<li> Fout op de productpagina </li>";}
if ($_POST['product_missend_product'] == "true")                    {$product_onderwerp_03 .= "<li> Missend product </li>";}
if ($_POST['product_anders'] == "true")                             {$product_onderwerp_04 .= "<li> Anders.. </li>";}
$feedback_over_producten = htmlspecialchars($_POST['feedback_over_producten']);

// if Collect '' = empty
//if(empty($_POST['algemene_rating'])){$algemene_rating = '-';}
//if(empty($_POST['gevonden_wat_u_zocht'])){$gevonden_wat_u_zocht   =   '-';}
//if(empty($_POST['gaat_u_bestellen'])){$gaat_u_bestellen   =   '-';}
//if(empty($_POST['fout_op_website'])){$fout_op_website =   '-';}
//if(empty($_POST['fouturl_op_website'])){$fouturl_op_website   =   '-';}
//if(empty($_POST['compliment_over_website'])){$compliment_over_website =   '-';}
//if(empty($_POST['suggestie_geven'])){$suggestie_geven =   '-';}
//if(empty($_POST['feedback_over_inhoud'])){$feedback_over_inhoud   =   '-';}
//if(empty($_POST['feedback_over_inhoud_url_klopt'])){$feedback_over_inhoud_url_klopt   =   '-';}
//if(empty($_POST['feedback_producten'])){$feedback_producten   =   '-';}



//Collect 'Emailadres'
$email = htmlspecialchars($_POST['email']);

// Collect: 'Attributes'
$tijd = time();
$datum = strftime("%d/%m/%y %H:%M", $tijd);
$ip = getenv("REMOTE_ADDR");

// Collect 'Mail info'
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Het bericht voor de ontvanger 
$message = '<html><body>';
$message .= '<table rules="all" style="background-color:#F8F8F8; width:600px; font-family:arial; font-size:11px; valign:top">';
$message .= '<table rules="all" border="1px solid #666666" style="background-color:#F8F8F8; width:600px; font-family:arial; font-size:11px;">';
$message .= '<td style="width:200px;"  /></td>';
$message .= "<tr><td><strong></strong> </td><td><h3>Feedbackformulier Multihout.nl</h3></td></tr>";
// 'Attributes'
$message .=  "<tr><td><hr/></td><td><hr/></td></tr>";
$message .=  "<tr><td><strong>Feedback datum:</strong> </td><td>        " . $datum . "</td></tr>";
$message .=  "<tr><td><strong>Feedback e-mailadres:</strong> </td><td>  " . $email . "</td></tr>";
$message .=  "<tr><td><strong>Feedback IP-adres:</strong> </td><td>     " . $ip . "</td></tr>";
// 'Algemeen'
$message .=  "<tr><td><hr/></td><td><hr/></td></tr>";
$message .=  "<tr><td><strong>Algemene rating van de site:</strong> </td><td>" . $algemene_rating . "</td></tr>";
$message .=  "<tr><td><strong>Gevonden wat hij/zij zocht: </td><td>" . $gevonden_wat_u_zocht . "</td></tr>";
$message .=  "<tr><td><strong>Gaat de klant bestellen?</strong> </td><td>" . $gaat_u_bestellen . "</td></tr>";
$message .=  "<tr><td><strong>De klant zocht naar:</strong> </td><td>" . $wat_zocht_u . "</td></tr>";
$message .=  "<tr><td><strong>Feedback om niet te bestellen:</strong> </td><td>" . $feedback_motivatie . "</td></tr>";
// 'Fout'
$message .=  "<tr><td><hr/></td><td><hr/></td></tr>";
$message .=  "<tr><td><strong>Fout op de website?</strong> </td><td>" . $fout_op_website . "</td></tr>";
$message .=  "<tr><td><strong>Feedback over URL:</strong> </td><td>" . $url_van_foutpagina . "</td></tr>";
$message .=  "<tr><td><strong>URL correct:</strong> </td><td>" . $fouturl_op_website . "</td></tr>";
$message .=  "<tr><td><strong>Feedback op de fout:</strong> </td><td>" . $feedback_fout . "</td></tr>";
// 'Compliment'
$message .=  "<tr><td><hr/></td><td><hr/></td></tr>";
$message .=  "<tr><td><strong>Is er een compliment?</strong> </td><td>" . $compliment_over_website . "</td></tr>";
$message .=  "<tr><td><strong>De compliment:</strong> </td><td>" . $feedback_compliment . "</td></tr>";
// Collect 'Suggestie'
$message .=  "<tr><td><hr/> </td><td><hr/></td></tr>";
$message .=  "<tr><td><strong>Een suggestie?</strong> </td><td>" . $suggestie_geven . "</td></tr>";
$message .=  "<tr><td><strong>Onderwerp suggestie:</strong> </td><td>" . $suggestie_onderwerp_01 ."". $suggestie_onderwerp_02 ."". $suggestie_onderwerp_03 ."". $suggestie_onderwerp_04 ."". $suggestie_onderwerp_05 ."</td></tr>";
$message .=  "<tr><td><strong>De suggestie:</strong> </td><td>" . $feedback_suggestie . "</td></tr>";
// Collect 'Inhoud'
$message .=  "<tr><td><hr/></td><td><hr/></td></tr>";
$message .=  "<tr><td><strong>Feedback over de inhoud?</strong> </td><td>" . $feedback_over_inhoud . "</td></tr>";
$message .=  "<tr><td><strong>Feedback over URL:</strong> </td><td>" . $feedback_over_inhoud_url . "</td></tr>";
$message .=  "<tr><td><strong>URL correct:</strong> </td><td>" . $feedback_over_inhoud_url_klopt . "</td></tr>";
$message .=  "<tr><td><strong>Feedback onderwerp:</strong> </td><td>" . $inhoud_onderwerp_01 ."". $inhoud_onderwerp_02 ."". $inhoud_onderwerp_03 ."". $inhoud_onderwerp_04 ."</td></tr>";
$message .=  "<tr><td><strong>Feedback over inhoud:</strong> </td><td>" . $feedback_inhoud_tekst . "</td></tr>";
// Collect 'producten'
$message .=  "<tr><td><hr/></td><td><hr/></td></tr>";
$message .=  "<tr><td><strong>Feedback over producten?:</strong> </td><td>" . $feedback_producten . "</td></tr>";
$message .=  "<tr><td><strong>Feedback onderwerp:</strong> </td><td>" . $product_onderwerp_01 ."". $product_onderwerp_02 ."". $product_onderwerp_03 ."". $product_onderwerp_04 ."</td></tr>";
$message .=  "<tr><td><strong>Feedback over product:</strong> </td><td>" . $feedback_over_producten . "</td></tr>";
$message .=  "<tr><td><hr/></td><td><hr/></td></tr>";
$message .= "</table>";
$message .= "</body></html>"; 

// Gedeelte voor het verzenden van het bericht
$mailadres = "xx@mxx";
$message = str_replace ("<br>","\n", $message);
$headers .= "From: xx <xx@exx.nl>" . "\r\n";
mail("$mailadres", "Nieuw bericht via mailformulier", $message, $headers); 

?>
<!--Display a thankyou message in the callback -->
<div id="mail_response">
    <h3>Thank you <?php echo $name ?>!</h3><br />
    <p>I will answer your message soon as possible.</p><br /><br /><br />
    <h5>Message sent on: </h5>
    <p><?php  ?></p>
</div>    

3 个答案:

答案 0 :(得分:0)

if(isset($_POST['going_to_order']) && $_POST['going_to_order'] == 'Yes'){
    $going_to_order = 'xxx';
}

答案 1 :(得分:0)

我会将代码更改为:

$going_to_order = ! empty($_POST['going_to_order']) ? $_POST['going_to_order'] : 'xxx';

$message .= "<tr><td><strong>Is going to order?</strong> </td><td>"
    . htmlspecialchars($going_to_order) . "</td></tr>";

答案 2 :(得分:0)

你说

// if NOT empty, change the value
if(!empty($_POST['going_to_order'])){$going_to_order = 'xxx';}

什么时候应该说

// if it IS empty, change the value
if(empty($_POST['going_to_order'])){$going_to_order = 'xxx';}