简体   繁体   English

用PHP提交jQuery表单

[英]Jquery form submit with PHP

I need help with what I thought would be a simple form submit problem with Jquery, but am running into issues. 我需要有关Jquery的简单表单提交问题的帮助,但遇到了问题。

I have a form, #listing_step_1, in which the user first selects a category from a dropdown. 我有一个表单#listing_step_1,用户首先从下拉列表中选择一个类别。 When a category has been selected, if the user clicks "next", the subcategory dropdown is revealed. 选择类别后,如果用户单击“下一步”,则显示子类别下拉列表。 When both a category and subcategory is selected, clicking next will take them to step 2 of the process using a redirect. 同时选择类别和子类别时,单击“下一步”将使用重定向将它们带入流程的步骤2。 On submission, the form is reloading the same page using $_SERVER['PHP_SELF']. 提交后,表单将使用$ _SERVER ['PHP_SELF']重新加载同一页面。

I want to autosubmit the form so clicking on next is not required to reveal the subcategories. 我想自动提交表单,因此不需要单击下一步即可显示子类别。 I'm trying to do this on an onchange event using JQuery, but the following code does not work. 我正在尝试使用JQuery在onchange事件上执行此操作,但是以下代码不起作用。

$('#item_category').change(function(){
   $('#listing_step_1').submit();
});

I would appreciate any help with this as I am new to form submission using JQuery and AJAX. 我对此表示感谢,因为我是第一次使用JQuery和AJAX提交表单。 Manually, it's working fine - ie, pressing the page button and with the PHP page refresh. 手动,它可以正常工作-即,按下页面按钮并刷新PHP页面。 I thought by simply submitting the form in the onchange instance it would reload and execute the required actions, but this does not appear to be the case. 我认为,只需在onchange实例中提交表单,它就会重新加载并执行所需的操作,但是事实并非如此。

Form code: 表单代码:

<form method="post" name="listing_step_1" id="listing_step_1" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" <?php if($errormessage != ""){ echo ' style="margin-top: 30px"';}?>>
  <div class="formField fullwidth">
    <label for="item_category">Category *</label>
    <select id="item_category" name="item_category">
      <option value="0">Select category...</option>
      <?php $cd = new CategoryListing("<option_credits>", "</option>"); ?>
    </select>
  </div>
  <div class="formField fullwidth">
    <label for="item_subcategory">Subcategory*</label>
    <?php if($dropdownlist != "") { ?>
    <select id="item_subcategory" name="item_subcategory">
      <?php echo $dropdownlist; ?>
    </select>
    <?php } else { ?>
    <select id="item_subcategory" name="item_subcategory" disabled="true">
      <option value="0">Select subcategory...</option>
    </select>
    <?php } ?>
  </div>
  <input type="submit" value="Next" id="submit" name="submit" style="width: 100px;" />
</form>

Form Submission: 表格提交:

if(isset($_POST['submit'])){
 if($_POST['item_category'] != 0){
  $dropdownlist = CategoryListing::getSubCategoryDropdown($_POST['item_category']);
 } 
 else {
  $errormessage = "Please select a category";
 }
}

Jquery: jQuery:

$(document).ready(function(){
       $('#item_category').change(function(){
      this.form.submit();
     });
});

Try this instead: 尝试以下方法:

$(function() {
  $('#item_category').change(function(){
     this.form.submit();
  });
});

in your description, you say: 在您的描述中,您说:

I have a form, #listing_stage_1, in which the user first selects a c... 我有一个表单#listing_stage_1,用户首先在其中选择一个c ...

then in code you say: 然后在代码中你说:

$('#item_category').change(function(){
   $('#listing_step_1').submit();
});

I imagine you are just using the wrong selector... change your code to this: 我想您正在使用错误的选择器...将您的代码更改为此:

$('#item_category').change(function(){
   $('#listing_stage_1').submit();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM