redirect to URL via PHP -
this question has answer here:
i have form @ index.php takes in user input, includes , sends user input php file processing.
here code of index.php:
<?php if(isset($_get['q'])){ include_once "form.php"; exit(0); } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>search</title> </head> <body> <form method="get"> <input type="text" name="q" /> </form> </body> </html>
when 1 submits form goes http://mysite.com/?q=textuserentered
(if domain visited before) or http://mysite.com/index.php?q=textuserentered
(if index.php visited before)
how can go http://mysite.com/form?q=textuserentered
or http://mysite.com/index.php/form?q=textuserentered
while still passing form data form.php
i tried in beginning index.php , form.php, navigates url doesn't pass data form.php , instead goes 404 error page.
if(!empty($_get['q'])) { header("location: form?q=".rawurlencode($_get['q'])); exit; }
update:
i can't use action attribute because adding form.php value of action attribute make url http://mysite.com/form.php?q=userenteredtext
not http://mysite.com/form?q=userenteredtext
you can use curl post data form.php file , can redirect form.php display form submission message.
how post using curl:
if(!empty($_get['q'])) { $output_url = "http://www.yoursite.com/form.php"; $data = "q=$_get['q']"; ob_start(); $ch = curl_init ($output_url); curl_setopt ($ch, curlopt_verbose, 1); curl_setopt ($ch, curlopt_post, 1); curl_setopt ($ch, curlopt_postfields, $data); curl_exec ($ch); curl_close ($ch); $process_result = ob_get_contents(); ob_end_clean(); if ($process_result != '') { header("location: http://www.yoursite.com/form"); exit; } }
also, write mod_rewrite code in .htaccess redirect form.php page using keyword 'form'.
if want show 'q=userenteredtext' in url, can use below mentioned code.
header("location: http://www.yoursite.com/form?$data");
Comments
Post a Comment