I have code PHP in file update_customer.php to save information customer
<?php
date_default_timezone_set('Asia/Ho_Chi_Minh');
$response = array( 'status' => 0, 'message' => '', 'typeinsurance' => '', 'thoigianmua' => 0 );
if (empty($_GET) && !empty($_POST) && count($_POST) == 7 && isset($_POST['fullname']) && isset($_POST['email']) &&
isset($_POST['address']) && isset($_POST['phone']) && isset($_POST['cost']) && isset($_POST['typeinsurance']) &&
isset($_POST['typepay']))
{
require_once ('./../include/database.php');
$database = new Database();
$time = time();
$_POST['thoigianmua'] = $time;
if ($database->insert('customer', $_POST)){
$response['status'] = 1;
$response['typeinsurance'] = $_POST['typeinsurance'];
$response['thoigianmua'] = $time;
}
}
echo json_encode($response);
and code JQUERY to sent data, but My JSON get
Object {status: 1, message: "", typeinsurance: "<?php echo $_POST['typeinsurance']; ?>", thoigianmua: 1440642246}
Please help me, thank in advance
You php code has error for sure that's why its returning the data in a way that you not expected.
The wrong part is <?php echo $_POST['typeinsurance']; ?> you have given the php code as string.
you have to check your php file
Related
I am trying to detect status of invoice from a json file, then if the status is a confirmed payment, update the status and write the json to a new location, then unlink the existing json location.
<?php
// get posted variables or die;
if (isset($_POST['num'])) {
$invoice = strip_tags($_POST['num']);
$filename = $invoice.'.json';
} else {
die;
}
if (isset($_POST['status'])) {
$status = strip_tags($_POST['status']);
} else {
die;
}
// get existing invoice
$content = file_get_contents('data/'.$invoice.'.json');
$data = json_decode($content, true);
// read json into variables
$email = $data['email'];
$id = $data['id'];
$addr = $data['tac_address'];
$os = $data['os'];
$exp = $data['experience'];
$hosting = $data['type'];
if (isset($data['telegram']) && $data['telegram'] != '') { $telegram = $data['telegram']; } else { $telegram = ''; }
if (isset($data['linkedin']) && $data['linkedin'] != '') { $linkedin = $data['linkedin']; } else { $linkedin = ''; }
if (isset($data['pay_status']) && $data['pay_status'] != '' && $data['pay_status'] == $status) { $status = $data['pay_status']; }
$payment_addr = $data['bitcoin'];
$payment_value = $data['value'];
$payment = substr($payment_value, 0, -4);
// turn variables into json array
$arr = array(
'id' => $invoice,
'email' => $email,
'tac_address' => $addr,
'os' => $os,
'experience' => $exp,
'type' => $hosting,
'telegram' => $telegram,
'linkedin' => $linkedin,
'bitcoin' => $payment_addr,
'value' => $payment_value,
'pay_status' => $status
);
$json = json_encode($arr);
// check status if paid save output to new location and delete old file
if ($status == 'Confirmed Payment') {
file_put_contents('paid_data/'.$filename, $json);
unlink('data/'.$filename);
}
The problem I am facing is that file_put_contents('paid_data/'.$filename, $json); ends up a file with a bunch of NULL variables. If I remove the unlink the variables save just fine, when I add it back the variables are all NULL.
So how can I verify file_put_contents takes place before the unlinking happens?
Also.... WHY does this happen? Isn't php supposed to be linear and shouldn't file_put_contents finish before the next line is carried out? Everything I have read about file_put_contents suggests as much. So why does the unlink take place before writing the content to a new location?
I still hope for a better answer, but so far this is my working solution to the problem. I changed the final if statement to the following. This seems to solve the issue - but there really has to be a better way than this. This feels very "hacky".
if ($status == 'Confirmed Payment') {
file_put_contents('paid_data/'.$filename, $json);
$i = 0;
while ($i < 1000) {
$i++;
if (file_exists('paid_data/'.$filename)) {
unlink('data/'.$filename);
break;
}
}
}
After mimicking your file structure and seeding a few examples, I was able to execute your code as is with the expected results. However, file_put_contents willreturn false on failure, so you might try something like this:
if ($status == 'Confirmed Payment') {
if(!file_put_contents('paid_data/'.$filename, $json);){
print_r(error_get_last());
die;
}
unlink('data/'.$filename);
}
Your code as originally written should be fine, as far as I can see. Usually when I see the kind of behavior you're describing, the problem is that the script itself is being called twice (or more) and overlapping calls are manipulating the same file.
I would definitely put in some debugging statements to verify this; I don't know your environment, but a simple line written to a log will probably be enlightening.
I am working wiht RestApi in php and i want to check whether "Post Data" is empty or not with one condition but right now my "else" part is working instead of "if",Where i am wrong ? Here is my code
$email = $this->input->post('otpEmail');
$one = $this->input->post('one');
$two= trim($this->input->post('two'));
$three= trim($this->input->post('three'));
$four= trim($this->input->post('four'));
if(empty($email) && empty($one) && empty($two) && empty($three) && empty($four)) {
$responseJSON = array("Status" => false, "Message" => "Please enter your otpEmail,one,two,three,four");
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
}
else
{
$responseJSON = array("Status" => true, "Message" => "All data exist here");
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
}
Try this as your code seems logically incorrect ( replace && with || )
if(empty($email) && empty($codeBox1) && empty($codeBox2) && empty($codeBox3) && empty($codeBox4)) {
}else{
}
Hey fellow stackoverflow members,
I have been struggling with this for days and can't seem to figure this out.
My Issue:
PHP Notice: Undefined index: use_default in settings.php on line 111
That is the error/exception that is being thrown at me, but after making several attempts at fixing this, I just simply cannot get it to fully work. I just recently upgraded to version 5.4.24 in php from 5.2.17 which has to be the reason why this error is being thrown at me. When using the older php version, this exception never came up.
Anyway, this is what I have for line 111 in my settings.php:
if (isset($_FILES['myfile']) && !empty($_FILES['myfile']['name']) && $_POST['use_default'] != 'on') {
Entire section for it (Updated):
if(empty($errors) === true) {
if (isset($_FILES['myfile']) && !empty($_FILES['myfile']['name']) && $_POST['use_default'] != 'on') {
$newpath = $general->file_newpath($path, $name);
move_uploaded_file($tmp_name, $newpath);
} else if(isset($_POST['use_default']) && $_POST['use_default'] === 'on') {
$newpath = 'avatars/default_avatar_mc.png';
}
$clan_tag = htmlentities(trim($_POST['clan_tag']), ENT_QUOTES, "UTF-8"); // ENT_QUOTES - (review, may need revised)
$gamer_tag = htmlentities(trim($_POST['gamer_tag']), ENT_QUOTES, "UTF-8"); // ENT_QUOTES - (review, may need revised)
$gender = htmlentities(trim($_POST['gender']));
$day = htmlentities(trim($_POST['day']));
$month = htmlentities(trim($_POST['month']));
$year = htmlentities(trim($_POST['year']));
$country = htmlentities(trim($_POST['country']));
$occupation = htmlentities(stripslashes($_POST['occupation']));
$interests = htmlentities(stripslashes($_POST['interests']));
$bio = htmlentities(stripslashes($_POST['bio']));
$status = htmlentities(stripslashes($_POST['status']));
$xfire = htmlentities(trim($_POST['xfire']));
$steam = htmlentities(trim($_POST['steam']));
$image_location = htmlentities(trim($newpath));
$users->update_user( $clan_tag,
$gamer_tag,
$gender,
$day,
$month,
$year,
$country,
$occupation,
$interests,
$bio,
$status,
$xfire,
$steam,
$image_location,
$user_id);
header('Location: settings.php?success');
exit();
} else if (empty($errors) === false) {
echo '<p>' . implode('</p><p>', $errors) . '</p>';
}
Then the very last bit:
<input type="checkbox" name="use_default" id="use_default" /> <label for="use_default">Use default picture</label>
I only receive this exception error if a user uploads a new pic, - any help would be greatly appreciated...
Before you execute this line
if (isset($_FILES['myfile']) && !empty($_FILES['myfile']['name']) && $_POST['use_default'] != 'on') {
Do an isset on the use_default .. like this.
if(isset($_POST['use_default']))
{
if (isset($_FILES['myfile']) && !empty($_FILES['myfile']['name']) && $_POST['use_default'] != 'on') {
Right now I am using this code to grab a variable in my URL:
<?php
$transaction_id = $_GET['transaction_id'];
if($transaction_id == "") {
$transaction_id = 'NA';
}
?>
So far I have only been grabbing that single variable, but now I need to grab a total of 5 variables. Will everything still work properly and operate fast and smoothly if I just copy and paste multiple codes right next to each other like this:
<?php
$transaction_id = $_GET['transaction_id'];
if($transaction_id == "") {
$transaction_id = 'NA';
}
?>
<?php
$transaction_id2 = $_GET['transaction_id2'];
if($transaction_id2 == "") {
$transaction_id2 = 'NA';
}
?>
<?php
$transaction_id3 = $_GET['transaction_id3'];
if($transaction_id3 == "") {
$transaction_id3 = 'NA';
}
?>
Or is there a more efficient way to combine them all into one code?
Thanks for the help.
Make use of isset() construct
<?php
if(!isset($_GET['transaction_id'])) {
$transaction_id = 'NA';
}
if(!isset($_GET['transaction_id2'])) {
$transaction_id2 = 'NA';
}
if(!isset($_GET['transaction_id3'])) {
$transaction_id3 = 'NA';
}
?>
justo to be sure them all are setted and not empty
$na = 'NA';
$transaction_id = (isset($_GET['transaction_id']) && $_GET['transaction_id'] != '') ? $_GET['transaction_id'] : $na ;
$transaction_id2 = (isset($_GET['transaction_id2']) && $_GET['transaction_id2'] != '') ? $_GET['transaction_id2'] : $na ;
...
you could do a for loop
<?php
$transaction_id = array();
for($i=1;$i<6:$i++){
if( $_GET['transaction_id'.$i] == "") {
$transaction_id[] = 'NA'
}else{
$transaction_id[] = $_GET['transaction_id'.$i]
}
}
?>
This will make an array of transaction ids or NA
[0] => NA
[1] => 12345
[2] => 67890
[3] => 23454
[4] => NA
[5] => 55422
I don't know if this is better practice or not, maybe its just a different way.
Hope it helps.
P.S.
you will have to change your first $_GET['transaction_id']; to $_GET['transaction_id1'];
I have a form that contains a number of textboxes i.e. Volome, Gain, Treble, Middle and Bass. Only whole numbers can be entered, which is validated with javascript and the Maxlength is set to, so no problem there. But how do I make sure that only numbers between 0 and 65535 are entered.
<?php
$name = $_POST['ampMod'];
$volume = 'Volume = '. $_POST['volume'];
$gain = 'Gain = '. $_POST['gain'];
$treble = 'Treble = '. $_POST['treble'];
$middle = 'Middle = '. $_POST['middle'];
$bass = 'Bass = '. $_POST['bass'];
if($volume != null && $gain != null && $treble != null && $middle != null && $bass != null)
{
echo "<h3> $name </h3>";
echo "<table><tr>";
echo "<td>$volume</td>";
echo "<td>$gain</td>";
echo "<td>$treble</td>";
echo "<td>$middle</td>";
echo "<td>$bass</td>";
}
else
{echo ("Please try again. Values must be between 0-65535. 0=Off 65535=Full On 10<br>Click here to try again!");}
?>
It is important to mention that your $volume, $gain, $treble, $middle and $bass will never actually be null as you have assigned a string to them in addition to the $_POST value. In addition you should always check if the $_POST values exist before trying to use them (or you will get an undefined notice message).
Here is an example for a PHP version based on the code you had (untested, but should work fine).
<?php
function isValidRange( $value, $low = 0, $high = 65535) {
// validate / cast value as int (add additional validation here
$value = (int)$value;
if ( $value > $high || $value < $low ) {
// return null (not a valid value)
return null;
}
// otherwise the value is valid so return it
return $value;
}
// make sure the $name var is safe to use
$name = ( isset($_POST['ampMod']) ) ? htmlentities($_POST['ampMod'],ENT_QUOTES,'UTF-8') : null;
$volume = ( isset($_POST['volume']) ) ? isValidRange($_POST['volume']) : null;
$gain = ( isset($_POST['gain']) ) ? isValidRange($_POST['gain']) : null;
$treble = ( isset($_POST['treble']) ) ? isValidRange($_POST['treble']) : null;
$middle = ( isset($_POST['middle']) ) ? isValidRange($_POST['middle']) : null;
$bass = ( isset($_POST['bass']) ) ? isValidRange($_POST['bass']) : null;
if( isset($volume) && isset($gain) && isset($treble) && isset($middle) && isset($bass) )
{
echo "<h3> $name </h3>";
echo "<table><tr>";
echo "<td>Volume = $volume</td>";
echo "<td>Gain = $gain</td>";
echo "<td>Treble = $treble</td>";
echo "<td>Middle = $middle</td>";
echo "<td>Bass = $bass</td>";
echo "</tr></table>";
} else {
echo ("Please try again. Values must be between 0-65535. 0=Off 65535=Full On 10<br>Click here to try again!");}
?>
Lastly I would not recommend just relying on JavaScript to actually check if your values are safe to use (i.e. echo them out), but using js as a pre-warning to users and then properly validating with PHP is the best way to go.
Just do something like this? Don't know why you would want to go between 0 and 65535. I doubt you want them to go that high. If you do just change 10 to 65535
if($value > 10 || $value < 0)
{
echo "Value cant be higher then 10 or lower then 0";
}
This makes sure the value is between 10 and 0
In situations like this, I often prefer to silently clean the form input. You've got client-side validation in place already. If the value is higher than allowed, just set the value to the maximum allowed instead of showing an error message.
// Clean the posted data and prevent notices if not set
$volume = (isset($_POST['volume'])) ? (int) $_POST['volume'] : 0;
// Make sure the value is within a certain range
$min = 0;
$max = 10;
$volume = min($max, max($min, $volume));
You can make use of the filter extension (bundled by default since 5.2):
$FILTER_VALIDATE_KNOB = array(
'filter' => FILTER_VALIDATE_INT,
'options' => array(
'min_range' => 0,
'max_range' => 65535,
)
);
$res = filter_input_array(INPUT_POST, array(
'ampMod' => $FILTER_VALIDATE_KNOB,
'volume' => $FILTER_VALIDATE_KNOB,
'gain' => $FILTER_VALIDATE_KNOB,
'treble' => $FILTER_VALIDATE_KNOB,
'middle' => $FILTER_VALIDATE_KNOB,
'bass' => $FILTER_VALIDATE_KNOB,
));
if (is_null($res) || in_array(null, $res, true)) {
// some or all fields are missing
// - missing fields have null value
} elseif (in_array(false, $res, true)) {
// some or all fields have a wrong value
// - wrong values have false value
}
I would do it with javascript. That way, you wouldn't have to submit the form and if the user types a higher number the alert (or something nicer) is shown:
In the input field, just call the javascript function:
<input id="thefirstnumbervalue" type="text" onchange="checknumber('thefirstnumbervalue')" />
<input id="thesecondnumbervalue" type="text" onchange="checknumber('thesecondnumbervalue')" />
In the function:
function checknumber(theid){
var mynumbervalue = document.getElementById(theid).value;
if (mynumbervalue > 65535){
document.getElementById(theid).value = "65535";
alert("Please try again. Values must be between 0-65535. ...");
}
if(mynumbervalue < 0){
document.getElementById(theid).value = "0";
alert("Please try again. Values must be between 0-65535 ...");
}
}
This is a simple approach in raw javascript. If you use ajax and jquery the result could be easier and nicer. This is complementary to the php solution, as you should also check the data before inserting in your database.