I have this http://localhost/resume/Template/index.html?msg=Message%20Sent%20Successfully
how do I extract the message "Message Sent Successfully and display it in this form
<form action="send_form_email.php" name="contactForm" method="post">
//I want to display the message here
<h4>E-mail</h4>
<div class="border-stripes">
<input type="email" class="textfield" name="email" placeholder="Your e-mail address" />
</div>
<h4>Message</h4>
<div class="border-stripes">
<textarea class="textarea" name="message" rows="3" placeholder="Your message"></textarea>
</div>
<br />
<br />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
这应该回显GET变量:
<?php echo urldecode($_GET['msg']); ?>
If you need to do it in php (note that there is no security check for XSS)
<form action="send_form_email.php" name = "contactForm" method="post">
<?php echo urldecode($_GET['msg']); ?>
<h4>E-mail</h4>
<div class="border-stripes"><input type="email" class="textfield" name="email" placeholder="Your e-mail address" /></div>
<h4>Message</h4>
<div class="border-stripes"><textarea class="textarea" name="message" rows="3" placeholder="Your message"></textarea></div><br /><br />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
else in js
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var msg = getParameterByName('msg');
$( "form" ).prepend( "<p>"+msg+"</p>" );
The function getParameterByName
is taken from here
If you want to do this in javascript then you can try to parse query string:
var query = (function() {
function decode(string) {
return decodeURIComponent(string.replace(/\+/g, " "));
}
var result = {};
if (location.search) {
location.search.substring(1).split('&').forEach(function(pair) {
pair = pair.split('=');
result[decode(pair[0])] = decode(pair[1]);
});
}
return result;
})();
$('form[name=contactForm]').prepend('<p>' + query['msg'] + '</p>');
Javascript:
I have write down the function for same using that you can access any parameter of URL.
Example:
function getParamValue(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(location.href);
if (results == null)
return "";
else
return decodeURI(results[1]);
}
alert(getParamValue('param'));
Your solution: http://jsfiddle.net/tb8cetLy/1/
I guess you are doing something like this in send_form_email.php
// process form data
// redirect to index.html with $_GET['msg'] variable
If you want pretty URLs , use a function like this below, include it on top of your common file
session_start();
function flash_set($k,$v)
{
$_SESSION['flash'][$k] = $v;
}
function flash_get($k)
{
if(isset($_SESSION['flash'][$k]))
{
$msg = $_SESSION['flash'][$k];
unset($_SESSION['flash'][$k]);
return $msg;
}
return '';
}
change the send_form_email.php
to redirect without $_GET
parameters, after you process your form put this,
// process form data
flash_set('form','Message Sent Successfully');
// redirect here
Now, use this in your form like,
<form action="send_form_email.php" name="contactForm" method="post">
<?php echo flash_get('form')?> // I want to display the message here
<h4>E-mail</h4>
The flash message will only show up single time after being redirected, if user refreshes the page, it disappears!!
In HTML/PHP only, you would use urldecode($_GET['msg'])
You can also do it with javascript with a function like :
function getQuerystring(key, default_) {
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}
This function will allow you to get msg with var msg = getQuerystring("msg");
and you can mix it with unescape functions ( http://www.w3schools.com/jsref/jsref_unescape.asp ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.