I got an ajax submitting form source code, and learning it right, I found the return errors are all in a big square. I want to separate errors to where they belong to.
then I can simply add something like following code
<?php if($_session('errorarray'): ?>
<span class="errorclass"><?php echo $errorarray['phone']; ?></span>
<?php endif; ?>
here is validation php code, (I have 5 items need to be checked)
$error = array();
if(!check('name'))
$error[]='too short!';
else if(validate_name($_POST['name']))
$error[]='letters please!';
..........
if(checkphone($_POST['phone'])){
$error[]="Please enter a valid phone number";
}
here is the code return to $_Session
if(count($error))
{
if($_POST['ajax'])
{
echo '-1';
}
else if($_SERVER['HTTP_REFERER'])
{
**$_SESSION['errorarray'] = array ($error);** // possible?
$_SESSION['post']=$_POST;
header('Location: '.$_SERVER['HTTP_REFERER']);
}
exit;
}
Sorry I am very new to php. Hope my expression is not too hard to understand.
Many thanks in advance.
You can just set $_SESSION['errorarray'] = $error; Then in your view, you can do something like <span class="errorclass"><?php foreach ($_SESSION['errorarray'] as $err) { echo $err; } ?></span>
Sorry, your question is a bit unclear, if you provide more details we could probably give you a better answer.
Related
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 implement recaptcha in a very simple form
I have a index.html file on client-side, and a post.php server side.
I've tried to integrate recaptcha on the server site, as you can see in my code bellow.
I've made some tests, that seem to have an expected result...
The problem appeard when I tried this query
for X in `seq 0 100`; do curl -D - "http://example.com/post.php" -d
"email=email${X}%40example.com&tos=on&g-recaptcha-response[]=plm&submit="; done
The result was that I've bypassed recaptcha succesfully, and I'm not sure what the problem is.
Most probably, there's a problem in my php code, but what exactly?
post.php
<?php
$email;$submit;$captcha;
if(isset($_POST['submit']))
{
$email=filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
}
if(isset($_POST['g-recaptcha-response']))
{
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha)
{
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6Le[whatever[7_t&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo '<h2>You are spammer ! Get the #$%K out</h2>';
}
else
{
$file = 'email-list.txt';
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
if(!(exec('grep '.escapeshellarg($email).' '.$file)))
{
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $email . "\n";
// Write the contents back to the file
file_put_contents($file, $current);
header('Location: index.html?success='.urlencode($email));
}
else
header('Location: index.html?fail='.urlencode($email));
}
else
{
echo "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
}
?>
index.html
...
<div class="form-group" ng-cloak>
<div class="g-recaptcha" ng-show="IAgree" data-sitekey="6LeEW[whatever]-UXo3"></div>
</div>
...
How can I solve this? English is not my native language; please excuse typing errors.
As mentioned in my comments above - file_get_contents returns a string. You need to decode the json string into a php object using the json_decode function:
$url = "https://www.google.com/recaptcha/api/siteverify?"
$response = json_decode(file_get_contents($url));
if($response->success == false) {
echo "Oh no";
}
Can anybody help me on how I can stop an heading displaying if there are no search results?
The app generates palettes based on flickr images, at the minute I have a few else statements for validation that if there are no results from the flickr API etc, then a message will appear, as shown:
// No results from Flickr
} else {
echo "<div class=\"errormsg\"> Uh oh... no results for that Hex. Why not try a keyword?</div>";
}
} else {
// Error with the Flickr API
echo "<div class=\"errormsg\"> Oh boy... there was an error with the API.</div>";
}
} else {
// No hex code entered
echo "<div class=\"errormsg\"> Hey, it looks like you forgot to enter a hex code - Try again!</div>";
}
If there are results it will display a heading (#welcomeDiv) stating
Displaying palettes for...
However, I only want this to display if there are results, I have tried:
else{
?>
<style type="text/css">#welcomeDiv{
display:none;
}</style>
<?php
}
welcomeDiv that I want hidden
<?php if (isset($_POST['submit'])) { ?>
<h2 id="welcomeDiv">Displaying palettes for <span class="hex">"<?php echo $_POST['hex'] ?>"</span></h2>
<div id="results">
<?php include 'assets/php/generate.php'; ?>
</div><!--/results-->
<?php } ?>
But this doesn't apply. Does anybody know why?
Here's a link to the project.
This edit could work.
// No results from Flickr
} else {
?>
<style type="text/css">#welcomeDiv{ display:none;}</style>
<?php
echo "<div class=\"errormsg\"> Uh oh... no results for that Hex. Why not try a keyword?</div>";
}
} else {
// Error with the Flickr API
echo "<div class=\"errormsg\"> Oh boy... there was an error with the API.</div>";
}
} else {
// No hex code entered
echo "<div class=\"errormsg\"> Hey, it looks like you forgot to enter a hex code - Try again!</div>";
}
I see you also have jquery referred in the link. You can use javascript to hide the heading.
// No results from Flickr
} else {
?>
<script>$('#welcomeDiv').hide();</script>
<?php
echo "<div class=\"errormsg\"> Uh oh... no results for that Hex. Why not try a keyword?</div>";
}
} else {
// Error with the Flickr API
echo "<div class=\"errormsg\"> Oh boy... there was an error with the API.</div>";
}
} else {
// No hex code entered
echo "<div class=\"errormsg\"> Hey, it looks like you forgot to enter a hex code - Try again!</div>";
}
Basically:
if ($has_results) {
echo ...
} else {
// do nothing
}
put an if() test on the relevant section, and DON'T echo something when you don't want it to be echoed.
I have a problem with the understanding of variable scopes.
I've got a huge .php file with many $_POST validations (I know that isn't not good practise). Anyways I want a little html-part above all the code which outputs an error message. This message I want to change in every $_POST validation function.
Example:
if($ERR) {
echo '<div class="error-message">'.$ERR.'</div>';
}
Now my functions are following in the same file.
if(isset($_POST['test']) {
$ERR = 'Error!';
}
if(isset($_POST['test2'] {
$ERR = 'Error 2!';
}
But that doesn't work. I think there's a huge missunderstanding and i'm ashamed.
Can you help me?
I didnt catch your question but maybe this is your answer:
<body>
<p id="error_message">
<?php if(isset($ERR)){echo $ERR;} ?>
</p>
</body>
and I suggest you to learn how to work with sessions.
and you should know that $_Post will be empty on each refresh or F5
You can do put the errors in array make them dynamic.
<?php
$error = array();
if (!isset($_POST["test"]) || empty($_POST["test"])) {
$error['test'] = "test Field is required";
} else if (!isset($_POST["test1"]) || empty($_POST["test1"])) {
$error['test1'] = "test Field is required";
}else{
//do something else
}
?>
You can also use switch statement instead of elseif which is neater.