PHP - how to set up URL after sending form? - php

I have a website (www.website.com) and on the homepage is a form, where the user set up a city. After sending the form, I am trying to have the following URL address:
www.website.com/city-name
But I am lost a bit here, because I am not sure, how to set up the form (GET or POST is better for this need?), and .htaccess.
Could anyone help with this? Thank you

After the form is submitted you could redirect your users via PHP redirect, or am i missing something?
header('Location: http://www.website.com/city-name');
But be careful, this can only be used before you send any output!
PHP: header()

You could let the submit button run a javascript function.
In that javascript function, use
var cityName = document.getElementById("the textarea id").value
to get the value the user typed for the city name.
Then redirect to the right page:
window.location='http://www.website.com/' + cityName

Related

how to get hashed url from ajax response of a form submit?

I'm using an exchange plugin on wordpress, and I want to make the whole process of bidding ajax; so when submitting one of the forms the page goes to a blank page which show something like this:
{"status":"success","response":"","status_code":0,"status_text":"Bid successfully created","url":"http:\/\/example.com\/step2\/hst_HH8yhuadhHFKlgij94fE4fGaw59HUmsL94j\/","account1_error":0,"account1_error_text":"","account2_error":0,"account2_error_text":"","summ1_error":0,"summ1_error_text":"","summ2_error":0,"summ2_error_text":"","summ1c_error":0,"summ1c_error_text":"","summ2c_error":0,"summ2c_error_text":"","cf":[],"cf_er":[],"cfc":[],"cfc_er":[]}
which seems like ajax response (or whatever, I don't know exactly)
But here's the thing, when I enter the url in this code in the address bar, it goes exactly wehere I wanted to go, so How can I somehow get this hashed url right after form submit and load the content of that page in current page using jQuery?
or just when this page loads immediately it redirects to the url in it?
or at least can you help me somehow or just put me on the right direction?
Capture the response to a variable:
$json = json_decode(input);
then access the URL through that:
echo $json.url
This should be done in the script where your form is POST to.
EDIT, regarding the POST to /ajax.html, that file should contain something similar to this tutorial:
<?php
// If loop is only entered if $_POST["name"] is not empty.
// $_POST[value] comes from the POST parameters, e.g., for example.com/ajax.html?name=Nick ...
if( $_POST["name"] ) {
$name = $_POST['name']; // ... then `echo $name` will print "Nick"
echo "Welcome ". $name;
}
?>

PHP - Smarty Variables unset after header redirect

I have a php file (form.php) that uses a template (form.htm) to display a form. The template file's form action is form.php. If the form is incomplete when submitted, I want to redirect back to the form WITHOUT losing information that the user has already input.
I'm setting smarty variables using t->assign('varname', $_POST['var']) and setting the form values to these variables ( value="{$varname}" ), however when redirected using php's header() function, these Smarty variables are lost (which I've confirmed using Smarty {debug}).
Does anyone have any idea of how to make it so that these variables aren't unset when using a header redirect? Thanks!
Smarty is only template engine, so you cannot do here more than in PHP.
But in your case you said you want to make header redirect after sending form (to be honest I have no idea why you want to do it. In normal case you simple set action in form and in controller you simple check data and if they are valid you do what you want - for example send email and if they are not valid you simple show the form again - that's it).
However if you really need to do this that way, what you can do is to use session data and save all data from post to session, make redirection and use that from session to display in Smarty.
So in PHP you can do:
$_SESSION['post_data'] = $_POST;
// now you make redirection
And in file you handle redirection you can simple do:
$smarty->assign('post_data',$SESSION['post_data']);
unset($SESSION['post_data']);
And in template file you can then use:
{$post_data.var}
EDIT
But as I said normally you do it this way in PHP file:
$isValid = false;
if (isset($_POST['submit']) {
$isValid = validateData($_POST);
if ($isValid) {
// do something here - for example send email
}
else {
$smarty->assign('error', 'There were errors in your form. Try again');
}
}
$smarty->assign('is_valid',$isValid);
And in Smarty you do:
{if $isValid}
form was sent
{else}
{if isset($error)}{$error}{/if}
you display form here
{/if}
Don't redirect the user. Just redraw the form. In general, this HTTP workflow should be used when dealing with forms:
GET /form.php - Display the form to the user
POST /form.php - Process the form input
If the form input is invalid, send back form.php
If the form input is valid, save to the database, now redirect the user
So basically you have:
GET
POST (invalid input from the user)
POST (valid input from the user)
REDIRECT

Trouble Validating Required Text in textarea with PHP/MySQL

I have a textarea that I am trying to validate whether someone has filled out or not before submitted the form. I'm doing all of my validation server side with PHP.
I had two thoughts. Either:
1) submit the form to itself and have the fields validate themselves on the same page and then redirect to the page where the form would update my database. The problem I had was that since it was posting to itself, once the page redirected, I couldn't carry out using the POST values because they were no longer valid.
2) So I figured I'd just do the validation on the second page (the page that the form initially is set to post to). Here's my code for that page, where 'description' is the name of the textarea:
session_start();
if(!$_POST['description']){
echo "<script>alert(\"Please fill out all fields\");</script>";
header("location:post_job.php");
}
And then it redirects back to the page where the form is being filled. I get this error though:
Warning: Cannot modify header information - headers already sent by (output started at siteinfo/site/checkjob.php:4) in siteinfo/site/checkjob.php on line 5
Line five is the header line (so line four is obviously the echo line). I don't understand why it would create that error since I haven't sent a header yet. Any help would be greatly appreciated. Thanks!
Try this
session_start();
if(!$_POST['description']){
echo "<script>alert(\"Please fill out all fields\");";
echo "window.location.href='post_job.php';";
echo "</script>";
}
Although I must add the way you intend to show your message isn't in a good manner. Validating through Ajax or javascript before the form is submitted is a good route to follow. Keep this way as a backup plan in case javascript is disabled on the client's system.
The echo with the tag in it sends output to the browser. Use your preferred JavaScript way of redirecting within the script tag instead.

How to hide php actions from history?

Is it possible to tell the browser that he should not remember the action scripts page?
So when you delete a headline for example. The get action will be ?d=ID where ID is the id of the headline. After removing the headline go to the page without the get variable in the url (the header part).
<?php
if(isset($_GET['d']) && preg_match('{^[0-9]{1,3}$}',$_GET['d'])){
$hid = $_GET['d'];
$deletesql = "DELETE FROM headlines WHERE hid = $hid";
mysql_query($deletesql);
header('Location: panel.php');} ?>
But now you browser history shows the link panel.php?d=23
Can prevent the browser from remembering the page? Maybe a 303 header?
You should use POST instead of GET for this. That way the browser will prompt the user if he wants to send the information again. (typically the id argument)
Make the ID hidden in the form and then get it in the form action part using $_POST and then do the delete action and redirect once delete is successful to avoid user from re posting the form.

Help needed in dealing with URL

I am developing an app using twitter API,in which any user can enter his twitter username in textbox and get his followers. On home.php , I am using jquery. on click event of button, I am calling getinfo.php and passing textbox value along with it. This is the case so far.
What I want to do?
Right now my url is:
http://www.example.com this redirects to
http://www.example.com/home.php
Now I wish that my URL should also work like this:
http://www.example.com/username
means when user will type sitename and his username he should get his results.
As I am trying jquery first time, I am not getting how can I pass value of URL (username in this case) to getinfo.php directly?
or any other solution?
Can you guys help me with this? please ask for any details.
Your help will be greatly appreciated.
Thank you in advance.
Here is a posted code on demand
the click event in jquery:
$(document).ready(function(){
$("#btonsubmit").bind('click', function(){
$.get('/getinfo.php?tnm='+$('#tnm').val(), '', function(data){
$('#get_data').html(data);
});
}
)};
// here tnm is name of textbox. and btonsubmit for button
You can do this like below.
step 1: update your .htaccess file . add one line
ErrorDocument 404 /notfound.php
This will redirect your user to /notfound.php
step 2:
on notfound.php,
get any how URL that user has called like http://yoursite.com/username
below line might help you to get that URL, not sure...
$url = $_SERVER['HTTP_REFERRER'];
extract username from this url.
check whether this userName exist?
if exist process getInfo.php with that
username..
else redirect to notfound.html..
I am not sure as this will work or not?? the main Question is whether you will get URL with $_SERVER['HTTP_REFERRER'];

Categories