Send HTML input to PHP Twilio Script - php

I'm new to PHP and HTML forms. I'm trying to send the form data below to my PHP script sendsms.php it doesn't seem that the php script is getting the input data from the html form.
<form action="sendsms.php" method="post" >
<div id="space">Cell:<div><input type="number" name="phone" id="number" ></div></div>
<div id="space"><div>Message</div><div><input type="text" name="message" id="message"> </div></div>
<div id="button"><input type="submit" name="send" value="Send" id="button" ></div>
</form>
Here is my PHP file sendsms.php
<?php
// this line loads the library
require('twilio-php/Services/Twilio.php');
$account_sid = 'REMOEVD';
$auth_token = 'REMOVED';
$client = new Services_Twilio($account_sid, $auth_token);
$client->account->messages->create(array(
'To' => $_POST['phone'];,
'From' => "+16194523868",
'Body' => $_POST['message'];,
));

If you look at your form:
<form action="sendsms.php" method="post" >
you are essentially posting the form inputs, so you need to get the $_POST parameters in PHP as in :
$client->account->messages->create(array(
'To' => $_POST['phone'],
'From' => "+16194523868",
'Body' => $_POST['message']
));
Edit
Just noticed you also have some semicolons (;) inside the array declaration which will still break your code

Related

Take data from html form and send it off as a http post request ( Class 'HttpRequest' not found) [duplicate]

This question already has answers here:
Trying to send a POST request using php, No matter what i do i get "HTTP ERROR 500"
(2 answers)
Closed 2 years ago.
This is my index.html (form):
<div class="body">
<form method="post" action="index.php">
<div id="form">
<div class="formInput">
<label>To:
<input type="text" name="to" id="to" />
</label>
</div>
<div class="formInput">
<label>From:
<input type="text" name="from" id="from" />
</label>
</div>
<div class="formInput">
<label>Message:
<input type="text" name="message" id="message" />
</label>
</div>
<input type="submit" value="Submit" />
<input type="reset" value="Clear Form" />
</div>
</form>
And this is the index.php that handles the data:
<?php
$url = 'https://domainname.com/dashboard/api';
$r = new HttpRequest($url, HttpRequest::METH_POST);
$r->addQueryData(array('to' => $_POST['to'],
'from' => $_POST['from'],
'email' => $email,
'api_secret' => $api_secret));
try {
$r->send();
if ($r->getResponseCode() == 200) {
echo $r->getResponseBody();
}
} catch (HttpException $ex) {
echo $ex;
}
?>
Everytime i sumbit to the form i get an error: "PHP Fatal error: Class 'HttpRequest' not found"
I have no idea how to resolve this since im running this on my namecheap web server..
A few people suggested i use cURL but i absolutely have no idea how.. I spent the past 3 hours trying to understand and i made zero progress.
Note:This how the http request should look like, i don't even know if i'm doing it correctly...
https://domainname.com/dashboard/api
?to={PHONE NUMBER}&from={SENDER ID}&message={TEXT}
&email={YOUR EMAIL}&api_secret={API SECRET}
HttpRequest is a class only available from pecl
If you wish to use this class, you'll need to install pecl on your server. Since you're using a namecheap server, it's unlikely that you'll be able to do this.
As has been mentioned in some comments, I'd suggest looking at using cURL. For example, you could do something like this:
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://domainname.com/dashboard/api',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'to' => $_POST['to'],
'from' => $_POST['from'],
'email' => $email,
'api_secret' => $api_secret,
],
]);
$response = curl_exec($ch);
curl_close($ch);
echo($response);
See the documentation here.

Why Wordpress wp_update_post function removes html form tags?

I'm trying to add a form with hidden inputs into the post content using external PHP fle.
When i try it from the browser the form added successfully but when i try it from command line, the form inputs are deleted.
Here is my code:
require('../wp-load.php');
$content = '<div class="buy-preowned">
<form accept-charset="UTF-8" action="https://www.tesla.com//order?redirect=no" id="tesla-cpo-marketing-buy-form" method="post">
<div>
<input name="CPOvehicle" type="text" value="1"/>
<input name="VIN" type="hidden" value="5YJSA1E1XHF210809"/>
<input name="vehicleMapId" type="hidden" value="1280359"/>
<input name="titleStatus" type="hidden" value="NEW"/>
<input name="form_id" type="hidden" value="tesla_cpo_marketing_buy_form"/>
</div>
</form>
<p class="small-text">
Requires a $2,500 deposit
</p>
</div>
';
$post_id = 1;
$data = array(
'ID' => $post_id,
'post_content' => $content,
);
When i checked the database, the form is stored as:
<form accept-charset="UTF-8" action="https://www.tesla.com//order?redirect=no" id="tesla-cpo-marketing-buy-form" method="post">
<div>
</div>
</form>
this is only happen when i rub the script from command line, any idea how to resolve it.
Thank you
It s because of built-in security filters. As it is not normal to store some form data inside content(there are shortcodes for that), WP disabled inserting some non-text tags.
But anyway, you can enable it manually.
kses_remove_filters();
$post_id = 1;
$data = array(
'ID' => $post_id,
'post_content' => $content,
);
wp_update_post($data);
kses_init_filters();

