如何使AJAX发布到多个PHP文件?

时间:2018-08-27 21:23:08

标签: php jquery ajax

我在ajax中有一个数组变量,我想将其传输/发布到两个单独的php文件fetch.php和index.php。我下面的脚本用于捕获变化中的下拉选择并将其发布到fetch.php,但是当单击提交按钮时,我也想使用捕获的选择并在index.php中对其进行一些操作。是否可以将此变量(自动类型)发回到index.php,如果可以,怎么办?还是我需要采用其他方式?

index.php

package com.grrigore.dynamicgridview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    private int numberOfCurrentImages = 0;
    private int[] images = {
            R.drawable.sample_0,
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7,
            R.drawable.sample_0,
            R.drawable.sample_1,
            R.drawable.sample_2
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final LinearLayout linearLayout = findViewById(R.id.linearLayout);

        SeekBar seekBar = findViewById(R.id.seekBar);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                for (int i = numberOfCurrentImages; i < progress; i++) {
                    ImageView imageView = new ImageView(MainActivity.this);
                    imageView.setImageResource(images[numberOfCurrentImages]);
                    linearLayout.addView(imageView);
                    numberOfCurrentImages++;
                }

                if (progress == 0 && linearLayout.getChildCount() > 1) {
                    linearLayout.removeViews(1, numberOfCurrentImages);
                    numberOfCurrentImages = 0;
                }

                if (progress < numberOfCurrentImages) {
                    for (int i = numberOfCurrentImages; i > progress; i--) {
                        linearLayout.removeViewAt(i);
                    }
                    numberOfCurrentImages = progress;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

scripts.js

<form id="filters" method="post">
   <select name="automobiles" id="automobiles">
      <option value="all">All</option>
      <option value="car">Car</option>
      <option value="suv">SUV</option>
      <option value="van">Minivan</option>
   </select>
   <input type="submit" name="search" id="search" value="search">
</form>

fetch.php

$(document).ready(function(){
        $('#automobiles').on('change', function(){
            var autoVal = $(this).val();
            if(autoVal){                
                $.ajax({
                    method:'POST',
                    url: "fetch.php",           
                    data:{autotypes:autoVal},               
                })              
                .done(function(data){
                    $("#results").html(data);       
                });
            } 
        });     
});

1 个答案:

答案 0 :(得分:0)

首先,我要在您的ajax调用中添加一个success,并成功地从index.php提交表单;

    $.ajax({
        type: "POST",
        url: "/fetch.php",
        success: function(data){
            $("#filters").submit();
        }
    });