Google Recaptcha PHP Not Working - php

I have been building a Google ReCaptcha into a simple contact me form. I have been working on this for a couple of days now and I feel like there are 10 different ways to do it, but not of it "makes sense" or has clicked so far. The current code I have down is what I feel like would make sense best for me, but maybe someone else has a better way or tutorial.
Here is my question: Why is my form not connecting to Google's Servers and verifying and why am I not getting an echo when submitted.
CODE:
<div class="contactme" id="contactme">
<form class="form-horizontal" method="post" name="myemailform" id="myform" action="form-to-email.php">
<fieldset>
<h2>Let's Create Together</h2>
<hr class="small">
<div class="form-group">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">E-Mail</label>
<div class="col-lg-10">
<input type="email" class="form-control" name="visitor_email" placeholder="E-Mail" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="8" name="message" required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" name="submit" id="submitbutton" class="btn btn-primary mainButton">Submit</button>
<div class="g-recaptcha" data-sitekey="--KEY--"></div>
</div>
</div>
</fieldset>
</form>
</div>
PHP on seperate document.
<?php
if($_POST['submit']) {
$yoursecret='--KEY--';
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$yoursecret."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$googleobj = json_decode($response);
$verified = $googleobj->success;
if($verified === true){
//do stuff
echo "Success!, you have been verified!";
} else {
echo "No Success!";
}
}
?>
I also strangely get an "=" at the top of page ever since messing PHP and RECAPTCHA. The site is live at joshkelley.video If you submit the form it just takes you to a blank page, regardless of the whether you verify the Recapcha.
Is there a community standard for doing these? All I could find was a few youtube out of date youtube tutorials.

Related

php-How to have a button submit and send to page

