草庐IT

php - 如果 (!empty($_POST)) 不工作

coder 2024-04-09 原文

我有一个 php 表单(要遵循的代码),带有一个提交按钮,该按钮运行 JSON-events.php 作为其 action(method = POST)。在 JSON 事件代码中,我正在使用 if (!empty($_POST)) 测试表单是否已提交。我的问题是 JSON 事件代码似乎无法识别 $_POST

这是表单端代码部分。

<div class="simple_overlay" id="searchform">
<form id="myform" class = "cols" action="json-events.php" method="post" >

    <h3>Search</h3>

    <p>
      <label>address/postcode *</label>
      <input type="address" id="searchaddress" />
    </p>
    <p>
    <label for="amount">maximum distance:</label>
    <input type="text" id="amount" value="5 miles" style=" border:0; color:#43a8c4; font-weight:bold;" />
    </p>

    <div id="slider-range-min" style="width:130px; margin-left:4px; margin-bottom:20px"></div>

    <p id="terms">
      <label>Category 1</label>
      <input name="cat1" type="checkbox" value="1" checked="checked"/>
    </p>
    <p id="terms">
      <label>Category 2</label>
      <input name="cat2" type="checkbox" value="1" checked="checked"/>
    </p>
      <p id="terms">
      <label>Category 3</label>
      <input name="cat3" type="checkbox" value="1" checked="checked"/>
    </p>
      <p id="terms">
      <label>Category 4</label>
      <input name="cat4" type="checkbox" value="1" checked="checked"/>
    </p>
      <p id="terms">
      <label>Category 5</label>
      <input  name="cat5" type="checkbox" value="1" checked="checked"/>
    </p>
      <p id="terms">
      <label>Category 6</label>
      <input name="cat6" type="checkbox" value="1" checked="checked"/>
    </p>
      <p id="terms">
      <label>Category 7</label>
      <input name="cat7" type="checkbox" value="1" checked="checked"/>
    </p>
      <p id="terms">
      <label>Category 8</label>
      <input  name="cat8" type="checkbox" value="1" checked="checked"/>
    </p>
      <p id="terms">
      <label>Category 9</label>
      <input name="cat9" type="checkbox" value="1" checked="checked"/>
    </p>
      <p id="terms">
      <label>Category 10</label>
      <input name="cat10" type="checkbox" value="1" checked="checked"/>
    </p>
    <p>
      <input type="hidden" name="searchlat" id="searchlat"/>
    </p>
    <p>
      <input type="hidden" name="searchlong" id="searchlong" />
    </p>

    <input type="submit" name="search" id="search"/>
    <input type="button" value="Check Address" onclick="codeAddressSearch()"/>
    <button type="reset">Reset</button>

</form>
</div>

这是 JSON 的顶部部分...

if (!empty($_POST)) {

非常感谢任何帮助

最佳答案

如果 $_POST 没有任何值(空数组),

empty() 将起作用,但在您没有提交的情况下任何值仍然会得到如下所示的数组,并且它不是空的:

    Array
    (
        [searchlat] => 
        [searchlong] => 
        [search] => Submit Query
    )

empty() will return true only if $_POST returns

Array (

)

但这不会发生,因为每个表单都会有一个 Sumbit 按钮。

就用

if($_POST) {
//php code
}

它将解决您的问题。

访问 http://php.net/manual/en/function.empty.php 了解有关 empty() 的更多信息

关于php - 如果 (!empty($_POST)) 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12528524/

有关php - 如果 (!empty($_POST)) 不工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  4. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  5. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  6. ruby-on-rails - rails : How to make a form post to another controller action - 2

    我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak

  7. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  8. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  9. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  10. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

随机推荐