使用PHP从输入值构建URL

时间:2017-10-23 16:23:22

标签: php html input

Site1: http://www.sitea.com

Site2: http://www.siteb.com

我想使用 Site1 中的输入字段值<a href="">元素中构建一个指向 Site2的网址。这将用于访问其他网站的搜索功能。

示例: 输入值=测试搜索

网址 http://www.siteb.com/#!q=test-search?parameter1

我的尝试看起来像这样:

    <form action="index.php" method="GET">
        <input type="search" name="search" />
    </form>
    <a href="https://www.siteb.com/#!q=<?php echo $_GET['search']; ?>?parameter1">Search</a>

我正在查看不同的tuttorials,但找不到一个会使用PHP来传递输入字段的值并构建URL。

请注意:我仅限于PHP,完全没有JS。

AMP示例: - link to example

<form method="GET"
  class="p2"
  action="/components/amp-form/submit-form"
  target="_top">
  <div class="ampstart-input inline-block relative mb3">
    <input type="search"
      placeholder="Search..."
      name="googlesearch">
  </div>
  <input type="submit"
    value="OK"
    class="ampstart-btn caps">
</form>

2 个答案:

答案 0 :(得分:1)

在你的主index.php文件中你将有这个

<form method="GET"
      class="p2"
      action="submit-folder"
      target="_top">
    <div class="ampstart-input inline-block relative mb3">
        <input type="search"
               placeholder="Search..."
               name="googlesearch">
    </div>
    <input type="submit"
           value="OK"
           class="ampstart-btn caps">
</form>

在一个名为“submit-folder”的文件夹里面,yo中的另一个index.php文件会有这个

  <?php
if (isset($_GET["googlesearch"])) {
    header("Location: https://www.siteb.com/#!q=" . $_GET["googlesearch"] . "?parameter1");
}

这是接收请求然后自动重定向的文件,您可以在单个文件中完成此操作,但

<?php
if (isset($_GET["googlesearch"])) {
    header("Location: https://www.siteb.com/#!q=" . $_GET["googlesearch"] . "?parameter1");
    exit;
}
?>
<form method="GET"
      class="p2"
      action="<?php echo $_SERVER['PHP_SELF']; ?>"
      target="_top">
    <div class="ampstart-input inline-block relative mb3">
        <input type="search"
               placeholder="Search..."
               name="googlesearch">
    </div>
    <input type="submit"
           value="OK"
           class="ampstart-btn caps">
</form>

答案 1 :(得分:1)

您必须在动作脚本中进行重定向:

    <!-- my profile.html view -->
    <ion-row>
              <ion-col col-4>
                Tipo de socio
              </ion-col>
              <ion-col col-8>
                <span>{{ user.id }}</span><!-- cannot read id property of a null -->
              </ion-col>
            </ion-row>
            <ion-row>
              <ion-col col-4>
                Tipo de socio
              </ion-col>
              <ion-col col-8>
                <span>{{ user|json }}</span><!-- the data is returned -->
              </ion-col>
            </ion-row>
    // My profile.ts ( where I'm loading the view and calling the user services )

    import { User, UserService } from '../../app/models/user';
    @Component({
      selector: 'page-profile',
      templateUrl: 'profile.html'
    })
    export class ProfilePage {
      title = 'My Profile';
      user: User = null;
      private user_id: number = 0;
      constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public alertCtrl: AlertController,
        public modalCtrl: ModalController,
        public loadingCtrl: LoadingController,
        public userService: UserService) {
        this.user_id = this.navParams.get('user_id'); // this value is working fine!
        if ( this.user_id > 0 ) {
          this.userService.get( this.user_id ).then( user => this.user = user );
        }
      }
      ionViewDidLoad() { }
    }
    // My user.ts ( where I've my services )

    @Injectable()
    export class User {
      id: number;
      name: string;
      email: string;
      ic: number;
      birth_date: string;
      home_phone: number;
      phone: number;
      is_active: boolean;
      is_admin: boolean;
      from_social_account: boolean;
      is_superuser: boolean;
      status: boolean;
      created_at: any;
      updated_at: any;
      groups: any;
      permissions: any;
      addresses: any;
      all_permissions: any;
      auth_token: string;
      is_customer: boolean;
      is_partner: boolean;
      last_login: string;
      profile: any;
      requests: any;
      skills: any;
      social_providers: any;
      wallets: any;
      errors: any = null;
      birth_date_no_formatted: string;
      main_address: any;
    }
    @Injectable()
    export class UserService {
      private headers: Headers = new Headers(Constant.DEFAULT_HEADERS);
      private url: string = `${Constant.API_HOST}${Constant.API_PREFIX}${Constant.API_V1}`;
      constructor(
        private http: Http) { }
      get( _id: number ): Promise<User> {
        let url = `${this.url}users/${_id}/`;
        return this.http.get(url, { headers: this.headers })
          .toPromise().then( response => response.json() as User ).catch(this.handleError);
      }
      
      private handleError(error: any): any {
        console.log( JSON.stringify( error ) );
        return {errors: StaticMethods.formatSubmitErrors( error.json() ) };
      }
    }