I'm trying to make a button that submits order input to a database and then takes the user to the home page on my site, but I don't know how to do it. Here is my code:
<button type="submit" class="btn btn-primary" onclick="window.location.href='PurimHome.html'"> Place Order</button>
This button sends the info to the db but it doesn't go to the home page.
Here is more of the code:(I'm using bootstrap)
<form class="form-horizontal" action="" method="post">
<label class="col-sm-2" >Credit Card Number</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="creditnum" name="creditnum" placeholder="Credit Card Number" >
</div>
</div>
<div class="form-group">
<label class="col-sm-2" >Expiration Date</label>
<div class="col-sm-10">
<input type="date" class="form-control" id="date" name="date" placeholder="Expiration Date" >
</div>
</div>
<div class="form-group">
<label class="col-sm-2" >Security Code</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="code" name="code" placeholder="Three Digit Code" >
</div>
</div>
<input type="checkbox" name="contact" id="contact" value="Phone"> Contact Me by Phone<br>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" onclick="window.location.href='PurimHome.html'"> Place Order</button>
</div>
</div>
</form>
If you want to relocate to a different site after you submit.. then maybe you should place in your controller something like..
if(isset($_POST['submit'])){
//do stuff if needed
header('Location: url');
}
so that after you submit the form it will then redirect you to antoher location of your choosing.

Passing Values from One Form to another redirected Form in PHP

<form class="form-horizontal" enctype="multipart/form-data" method="post" action="store.php">
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION DATE</label>
<div class="col-sm-8">
<input type="date" class="form-control1" name="reg_date" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION No.</label>
<div class="col-sm-8">
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input type="SUBMIT" value="Next" class="form-control1">
</div>
</div>
</form>
Here, in this code there are more input types, I just skipped those. When I will press submit button this will take me to 'store.php' file where informations will be stored in database. And redirect me to another form and there are also some input type but I need a information like "reg_no" from the previous form in the current form. How can I get this?
NOTE : I am storing the inputs of first form in a database table. Then redirected to other form and the action of this form is another PHP file like 'store2.php' and it is also using to store informations to other table of database.
You could pass the value of reg_no as a
$_SESSION Variable
to the second form and get it there
You said that you are already passing the information from your current form into a database. When rendering your second form, you could grab whatever value you need from your database, and then add a value attribute to your input.
So for example if you have a form that looks like this in your second form:
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input">
You can add that class to Pre-Fill the form when the page loads. Lets say you want it to show "12345":
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input" value="12345">
I don't know the details of exactly how you are serving your site through PHP, but you could use some kind of string placeholder, or templating engine to put in this attribute.
Maybe this can help you :
this called form.php :
<form class="form-horizontal" enctype="multipart/form-data" method="POST" action="form_p.php">
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION DATE</label>
<div class="col-sm-8">
<input type="date" class="form-control1" name="reg_date" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION No.</label>
<div class="col-sm-8">
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input name="submit" type="SUBMIT" value="Next" class="form-control1">
</div>
</div>
</form>
this process of form.php, called form_p.php :
<?php
$reg_date = $_POST['reg_date'];
$reg_no = $_POST['reg_no'];
$submit = $_POST['submit'];
if (isset($submit)) {
$url = 'form_next.php?reg_date=' . $reg_date . '&reg_no=' . $reg_no;
header('Location:' . $url);
}
this next form, called form_next.php :
<?php
$reg_date = $_GET['reg_date'];
$reg_no = $_GET['reg_no'];
?>
<form class="form-horizontal" enctype="multipart/form-data" method="POST" action="form_p_next.php">
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION DATE AGAIN</label>
<div class="col-sm-8">
<input type="date" class="form-control1" name="reg_date" id="focusedinput" placeholder="Default Input" value="<?php echo $reg_date; ?>">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION No. AGAIN</label>
<div class="col-sm-8">
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input" value="<?php echo $reg_no; ?>">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input name="submit" type="SUBMIT" value="Next" class="form-control1">
</div>
</div>
</form>
and this is process of form_next.php, called form_next_p.php
<?php
//define your code
The code work because I passing reg_date and reg_no from form_p.php to form_next.php and I use $_GET to get reg_date and reg_no and print it in input value.
This is help you?

laravel show method not found exception when shortern URL link is passed in POST request

I have a laravel working directory on local. All forms are posting data on Local but only addshow form is not working on live. I came across that when I filled the link field with a google shortern URL. Laravel gives a method not allowed exception on submission of the form. Putting any other link beside shorten link works fine on live.
<form class="" role="form" method="post" action="{{ url('/addshow') }}">
{{ csrf_field() }}
<div class="box-body">
<div class="row">
<div class="col-sm-8">
<label for="name" class="col-sm-3 control-label">Title<span>*</span></label>
<div class="col-sm-9">
<input class="form-control" type="text" name="title" id="name" value="" placeholder="Enter Title"/>
</div>
</div>
</div>
<br/>
<div class="row">
<div class="col-sm-8">
<label for="name" class="col-sm-3 control-label">Place<span>*</span></label>
<div class="col-sm-9">
<input class="form-control" type="text" name="place" id="place" value="" placeholder="Enter place"/>
</div>
</div>
</div>
<br/>
<div class="row">
<div class="col-sm-8">
<label for="name" class="col-sm-3 control-label">Time<span>Start / End</span></label>
<div class="col-sm-4">
<input class="form-control" type="text" name="start_time" id="start_time" value="<?= date('h:m'); ?>" placeholder="Enter Start time"/>
</div>
<!-- <div class="col-sm-4 hidden-lg">
<input class="form-control" type="time" name="end_time" id="end_time" value="<?php// date('h:m'); ?>" placeholder="Enter End time"/>
</div> -->
</div>
</div>
<br/>
<div class="row">
<div class="col-sm-8">
<label for="name" class="col-sm-3 control-label">Date<span>*</span></label>
<div class="col-sm-9">
<input class="form-control" type="date" name="date" id="date" value="<?= date('y-m-20y') ?>" placeholder="Enter date"/>
</div>
</div>
</div>
<br/>
<div class="row">
<div class="col-sm-8">
<label for="name" class="col-sm-3 control-label">Link<span>*</span></label>
<div class="col-sm-9">
<input class="form-control" type="url" name="link" id="link" value="" placeholder="Enter Url"/>
</div>
</div>
</div>
<br/>
<div class="row">
<div class="col-sm-8">
<label for="status" class="col-sm-3 control-label">Status<span>*</span></label>
<div class="col-sm-9">
<select class="form-control" name="status">
<option value="1">Activated</option>
<option value="0">Deactivated</option>
</select>
</div>
</div>
</div>
</div>
<br/>
<!-- /.box-body -->
<div class="box-footer clearfix">
<div class="row">
<div class="col-sm-6">
<span class="pull-right">
<input class="btn btn-info" type="submit" name="Submit" id="Submit" value="Create" placeholder=""/>
</span>
</div>
</div>
</div>
</form>
plus this is route Route::post('/addshow', 'DashboardController#addshow');
public function addshow(Request $request) {
echo"<pre>";print_r($request->all());echo"</pre>";exit();
Shows::create($request->all());
return redirect(url('/allshows'));
}
for the time being validation and other stuff not including to keep it simple
this happens with me too the only solution i came accross that when i put the shortern URL it will give you method not found exception in most cases so simply allow user to enter only last 6 digits or alphabets of URL and save to database . only this way i solved this problem and hope you will find it helpfull . plus you should also see the collation from default to utf8_general_ci
The most critical piece of information that I can gather from your question is that you're getting a MethodNotAllowedHttpException which tells us that the specific issue is that the request is coming in using a different HTTP method than you have set the route up to handle.
From the code samples you have provided both your form and your route seem to use the POST method so it seems that submitting the form is not the cause of the issue. To be certain you could try adding a something like dd('test'); right at the start of your addshow method. If you still get the exception then it means either your form is not actually using the POST method or the route is incorrectly configured.
One alternative is that the MethodNotAllowedHttpException is not for /addshow. You mention that it appears when you add a Google shortened URL to the link field, does it appear when you enter other links? If so it could be that the Google shortened URL is failing validation and redirecting to a URL that does not use the GET method. It's impossible to say for certain without your validation code but it would be worth checking what URL your browser displays when you get the error screen.

How to stay on a Bootstrap PopUp form after validation?

I'm actually developing a Web site where you can register yourself from a form.
I've create a code in PHP for managing the errors which could happen (wrong number, mail...)
Here is my problem : When I send the form, I cant manage to stay on it to overwrite the form with the errors encounters (if there are some).
I'm using Bootstrap form the form and I have absolutely no clue of how to do that.
This is the my HTML
<div class="modal fade" id="ins" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form id="FormIns" action="Index.php" method="post">
<div class="modal-header">
<h4 class="text-center">Inscription</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="text" class="form-control" name="lastname" id="lname" placeholder="Nom" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="text" class="form-control" name="firstname" id="fname" placeholder="Prénom" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="text" class="form-control" name="nid" id="nidentifiant" placeholder="Identifiant" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="password" class="form-control" name="npass" id="npassword" placeholder="Mot de Passe" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="password" class="form-control" name="cnpass" id="cnpassword" placeholder="Confirmer Mot de Passe" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="mail" class="form-control" name="mail" id="mail" placeholder="Adresse Email" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="text" class="form-control" name="phone" id="phone" placeholder="Numéro de Téléphone" maxlength="25">
</div>
</div><br/>
<div class="modal-footer">
<input class="btn btn-primary" type="submit" value="Inscription" name="log">
<a class="btn btn-default" data-dismiss="modal">Annuler</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Firstly, you should add labels to your form fields. These help with the page as well as give a good entry point for error messages.
A simple approach would be to set the form-action to the form page, and put validation errors in an associative array with values like
$errors['name'] = "Name did not match"; and so on and so forth.
Then you add a <span> at the end of the label like (roughly) this:
<span>
<?php echo (isset($errors['name'])) ? $errors['name'] : "";?>
</span>
</label>
This would make PHP evaluate the errors and put them into the label so they can be easily connected to the field.
Lastly, to continue, you add a redirect to the next page, if the validation is successful, so create a redirect function and send the validated data to the next page.
€: for good effects, you can use php to append the alert or warning class to the corresponding form-control groups or inputs.
If you are using a script to check that the values are right, the first thing you have to do is stop the form action.
<script type="text/javascript">
$("#FormIns").submit(function(e){
e.preventDefault();
// your code to check the form goes here
//once you finished and everything is right then:
$( "#FormIns" ).submit();
});
</script>

PHP not able to access POST values

I have a HTML login form that is processed and displayed to the user by PHP if they are not logged in. This form is set to POST to /user.php which in turn accesses my authentication class, and either creates a session and returns true or false and does not log the user in.
My problem is that whatever I try, the HTML form simply will not POST values to the PHP processing part.
This is my current source code:
Log in form:
<div class="panel-body">
<!-- BEGIN ifLogInError -->
<div class="alert alert-danger">
<p>Either your username or password was incorrect. Please try again. Forgotten your password?</p>
</div>
<!-- END ifLogInError -->
<form class="form-horizontal" method="post" action="{ROOT}/user.php">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" value="" placeholder="Your username">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" value="" placeholder="Your password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="Sign in" />
</div>
</div>
</form>
</div>
User.php (as it currently stands):
echo "<pre>";
var_dump($_POST);
var_dump(file_get_contents("php://input"));
echo "</pre>";
phpinfo();
Both var_dumps return the following, suggesting the form is not POSTing:
array(0) {
}
string(0) ""
What am I doing wrong here?
You haven't given your <input>s a name attribute.
Needs <input name="myname">
Then in the output file put $_POST['myname']

Categories