user3534166 user3534166 - 19 days ago 5
PHP Question

Dynamic query string php

I am building a job board on our Wordpress website and each job has an Apply Now but which redirects to a form. Rather than build a new form for each job, I am trying to pass the Job Title (which is the page title) to the form but cannot get it to work.

So the button is located on this page for example:
http://mywebsite.com/careers/engineering-job

Page title is Engineering Job

The code for the button I'm trying is this..

<button onclick="window.location.href='http://mywebsite.com/careers/apply/?job_name=". $_GET["$title"] ."'">APPLY NOW</button>


Any idea how I can get the title into the query string so it would pass to my form as

http://mywebsite.com/careers/apply/?job_name=Engineering Job

Answer

You could use WordPress' wp_title() function and then urlencode() to properly encode all special characters.

<button onclick="window.location.href='http://mywebsite.com/careers/apply/?job_name=<?php echo urlencode(wp_title(' ', FALSE)); ?>'">APPLY NOW</button>
Comments