HTML5 form with PHP action on Heroku

I have a contact form on my site that is hooked up to a Mailgun server to send email upon submit.
All is working fine locally, but when hosted on Heroku, the form doesn't act as expected and instead triggers a download. Any idea what needs to be done to get this to behave properly?
I've attempted making my index.html an index.php and putting the PHP script in <?php ?> tags at the top of the page - both of which worked locally - but neither has worked on Heroku.
Thank you!
form from index.html:
<form method="post" name="contact_form" action="form.php">
<h1>want to chat?</h1>
<input type="text" name="name" placeholder="name" required>
<input type="email" name="email" placeholder="email" required>
<textarea name="message" cols="30" rows="10" placeholder="drop a message..." required></textarea>
<input type="submit" value="send off" name="submit">
<h1 class="email-thanks"></h1>
</form>
form.php file:
<?php
require '../vendor/autoload.php';
use Mailgun\Mailgun;
session_start();
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
# Instantiate the client.
$mgClient = new Mailgun('key-xxxxxxxx');
$domain = "mail.laurenfazah.com";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => $name . ' <' . $email . '>',
'to' => 'Lauren <example#gmail.com>',
'subject' => 'Portfolio Message',
'text' => $message
));
}
header( 'Location: /' ) ;
session_destroy();
?>
Sounds like the server it is hosted on doesn't have PHP installed.

Issues with HTML PHP Mailer

i I've been trying to get a .php that works with this HTML code for my website (template), I tried using my old .php from my old website and changing the details but that sadly lead to no avail.
I am clueless when it comes to .php and would really appreciate your help!
What would my .php have to contain?
<form action="#" id="contact-form">
<div id="success"></div>
<ul>
<li class="input-name">
<input type="text" id="name" class="required" placeholder="Name">
</li> <!-- END input-name -->
<li class="input-email">
<input type="text" id="email" class="email" placeholder="Email Address">
</li> <!-- END input-name -->
<li class="input-subject">
<input type="text" id="subject" placeholder="Subject">
</li> <!-- END input-name -->
<li class="input-subject">
<textarea rows="7" cols="50" id="message" class="required" placeholder="Message"></textarea>
</li> <!-- END input-name -->
</ul> <!-- END #contact -->
<button type="submit" class="btn btn-primary btn-lg pull-right">Send Message</button>
</form>
if you are not using ajax then write php file name in form action attribute currently there is #. then you php file will be called.
Change the below code
<form action="#" id="contact-form">
to
<form action="mailer.php" method="post" id="contact-form">
hope this will help.
You have to include the in action="#" the php file. example: action="contactForm.php". That's how the HTML code knows where to send the parameters.
Also, make sure your server supports the 'mail' function.
AND!
I have a good standard example for you of a well written mailing php file that I used. So you can check yourself for syntax issues.
<?php
require_once "Mail.php";
if(isset($_POST['email'])) {
$email = $_POST['email']; //this is how you get your variables from the HTML file. 'edit' is the id of the element.
$email_to = "yourEmail#example.com";
$host = "ssl://smtp.gmail.com:465"; //use your email host - this example is gmail based. (you can search for your own email host via google).
$username = 'username#example.pro';
$password = 'yourPass';
$email_subject = "You have a new email from $email via example.com website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($email_to, $headers, $message);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}
}
In case of sending emails with ajax - it's an other thing.. and I can help you with that too.

how to pass parameter to php function and call in html

I have this function in php; a separate file, function dbRowInsert($table_name, $form_data).
I included it in my php file in which registration happens. My problem is how do I call the function on form submit and pass a parameter to the dbRowInsert function. This is the data of my form:
$form_data = array(
'username' => $username,
'password' => $password,
'title' => $title,
'first_name' => $first_name,
'middle_name' => $middle_name,
'last_name' => $last_name,
'position' => $position,
'residence' => $residence,
'monthly_salary' => $monthly_salary,
);
I tried this method:
<form id="signup_form" class="form-horizontal" role="form" action="<?php dbRowInsert(tblperson, $form_data) ?>">
...
</form>
PHP is not written like JavaScript; a POST request must be sent to a PHP page for processing (unless you're using AJAX), like so
<form method="POST" action="process.php">
....
</form>
In process.php, you have to extract out the fields you want to send to the function.
$username = $_POST['username'];
doSomethingWIthUserName($username);
Or in you case, since you are sending the entire array:
dbRowInsert("tblHelpers", $_POST);
Here's a detailed tutorial on handling POST requests.
Put all that in the same file where register form is after including function php file
<?php
//include file code start
function dbRowInsert($tblperson, $form_data){
echo "<pre>".print_r($form_data,true)."</pre>";
}
//include file code end
if(isset($_POST['submit'])){
$form_data = array();
$form_data['username'] = $_POST['username'];
$form_data['password'] = $_POST['password'];
$tblperson = "person";
//do the action
dbRowInsert($tblperson, $form_data);
}
?>
<form id="signup_form" method="post" class="form-horizontal" role="form" action="">
<input type="text" name="username" />
<input type="text" name="password" />
<input type="submit" name="submit" />
</form>

Categories