Error Message Display - Form Data PHP Entry in Wordpress - javascript

Currently, we have a PHP Form that uses an entry for a redirect to Private sites within our main website, of which code we use the following:
<?php
//Turn the content from text box into variable
$page=$_POST['text'];
if($_POST['text']=='sit') {
//set up a redirect to that page
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://example.com/index.php/private/sit\">";
}
else if($_POST['text']=='pony') {
//set up a redirect to that page
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://example.com/wp-content/uploads/sites/2/client/pony/index.html\">";
}
Originally, we had implemented a final line that if someone inserted something wrong, it would refresh the page:
else {
echo "<meta http-equiv=\"refresh\"content=\"0;URL=http://example.com\">";
}
?>
How to show a message that says "Error" in Bold and Red next to the form?

You can send a parameter in the URL you use to refresh. And then check on the parameter after the URL refresh by Javascript and add the error.
In this case you will echo something like this instead
echo "<meta http-equiv=\"refresh\"content=\"0;URL=http://example.com?error=1\">";
Then in your html, use Javascript or PHP to check on the URL param and render error with style.

Related

why wont javascript show the alert?

beginner here.
I do not understand why no alert pops up when I use the header statement,but the alert pops up just fine when I remove the header location line. Really confused :(
<?php
include "connect.php";
session_start();
// if(isset($_SESSION['user_id'])
if(isset($_POST['register'])){
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
$name=$_POST["name"];
$roll=$_POST["roll"];
$year=$_POST["year"];
$pass=$_POST["register_password"];
$sql="INSERT INTO students (roll_id,name,year,password) VALUES
('$roll','$name','$year','$pass')";
$query=mysqli_query($conn,$sql);
if(!$query)
{
echo "Not Inserted" . mysqli_error($conn);
}
header("location:index.php?registered=true");
}
?>
The problem is header() needs to be sent before ANY output in order to redirect the page to another URL. At that time, you're already echoing things out, so redirects via headers wont work.
In a case such as this, (Where you want to popup a message and then redirect), you should use a javascript redirect:
echo '<script language="javascript">';
echo 'window.location.replace("/index.php?registered=true");';
echo '</script>';
This will output your popup message. After the user hits OK, the javascript redirect code will run, and redirect the page to /index.php?registered=true.
Additionally, you can add the alert box to the page that you're redirecting too. See this example index.php file:
<?php
if (isset($_GET['registered'])) {
echo '<script>alert("You have registered!");</script>';
}
//Continue with rest of page.
If you go this route, don't include ANY output (No echo) on the register page, so that header()s are your only output. This would ideally be a better user experience, as they don't have a white popup page as they're clicking a box that says alert("message successfully sent").
This is because the location header redirects to a different page before the JavaScript alert is shown.
Add the alert to the destination page and it will be shown there.
Echo it like this:
echo "<script>alert('message sent'); window.location.href='url';</script>";

Get Image captcha result after form submit from a site with login

I am a complete newbie to Web development.
There is a website with link such as https://example/login.asp with three fields of id, password and an Image Captcha. I am creating an API where the image captcha is automatically detected or taken from the session cookie after the submit button so the user doesn't need to type it.
<?php
echo '';
if(isset($POST['submit'])){
echo '<pre>';
echo print_r($_SESSION['imgCaptcha']);
echo '</pre>';
}
?>
<script type="text/javascript">
$(document).ready(function (response) {
console.log(response.getAttribute("imgCaptcha"));
})
</script>
I am getting a completely blank page. What should I do and where am I going wrong.

User Name display as after login

I want to display logging user name. But below given code working perfect but the same page only working. I want home.php to display.
Login.php
if($check_user>0)
{
$_SESSION['user_name']=$username;
echo "<script>window.open('home.php','_self')</script>";
}
Home page coding
<?php echo($_SESSION['username']); ?>
But this code undefined index error showing.
How can call session username in home page
You have undefined index because you setting $_SESSION['user_name'] and try to read $_SESSION['username']. Change your home page code to: <?php echo($_SESSION['user_name']); ?>

href call in javascript

I have a problem with using , during
click of the link, I need to update a field in the database and redirect to another page after.
I have this code:
<a href="#" onclick="<?php
$sql="UPDATE MyDB.mytable SET Date = '".date("Y-m-d H:i:s")."'
WHERE ID='" . $id . "'";
if (!mysql_query($sql)) ///Cannot query
{
$logger->error(mysql_error());
}
if ($sql)
{
$logger->debug('OK');
}
else
{
$logger->debug( 'NOt OK');
}
?>"> </a>
After the php end tag '?>' can I add my path to be directed to? like:
<a href="#" onclick="<?php
$sql="UPDATE MyDB.mytable SET Date = '".date("Y-m-d H:i:s")."'
WHERE ID='" . $id . "'";
if (!mysql_query($sql)) ///Cannot query
{
$logger->error(mysql_error());
}
if ($sql)
{
$logger->debug('OK');
}
else
{
$logger->debug( 'NOt OK');
}
?> ../index.php"></a>
Is that even possible?
What is the right thing to do it?
Thanks a lot!
this is not the right way.
There can be multiple ways you could take to do this. But I'd suggest you to place the DB update code in the target page (that I assume you mentioned as index.php). If you only want to trigger the DB update code on clicking of the link, use a page in middle to redirect the flow.
So, your page flow will be:
Current Page (Link Clicked, simple href to middleman.php) ==> middleman.php (just run the DB update code here and use header Location syntax to index.php) ==> index.php
codes:
page in which you have the link
source.php
<.... html contents ....>
<a href='middleman.php'>Visit the page</a>
<.... more html contents ....>
middleman.php
<?php  
$sql="UPDATE MyDB.mytable SET Date = '".date("Y-m-d H:i:s")."' WHERE ID='" . $id . "'";
if (!mysql_query($sql)) ///Cannot query
{
$logger->error(mysql_error());
}
if ($sql)
{
 $logger->debug('OK');
}
else
{
 $logger->debug( 'NOt OK');
}
header("Location: index.php"); //redirects to index.php
?>
index.php
do whatever you want
When a page is rendered, php code will run once. Whenever you see a webpage, it's only html, always, with no live access to the php code. So, you cannot execute php blocks directly from for example a javascript event. In your case the sql query would execute once, when you load the page.
kishu27 just posted one of the proper ways to do it, and the best option for you in this case. If you only wanted to update the database, without being redirected to another page, an ajax call to a php page with the database code would be a good alternative.
Using location.pathname

how to redirect the user to the referer page

I have this part of my script
<?php
else:
?>
<script>
alert("You are not allowed to edit this CV!");
</script>
<?php
echo '<meta http-equiv="refresh" content="1"; url="'.$the_class->settings[0]['DomainName'].'myresume.php"';
endif;
?>
the objective is, after the alert box popped-out and the "ok" button was clicked,
the user should be redirected to http://www.mydomain.com/myresume.php
now the problem is, before the page loads the redirection, the alert box keeps popping out without reaching the destination page at all...how to fix this ?
You can do something like this if you want to always send them back to http://www.mydomain.com/myresume.php:
<script>
alert("You are not allowed to edit this CV!");
window.location.href = 'http://www.mydomain.com/myresume.php';
</script>
You can do something like this if you instead want to send them back to whatever page they were on previously:
<script>
alert("You are not allowed to edit this CV!");
history.back();
</script>
the problem seems to be that you are trying to set a header after sending content.
You can try using the header function. If that does not work, you can simply remove the alert.
If you need to display the alert you can put a window.location in the script tag and remove the php part.
You can also experiment by turning the output buffering off using ob_implicit_flush.
How about sending them back using javascript:
<?php
else:
?>
<script>
alert("You are not allowed to edit this CV!");
document.location =
<?php
echo '"'.$the_class->settings[0]['DomainName'].'myresume.php"';
?>
;
</script>
<?php
endif;
?>

Categories

Resources