dongpao5261 2014-04-03 11:44
浏览 25
已采纳

PHP根据id设置表单字段的值

My WordPress contact form is generated like this by duplicating this line until the form is complete. The form has many fields which are determined in the WP dashboard.

 <input type="text" name="<?php echo "umbheadfld_".$field["field_name"]; ?>" id="<?php echo "umbheadfld_".$field["field_name"]; ?>" data-required="<?php echo $field["required"]; ?>" data-fieldtype="<?php echo $field["field_type"]; ?>">

However, the theme does not allow an option to set a value for the fields. I am trying to set my value as the name of the WordPress post.

Using PHP I can obtain the name of the WordPress post using this code

$title = get_the_title‎();

The first in this post code generates a field defined as id="umbheadfld_Subject" as I named the field "Subject" in the WP dashboard.

I need to ammend the above code to include the value = $title if id=umbheadfld_Subject.

Any ideas?

  • 写回答

1条回答 默认 最新

  • douhuireng4407 2014-04-03 11:58
    关注

    You can add a single line if statement using terniary operators.

    <?php echo ($field["field_name"] == "Subject" ? "value=\"" .$title."\"" : "") ?>
    

    When added to your first line:

     <input type="text" <?php echo ($field["field_name"] == "Subject" ? "value=\"" .$title."\"" : "") ?> name="<?php echo "umbheadfld_".$field["field_name"]; ?>" id="<?php echo "umbheadfld_".$field["field_name"]; ?>" data-required="<?php echo $field["required"]; ?>" data-fieldtype="<?php echo $field["field_type"]; ?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了