Hi I have this function in handling upload errors.
function error($error, $location, $seconds = 5)
{
header("Refresh: $seconds; URL=\"$location\"");
echo 'Error :' . $error . ' Please correct before proceeding.';
exit;
}
Since this is echo's the message, you know the message shows not in the same page and you need to hit browser back to return to the original content page.
What I wanted is the validation messages to be shown on the same page, so I tried changing the code to following.
function error($error)
{
$ErrMsg = 'Error :' . $error . ' Please correct before proceeding.';
}
And later in the form in a table I have something like following to call the massage.
<td><?php if(!empty($ErrMsg)) echo $ErrMsg; ?></td>
However this method doesn't seems to be giving me the solution I need, which is to print the validation message on the same page.
Can anyone help?
thanks.
You could use ajax to call the error function and output the message on the page.
Or you could set a $_POST, $_GET, or $_SESSION variable to pass the error message onto the next page.
Session example:
function error($error)
{
$_SESSION['ErrMsg'] = 'Error :' . $error . ' Please correct before proceeding.';
}
And later...
<td>
<?php if (!empty($_SESSION['ErrMsg'])) {
echo $_SESSION['ErrMsg'];
}?>
</td>
If the form posts to the same page it's on, you could try something like this:
function uploadValidation()
{
$errorReason = '';
// do validation here
// if something goes wrong $errorReason = 'reason for error'
if (!empty($errorReason)) {
return 'Error :' . $errorReason . ' Please correct before proceeding.';
} else {
// success stuff
// maybe return thanks message or redirect somewhere else?
return 'You upload real good!';
}
}
$validationMessage = uploadValidation();
?>
And later in the form in a table I have something like following to call the massage.
<td><?= $validationMessage ?></td>
Add { after your if... codes and just before echo and a closing } after your echo... codes.
It's actually pretty hard to tell what you are trying to achieve based on your description. But try this:
<?php
function error($error) {
return 'Error :' . $error . ' Please correct before proceeding.';
}
?>
And then:
<td><?php echo error('YOUR ERROR CODE') ?></td>
Related
I want to make a call of this php logic inside a html div but when passing it as a function the logic breaks since it does not send an error message in case of entering the pass wrong and its confirmation at the time of performing the password change.
<?php
require 'funcs/conexion.php';
require 'funcs/funcs.php';
$user_id = $mysqli->real_escape_string($_POST['user_id']);
$token = $mysqli->real_escape_string($_POST['token']);
$password = $mysqli->real_escape_string($_POST['password']);
$con_password = $mysqli->real_escape_string($_POST['con_password']);
if(validaPassword($password, $con_password))
{
$pass_hash = hashPassword($password);
if(cambiaPassword($pass_hash, $user_id, $token))
{
echo "Contraseña Modificada <br> <a href='index_alumnos.php' >Iniciar Sesion</a>";
} else {
echo "Error al modificar contraseña";
}
} else {
echo "Las contraseñas no coinciden <br> <a href='index_alumnos.php' >contacta a Academia</a>";
}
?>
If the echo happens before your actual div is drawn, the echo goes... right where it happens. Which isn't within your div.
One way of getting around this would be to put your error message into a variable and then deliver this variable into your div (whether it be through a return value, if it's a call, or some other means.)
Here's a simple example to illustrate this:
<?php
if(1 === 2) {
//great, 1 is 2
} else {
//oh no, an error
$someErrorLine = '1 is not 2';
} ?>
<h1>Hi</h1>
<div><?= $someErrorLine ?></div>
You could also check if the variable exists, something like if(isset($someErrorLine)) {} and echo the div with it, or put the div within your variable.
I have this little contact form manager and it displays an error message as an array. I need that every time the error message appears, the user goes down to an anchor in my page.
The code looks like this:
$errors = array();
if (filter_var($email, FILTER_VALIDATE_EMAIL)=== false){
$errors[] = 'Invalid email';
}
if (ctype_alpha($name)===false){
$errors[] = 'Invalid name';
}
if (ctype_alpha($phone)===true){
$errors[] = 'Invalid number';
}
Besides showing the message, how can I send the user to an anchor in the site?
The only way so far I got it to work was by using:
<script>
window.location = 'sent.php';
</script>
But I can't use that everytime the user gets an error, or can I?
Somehow I couldn't use the header tag with this array because the result for the array was an empty one, so I managed to do something like this at the index where the error message was posted.
<?php
if (empty($errors)===false) {
echo'<ul>';
foreach ($errors as $error) {
echo '<li style="color:red; font-weight:bolder;
text-decoration:none;">'
,$error, '</li>';
}
echo '</ul>';
echo '</br>';
?>
<script>
window.location = '#correoA';
</script>
<?php
} ?>
So this way I get the error message and the window location
I want to set a message for the user to see in php, but I'm having issues crossing controllers. Here was my first try:
if($revOutcome > 0){
$message = "<p>Review updated!</p>";
header('Location: /acme/accounts/index.php?action=seshLink');
exit;
}
And here was my second try:
if($revOutcome > 0){
header('Location: /acme/accounts/index.php?action=seshLink&message=Update was successful!');
exit;
}
I have an isset in the view that checks if $message is set, and if it is, echo what is displayed in $message. But for some reason, it's not displaying. Here is the code for the view:
<?php
if (isset($message)) {
echo $message;
}
?>
And here is the switch case statement seshLink:
case 'seshLink':
$userId = $clientData['clientId'];
$revData = getCliRev($userId);
if(!$revData){
$message = "<p>No reviews here yet. Write your first one today!</p>";
include '../view/admin.php';
exit;
}
else {
$RevDisplay = buildAdminReviewDisplay($revData);
}
include '../view/admin.php';
break;
I really don't know why $message isn't displaying.
Because you are making a request call (parameters through url) which means that you need to get your variables using $_GET array like
...
if (isset($_GET["message"]))
...
What's wrong with this preg_match() usage? I want to check steam lobby link and if it's matching then write to database. If not, just echo the error. I am doing this through ajax. Is it better to do this with ajax or $_SERVER["REQUEST_METHOD"] == "POST"?
<?php
require("../includes/config.php");
$lobby = "steam://joinlobby/730/109775243427128868/76561198254260308";
if (!preg_match("%^((steam?:)+(/joinlobby\/730\/)+([0-9]{17,25}\/.?)+([0-9]{17,25})/$)%i", $lobby)) {
echo "Lobby link isn't formatted correctly.";
}
else {
$rank = "Golden";
$mic = "No";
try {
$stmt=$db->prepare("INSERT INTO created_lobby (lobby_link, current_rank, have_mic) VALUES (:lobby_link, '$rank', '$mic')");
$stmt->execute(array(
':input_link' => $_POST['lobbyLink']
));
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
?>
My Problem:
When I execute this code, it will give me false.
Thank you for help.
This works:
$lobby = "steam://joinlobby/730/109775243427128868/76561198254260308";
if (!preg_match("%^(steam?:)+(//joinlobby/730/)+([0-9]{17,25}/.?)+([0-9]{17,25}$)%i", $lobby)) {
echo "Lobby link isn't formatted correctly.";
}
I changed /joinlobby to //joinlobby, and remove the / at the end. I also removed the unnecessary () around everything.
I suspect you also shouldn't have (...)+ around steam?: and //joinlobby/730/. They'll cause repeated uses of those prefixes to be accepted as correct, e.g. steam:steam:...
I want to use my webpage to recognize if a $_POST is set and the, if it is, print it in the page, so this is what I really have now:
if (isset($_POST['error'])) {
echo '<div id="error">'.$_POST['error'].'</div>';
}
But what I want is that, when an if statement that I have in the same document returns true, to send a POST request to that same file and so, show the error message with the $_POST. Is this possible or it is another easy way for doing it?
Sorry for not explaining so well, this is my code:
if (password_verify($_POST['oldpassword'], $result['password'])) {
// Upload password to database
} else {
// Set the $_POST['error'] to an error message so I can show it in the error DIV.
}
Thanks!
You can define a $message athe beginning of your page then handle the errors you want to show
$message = '';
if (password_verify($_POST['oldpassword'], $result['password'])) {
// Upload password to database
} else {
//set a proper message ID which will be handled in your DIV
$message_id = 1;
header('location: /current_path.php?message='.$message_id);
}
Now in the div you can show it as
if (!empty($_GET['message'])) {
echo '<div id="error">';
if ($_GET['message'] == 1) { echo 'First message to show.'; }
elseif ($_GET['message'] == 2) { echo 'Second message to show.'; }
echo '</div>';
}