Simplest way to stop email submission if field is empty - php

Im trying to duplicate my client side validation on the server (PHP). What is the simplest way to make sure a field has been filled out? I've done it like this before but am having some issues, is there a better way?
<?php
if(empty($_POST['Contact0FirstName']) || empty($_POST['Contact0Email']) ||
empty($_POST['Contact0Phone1']) || empty($_POST['CaptchaInput']) || !empty($_POST['LeadBlind'])) {
echo "Error";
die();
} else {
//set POST variables
$url = 'xxx';
$infusion_xid = $_POST["xxx"];
$infusion_type = $_POST["xxx"];
$infusion_name = $_POST["xxx"];
$Contact0FirstName = $_POST["Contact0FirstName"];
$Contact0Email = $_POST["Contact0Email"];
$Contact0Phone1 = $_POST["Contact0Phone1"];
$fields = array(
'infusion_xid'=>urlencode($infusion_xid),
'infusion_type'=>urlencode($infusion_type),
'infusion_name'=>urlencode($infusion_name),
'Contact0FirstName'=>urlencode($Contact0FirstName),
'Contact0Email'=>urlencode($Contact0Email),
'Contact0Phone1'=>urlencode($Contact0Phone1)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'& ');
//open connection
$ch = curl_init($url);
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//their return html
$output = curl_exec($ch);
//echo "TEST";
echo $output;
curl_close($ch);
}
?>
<form id="Lead" method="post" action="PHP/Lead.php" accept-charset="UTF-8">
<ul id="LeadForm">
<li>*required field</li>
<li>
<input type="hidden" name="LeadBlind" id="LeadBlind">
<label for="Contact0FirstName">1 | First Name*</label>
<label for="Contact0Email">2 | Email*</label>
<label for="Contact0Phone1">3 | Daytime Phone*</label>
</li>
<li>
<input type="text" name="Contact0FirstName" id="Contact0FirstName">
<input type="text" name="Contact0Email" id="Contact0Email">
<input type="text" name="Contact0Phone1" id="Contact0Phone1">
</li>
<li>
<label for="CaptchaInput">4 | Please enter the code</label>
</li>
<li>
<input type="text" class="numbers" name="Captcha" id="Captcha" value="" readonly>
<input type="text" class="numbers" name="CaptchaInput" id="CaptchaInput" size="6" maxlength="6">
</li>
<li>
<input type="submit" id="LeadSend" value="Try It Now!">
<span id="processing">Submitting Your Request</span>
</li>
</ul>
<div class="clear"></div>
</form>
I need "LeadBlind" to be empty, it is a hidden input that only form filling bots would fill in, so if it has a value it should trigger the error. The ajax function is based on what is returned, if it's "error" it displays an error msg, otherwise a success one. thx for the help!

Related

How do I encode a GET request for the $search variable in the URI?

