I have a checkdate php code and I need to replace the dates with some kind of php/jscript code. Maybe some html form element, where I can insert the date how it is needed, submit it and then the date inside the file gets replaced. In this example the date inside this lines:
<?PHP
function isValidDate($sd, $ed, $currentDate = null)
{
if ($currentDate === null) {
$currentDate = date('Y-m-d');
}
return ($currentDate >= $sd && $currentDate <= $ed);
}
$startDate = '2016-10-30';
$endDate = '2016-10-31';
if (isValidDate($startDate, $endDate)) {
header("Location: x.php");
} else {
header("Location: y.htm");
}
?>
In this example in these lines
$startDate = '2016-10-30';
$endDate = '2016-10-31';
the date should be replaced.
It should be possible with some kind of html5 input/form element, where someone can insert the date like
<input type="text" value="XXXX-XX-XX" name="start"/>
<input type="submit" value="startdate"/>
and <input type="text" value="XXXX-XX-XX" name="end"/>
<input type="submit" value="enddate"/>
After submitting the date should be replaced via php/jscript or something like this.
Is this even possible? Can anybody push me in the right direction, maybe with some tutorial links f.e.?
Thanks in advance.
I think you need to use the $_POST array.
First have a form like:
<form method="POST" action="your_file_name.php">
<input type="text" name="start_date"/>
<input type="text" name="end_date"/>
<input type="submit" value="my_form_dates"/>
</form>
and then change your program to
<?php
function isValidDate($sd, $ed, $currentDate = null)
{
if ($currentDate === null) {
$currentDate = date('Y-m-d');
}
return ($currentDate >= $sd && $currentDate <= $ed);
}
if (is_set($_POST['start_date']) && is_set($_POST['end_date'])) {
$startDate = $_POST['start_date'];
$endDate = $_POST['end_date'];
}
if (isValidDate($startDate, $endDate)) {
header("Location: x.php");
} else {
header("Location: y.htm");
}
?>
As you may see, you don't need that value="XXXX-XX-XX" attributes, because the values of the fields will be typed by the user at runtime.
Don't forget make your_file_name.php in the form match with the name of your program, be it .php or .html.
I suggest you to read more about this $_POST array at this page.
Related
Saw other examples close to this, but I seem to still be having issues. The issue is with the script for the third error checker below (#bid < #budm). The amount of the bid field cannot be lower than the amount of the set budget, which #budm should be storing. Pulling a value for #budm from database related to the post id, using php. trying to pass that variable into a js error checker that compares the hidden input field (id="budm"), which stores the php variable, against a user entered input field (id="bid"). Seems to work on random. For instance, on a post where the value of $budgetmin was 400, most entries under 400 were not accepted (which is good), but for some reason, the value '9' was.
<?php
global $current_user;
get_currentuserinfo();
$cid = $current_user->ID;
$cwd = str_replace('wp-admin','',getcwd());
$post = get_post($pid);
$budgetmin = get_post_meta($pid, 'budget_start', true); ?>
<input type="hidden" id="budm" value="<?php echo $budgetmin; ?>">
<script type="text/javascript">
function check_submits()
{
if( jQuery("#days_done").val().length == 0 )
{
alert("<?php _e('Error text3'); ?>");
return false;
}
if( jQuery("#bid").val().length == 0 )
{
alert("<?php _e('Error text2'); ?>");
return false;
}
if (jQuery('#bid').val() < jQuery('#budm').val())
{
alert("<?php _e('Error text'); ?>");
return false;
}
return true;
}
</script>
<input type="text" name="bid" id="bid" class="bid_field" value="<?php echo $bid; ?>" size="10" />
<input class="green_btn grey" style="font-size: 20px; padding-left: 15px; padding-right: 15px; font-weight: 400;" id="submits_crt" type="submit" name="bid_now_reverse" value="<?php echo "Submit"; ?>" />
Any assistance is greatly appreciated!
The problem is that .val() returns a string, not a number. The test is comparing the 2 values as strings, which is not what you want and doesn't behave as you expect.
You need to cast your values as numbers before the test will work. If you know they're always going to be integers, you could use:
var bid=parseInt(jQuery('#bid').val()),
budm=parseInt(jQuery('#budm').val());
if (bid < budm) {
alert("Bid is lower than budget");
}
You could use parseFloat() if the inputs could be floats.
Here's a jsFiddle showing this working.
I have a form that gives me data from database, i have number input type. By default it is "0" showed for empty entries. I want to hide "0" from the field and show the value just if is different of 0.
I tried with the code below but it doesn't work.
<input data-validate="number" value="<?php echo $value; ?>" class="form-control" onload="if(this.value == '0') { this.value = ' '; } " >
Add ternary operator to PHP block instead:
<input data-validate="number" value="<?php echo ($value != '0' ? $value : ''); ?>" class="form-control">
I wrote a minimal php function to do this
function fnB($input){
//function to make values blank in number input boxes
if ($input==0) $r="";
else $r=$input;
return $r;}
?>
So in the form one can then enter
value = <?php echo fnB($value);?>
I am having some trouble figuring out a way to pass a javascripts value to a .php script, and then from there to a .txt script.
When doing this with a regular number it works, but when I want to do it with the variable which it has to be, the .txt file is left blank and nothing has been added to the file. I have searched the web "dry" for options, and I just can't figure out how to make it work. currently my scripts look like below and
I believe that is the right way of doing it, there is just some problem with it as I said, and as I also said I believe the mistake is in the part from the javascript file to the .php file.
javascript/html:
<form id="pg-form" action="chargeCard.php" method="POST" name="pg-form">
<input onkeypress="return isNumberKey(event)" type="text" name="amount" id="amount" />
<input type="hidden" id="curValueField" value=""/> <!-- this is where I am trying to pass the value -->
<input type="image" src="pgBut1.png" id="pgBut" value="submit" alt="butPG"/>
</form>
<script type="text/javascript">
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
<script type="text/javascript">
window.onload(function(){
var currentVarValue = 1; //this is the variable value I am trying to pass
document.getElementById("currentVarValue").innerHTML = currentVarValue;
document.getElementById("curValueField").value = currentVarValue; //this is where I get the variable value and make the new id to get in the html form
});
</script>
php:
<?php
require_once('./stripe-php/init.php');
\Stripe\Stripe::setApiKey("removed for security");
$token = $_POST['stripeToken'];
$myAmount = $_POST['amount'];
$describtion = $_POST['description'];
$curValue= $_POST['curValueField']; //this is where I try to get the variable value
$myAmount = round((int)$myAmount*100,0);
try {
$charge = \Stripe\Charge::create(array(
"amount" => $myAmount,
"currency" => "usd",
"source" => $token,
"description" => $describtion));
//pass value to .txt file start
$filename = "getVarValue.txt";
$content = file_get_contents($filename);
$content .= $curValue;
file_put_contents($filename, $content);
//pass value to .txt file end
} catch(\Stripe\Error\Card $e) {
}
?>
Your curValueField doesn't have a name, so its value is never sent to your PHP.
Try this :
<input type="hidden" id="curValueField" name="curValueField" value=""/>
I think you forgot to "name",
try
<input type="hidden" id="curValueField" name="curValueField" value=""/>
And to .txt, can try below code
$fp = fopen("test.txt", "a");
if($fp) {
fwrite($fp,$msg);
}
fclose($fp);
I am trying to send a JavaScript variable to PHP but not exactly sure how to do it, a few things have said Ajax but I've never used it before and can't get my head around it. Does anyone know what the easiest way to do this would be? The column which I am attempting to populate in my DB is called 'cogs'.
I have the following JavaScript code:
<script>
$(document).ready(function() {
$('#duration-select').change(function() {
var cogs = $('#cogsday').html();
cogs = cogs.replace(/\D/g,'');
var x =$('#duration-select').val();
var y = cogs * x;
$('#request').removeClass('hidden');
$('#duration-value').text('Total cost for this duration = ' + (y) + ' cogs');
if($(this).val() !== '') {
} else {
$('#duration-value').text('');
}
});
$('#request').click(function() {
var cogs = $('#cogsday').html();
cogs = cogs.replace(/\D/g,'');
var x =$('#duration-select').val();
var y = cogs * x;
$('#total').text(y);
});
});
</script>
And the following HTML code:
<label id="total"></label>
Here is where I am trying to post the data, everything else is posting except for the $cost:
<form name="form" method="post">
<div class="modal-footer">
<?php
if ($row3['availability'] === 'Available') {
if (isset($_POST['request'])) {
$to_id = $row3['customerid'];
$from_id = $_SESSION['customerid'];
$time_sent = date('Y-m-d H:i:s');
$subject = 'Request for ' . $row3['title'];
$title = $row3['title'];
$listingid = $listingid;
$cost = $_POST['total']; //posting 0
$message = $customer_data['first_name'] . ' ' . $customer_data['last_name']
$request = mysql_query("INSERT INTO messages (to_id, from_id, listing_id, time_sent, subject, message, cogs, messagenumber, title, msgrand) VALUES ('$to_id', '$from_id', '$listingid', '$time_sent', '$subject', '$message', '$cost', '1', '$title', '$randomString')") or die(mysql_error());
}
}
?>
<input type="submit" class="btn btn-success" name="request" value="Yes" />
<input type="submit" class="btn btn-danger" data-dismiss="modal" value="No" />
</div>
</form>
Then I am trying to post the value of the label id=total to my db or the JavaScript variable (y). The problem is that 0 is always being sent to the DB when it should instead be the value that is in the label where the id is total.
Use name parameter for hidden variable and it will be automatically passed to PHP .
<label id="total"></label>
<input type="hidden" name="total" id="nameID"/>
in javascript below $('#total').text(y); write $('#nameID').val(y); . Everything will work properly.
You used total label , but $_POST recognizes only input type so use input type=.... instead of a label,divs etc.
IF YOU REAllY NEED ANSWER REPLY HERE
you have make an input type and its value is to be set by that javascript and then you'll be able to get that $cost value in php code
<input type="hidden" value="" name="total" id="total">
..................
$("#total").val(y);
You can use this to send the variables....
<input type="text" id="name" class="name" placevalue="Enter you name" required /><br><br>
<input type="text" id="email" class="email" placevalue="Enter you name" required /><br><br>
<button id= "det_submit" onclick="submit_det()"> Submit </button>
<script>
function submit_det() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
if(name != "" && email != "") {
$.post(
'xx.php',
{
name : document.getElementById("name").value,
email1 : document.getElementById("email").value,
},
function(data){
alert(data);
});
} else {
alert("empty");
}
}
</script>
here is xx.php
<?php
if(isset($_POST['name']) && isset($_POST['email1'])) {
$name = $_POST['name'];
$email = $_POST['email1'];
//code to insert into your database......
}
?>
Use a ID and Name for hidden parameter like this
<label id="total"></label
<input type="hidden" name="name" id="name"/>
and in jQuery edit the code like this
$('#total').text(y);
$('#nameID').val(y);
hope that it will work
i have a form that includes several text inputs and checkboxes (the checkboxes comes from a DB), so... i know how to validate them separetly but i need to validate them together, the way i'm doing this only validate checkboxes, i know why its happening but i don't know how to write the right way... ¿can you help me? here is the code:
<form action="sendreq.php" name="contact" onsubmit="return valida_frm(this)" method="post">
<label>Name</label>
<input name="name" type="text" />
<label>Email</label>
<input name="email" type="text"/><!-- And Severeal inputs then the checkboxes-->
<?php $list3 = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 20");
while($row = mysql_fetch_object($list3)){ ?>
<input id="product" name="product[]" class="label" type="checkbox" value="<?php echo $row->name?>"><label class="label"><?php echo $row->name?></label>
<?php }?>
The Validation doesn't work fine its evident why, i just need the right way to write and unify the return of the alert:
function valida_frm(form){
var alerta="Ooops:\n";
if (form.name.value == "") {alerta+="Name.\n";}
if (form.email.value == "") {alerta+="Email.\n";}
for(var i = 0; i < form.product.length; i++){
if(form.product[i].checked)return true;}
alert('Oooops');
return false;
if (alerta!="Error:\n"){
alert(alerta);
return false;
}else{
return true;
}
}
Thanks for your time!
Do not call a field for "name" and then test form.name since it already has a .name
Then test form["product[]"] and not form.product - you cannot have id="product" since ID has to be unique!
I suggest you give id="product<?echo $somecounter; ?>" />...<label for="product<? echo $somecounter; ?>">...</label>
Also test against Error (or nothing as in my suggesion) and not Oops
Also more issues fixed
DEMO
function valida_frm(form){
var alerta="";
if (form.name.value == "") {alerta+="Name.\n";} // Please use FullName or such
if (form.email.value == "") {alerta+="Email.\n";}
var chks = form["product[]"],
checked = false;
for(var i = 0; i < chks.length; i++) {
if(chks[i].checked) {
checked = true;
break;
}
}
if (!checked) {
alerta+='Product.\n';
}
if (alerta){
alert("Error:\n"+alerta);
return false;
}
return true;
}