How do I encode a GET request for the $search variable in the URI (example: Android and Corp) to get (Android%2Band%2BCorp) instead of (Android+and+Corp)?
Form_
<form action="search.php" method="GET">
<input type="text" name="search" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
<input value="1" name="chank" id="one" type="radio" checked />
<label for="one" class="switch__label">1</label>
<input value="2" name="chank" id="two" type="radio" />
<label for="two" class="switch__label">2</label>
</form>
search.php_
$search=$_GET['search'];
$channel=$_GET['chank'];
$page=$_GET['page'];
include "conf/info.php";
$title = 'Result | '.$search;
include "header.php";
include "api/api_search.php";
GET_ api.php
$cs = curl_init();
curl_setopt($cs, CURLOPT_URL, "".$home."".$vapi."/search/".$chank."?api_key=".$key."&language=".$lang."&query=".$search."&page=".$page."");
curl_setopt($cs, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($cs, CURLOPT_HEADER, FALSE);
curl_setopt($cs, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$response = curl_exec($cs);
curl_close($cs);
$search = json_decode($response);
But I still get '+' instead of true '%2B' in my address bar
It looks like curl is encoding your string from _GET (with the +) into %2b. You can use urldecode() to get the 'original' before building the curlopt url:
$s = 'some+cool+string';
echo $s . PHP_EOL; // some+cool+string
echo urlencode($s) . PHP_EOL; // some%2Bcool%2Bstring
echo urldecode($s) . PHP_EOL; // some cool string

php file_get_contents($url) return strange string

I try to get news from website
http://www.science-support.ru/news.html
, but I can't extract anything from web page content.
When I apply file_get_contents() I get string for which other function don't work. Initially, I try to use file_get_html() from simple_html_dom.php, but then other functions from this source don't recognize any elements in obtained DOM.
About other function:
<?php
$content = file_get_contents('http://www.science-support.ru/news.html'); //normal page
$content = substr($content,20); //strange characters
$content_arr = explode( 'div id="box3"' , $content ); //doesn't work
echo $content;
echo $content_arr[0];
?>
After substr() I get something like
" <�/tr> <�/table> <�/div> <�div id="box3"><�!-- InstanceBeginEditable name="page-content" --> <�h4 class="yellow">14.11.2014 />2>AB8 $>=40/<�/h4> <�p>1JO2;O=K #57C;LB0BK :>=:C#A0 =0 ?#8AC645=85 ?>8A:>2KE 3#0=B>2 ?> #>3#0<<5 =0CG=>-B5E=8G5A:>9 <>45#=870F88 8 ?>2KH5=8N :20;8D8:0F88 <>;>4KE CG5=KE >AA88 2014-2015 33.<�a href="news/news2014-nota-res.html" class="txt" >?>4#>1=55<�/a><�/p>"
How can I extract readable content? Thanks
Try to do this from Curl i have post some code that will help you
<?php
if( isset( $_POST['site_url'] ) && !empty( $_POST['site_url'] ) ){
echo get_html($_POST['site_url']);
} else {
echo 'false';
}
function get_html($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
get website url from your html form this is html how to set
<div class="col-md-offset-2 col-md-8">
<form role="form" id="siteform" method="post">
<div class="form-group">
<input type="url" class="form-control" name="site_url" id="site_url" placeholder="Enter your site address">
<span class="help-block"></span>
</div>
<button data-loading-text="Please wait..." type="submit" id="url_getter" class="btn btn-default btn-success">Submit</button>
</form>
</div>
</div>

ExactTarget subscribe form also sends form detail

Currently have a newsletter subscribe form that submits info to a an ExactTarget account.
Is it possible to have the same form send an email, similar to a PHP contact form?
At one time this was a PHP form.
I did review Denver's MailChimp Subscribe in Contact Form option:
Mail Chimp Subscribe In Contact Form
This form does not need a check box to subscribe to the newsletter.
<form action="http://cl.exct.net/subscribe.aspx?lid=hidden" name="subscribeForm" method="post">
<input type="hidden" name="thx" value="enewsletter-thank-you.php" />
<input type="hidden" name="err" value="enewsletter-error.php" />
<input type="hidden" name="MID" value="hidden" />
<ul class="contactForm">
<li class="contactForm">
<label class="formFieldQuestion">E-mail Address: <span class="redrequired">*</span></label>
<input class="field-text" type="text" name="Email Address" size="20" value=""></li>
<li class="contactForm">
<label class="formFieldQuestion">Full Name:</label>
<input class="field-text" type="text" name="Full Name" size="20" value=""></li>
<li class="contactForm">
<label class="formFieldQuestion">Company:</label>
<input class="field-text" type="text" name="Company or Organization" size="20" value=""></li>
<li style="padding-left:10px;">
<input id="saveForm" class="button_form" type="submit" value="Subscribe" name="submit"> </li>
</ul>
</form>
Yes, you can send this as a curl request. Change your form action to something like mail_and_submit.php.
Then, in mail_and_submit.php:
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://cl.exct.net/subscribe.aspx?lid=hidden';
$fields = array(
'thx' => urlencode($thx),
'error' => urlencode($error),
'MID' => urlencode($MID),
... (etc)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
THEN, build the email message with those same fields, and send, with something like:
$message = 'A post was submitted:<br/>';
$message .= 'thx: ' . $thx;
$message .= 'error: ' . $error;
$message .= 'MID: ' . $mid;
... etc, then mail with:
mail('some-email#address.com', 'Your Entry', $message);

Problems sending data to PHP with Curl

I need some advice with this curl script. I am trying to send values from the FORM to the curl script, but seems that the curl script doesn`t receive the info.
Here is the script:
<?php
if($_SERVER['REQUEST_METHOD'] != 'POST') {
$self = $_SERVER['PHP_SELF'];
?>
<form method="post" action="<?php echo $self; ?>" class="form-horizontal">
<fieldset>
<legend>SMS Contact</legend>
<div class="control-group">
<label for="basic" class="control-label">Name and Surname</label>
<div class="controls">
<input type="text" name="name" id="name" disabled class='input-square' value="<?php print $contactname;?> <?php print $contactsurname;?>">
</div>
</div>
<div class="control-group">
<label for="basic" class="control-label">Mobile No</label>
<div class="controls">
<input type="text" name="mobile" id="mobile" disabled class='input-square' value="<?php echo urlencode($contactmobile);?>">
</div>
</div>
<div class="control-group">
<label for="textcounter" class="control-label" id="textcounter">Textarea</label>
<div class="controls">
<textarea name="textcounter" id="textcounter" class='input-square span9 counter' data-max="160" rows='6'></textarea>
</div>
</div>
<div class="form-actions">
<button class="btn btn-primary" type="submit">Send SMS</button>
</div>
</fieldset>
</form>
<?php
} else {
$mobile = $_POST['mobile'];
$text = $_POST['textcounter'];
$username = 'xxxx';
$password = 'xxxx';
// Set Timezone
date_default_timezone_set('Africa/Johannesburg');
$date = date("m/d/y G.i:s", time());
// Create Unique ID
$code = md5(time());
$newid = $code.$clientid;
$sql="INSERT INTO sent_itmes (sent_id, sent_message, sent_date, sent_quantity, client_id, sent_mobile)VALUES('$newid', '$text', '$date', '1', '$clientid', '$mobile')";
$result=mysql_query($sql);
$url = "http://bulksms.2way.co.za/eapi/submission/send_sms/2/2.0"; // URL to calc.cgi
$fields = array(
'site'=>'',
'username'=>($username),
'password'=>($password),
'message'=>urlencode($text),
'msisdn'=>urlencode($mobile)
);
$fields_string="?";
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
ob_start();
curl_exec($ch);
ob_end_clean();
echo '<div class="alert alert-block alert-success">
<a class="close" data-dismiss="alert" href="#">×</a>
<h4 class="alert-heading">Success!</h4>
Your message has been Sent!
</div><br/><br/>Search Contact';
//close connection
curl_close($ch);
}
?>
When submitting the form, I get the error from response server "No recipients specified", so this means that the form value "mobile" doesnt pass though the value to the curl.
Help please, going off my mind.
Try adding a header that describes the content you post and maybe content you apply
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/x-www-form-urlencoded',
'Accept: text/html',
'Expect: ',
) );
use this for sending data
//open connection
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,POSTVARS.$fields_string); // this line is edited..
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
//execute post
ob_start();
curl_exec($ch);
your order was wrong...
more info http://www.askapache.com/php/sending-post-form-data-php-curl.html
When you submit form with disabled field, that field do not sent:
<input type="text" name="mobile" id="mobile" disabled class='input-square' value="<?php echo urlencode($contactmobile);?>">
If you want to send "mobile", you should add hidden field for it. And maybe replace disabled field name

How CURL inject COOKIE in remote site

i can't inject the cookie in page2, Some tips pls.
With this method i load page1 with catcha what containing the cookie
and write it to cookie.txt :
define('COOKIE', './cookie.txt');
define('CAPTCHA', './captcha.png');
function saveCaptcha() {
$header = array('Content-Type: image/png');
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_COOKIESESSION, true);
curl_setopt($ch,CURLOPT_COOKIEJAR, COOKIE);
curl_setopt($ch,CURLOPT_URL,'http://example.com/captcha/securimage_show.php');
curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8118');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
$fileContents = curl_exec($ch);
curl_close($ch);
$newImg = imagecreatefromstring($fileContents);
return imagepng($newImg, "CAPTCHA",9);
}
$fr = filesize(COOKIE);
echo COOKIE . ': ' . $fr . ' bytes';
if ($fr <= 50) saveCaptcha();
Cookie successful saved and writed to cookie.txt, now we past captcha in form:
<form action="index.php" method="post">
<input type="hidden" name="p" value="login" />
<div class="line">
<label>Captcha</label>
<img src="CAPTCHA" border="0" />
</div>
<div class="line">
<label> </label>
<input type="text" name="captcha" value=""/>
</div>
<div class="line">
<label> </label>
<input type="submit" value="Login" />
</div>
</form>
Now we extract the captcha with $_POST and load page2 with our cookie.txt:
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://example.com/index.php';
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch,CURLOPT_COOKIESESSION, true);
curl_setopt($ch,CURLOPT_PROXY, '127.0.0.1:8118');
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POSTFIELDS, 'p=login&user=myuser&pass=123qwe&captcha='.$_POST['captcha'].'&submit=Login');
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch,CURLOPT_REFERER, 'http://example.com/');
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$fa = fopen(COOKIE, 'w');
#fwrite(COOKIE,'');
fclose($fa);
Page2 is loaded but without cookie. Some tips pls maybe you will saw the issue(( THanks.

Categories