I've got a JavaScript function, that as an array, and a HTML form that sends data to a PHP page to later send it by email. But I would also need to send the result of the JavaScript function on the form to the php page. Is this possible?
I'm currently trying the following way, but with no results:
HTML page and script :
function showcartitems() {
var showcart = paypal.minicart.cart.items();
}
<form action="teste4.php" method="post" class="creditly-card-form agileinfo_form" onsubmit="showcartitems()">
<section class="creditly-wrapper wthree, w3_agileits_wrapper">
<div class="information-wrapper">
<div class="first-row form-group">
<div class="controls">
<label class="control-label">Nome Completo: </label>
<input class="billing-address-name form-control" type="text" name="name" placeholder="Nome Completo">
<input type="hidden" name="cart" value="showcart" />
</div>
<div class="w3_agileits_card_number_grids">
<div class="w3_agileits_card_number_grid_left">
<div class="controls">
<label class="control-label">Numero de telemóvel:</label>
<input class="form-control" type="text" name="mobile" placeholder="Numero de telemóvel">
</div>
</div>
<div class="w3_agileits_card_number_grid_left">
<div class="controls">
<label class="control-label">Email:</label>
<input class="form-control" type="text" name="email" placeholder="Email">
</div>
</div>
<div class="w3_agileits_card_number_grid_right">
<div class="controls">
<label class="control-label">Rua: </label>
<input class="form-control" type="text" name="rua" placeholder="Rua">
</div>
</div>
<div class="clear"> </div>
</div>
<div class="controls">
<label class="control-label">Cidade: </label>
<input class="form-control" type="text" name="cidade" placeholder="Cidade">
</div>
<div class="controls">
<label class="control-label">Método de pagamento: </label>
<select name="pagamento">
<option>MBWay</option>
<option>Transferência Bancária</option>
<option>Multibanco (apenas para entregas ao domicílio)</option>
</select>
</div>
</div>
<button class="submit check_out">Verificar disponibilidade!</button>
</div>
</section>
</form>
The PHP page is still quite simple, just checking if the variables are coming correctly, they all are, except the value of the javascript function:
echo $_POST['name'];
echo $_POST['mobile'];
echo $_POST['email'];
echo $_POST['rua'];
echo $_POST['cidade'];
echo $_POST['pagamento'];
echo $_POST['showcart'];
?>
Am I doing something wrong? Is there a better way to do this, or is it just impossible?
EDIT 1:
Following #epascarello suggestion, I've managed to get the value into the PHP side, which is returning the following:
[{"_data":{"cmd":"_cart","add":"1","item_name":"FILA 1010302","item_image":"images/1010302-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":2,"href":"http://teste/index.html#5"},"_options":[],"_discount":100,"_amount":99,"_total":98,"_eventCache":{"change":[[null,null]]}},{"_data":{"cmd":"_cart","add":"1","item_name":"FILA 1010575","item_image":"images/1010575-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":1,"href":"http://teste/index.html#5"},"_options":[],"_discount":50,"_amount":99,"_total":49,"_eventCache":{"change":[[null,null]]}},{"_data":{"cmd":"_cart","add":"1","item_name":"FILA 1010707","item_image":"images/1010707-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":1,"href":"http://teste/index.html#5"},"_options":[],"_discount":50,"_amount":99,"_total":49,"_eventCache":{"change":[[null,null]]}},{"_data":{"cmd":"_cart","add":"1","item_name":"FILA SCM00514","item_image":"images/scm00514-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":1,"href":"http://teste/index.html#5"},"_options":[],"_discount":50,"_amount":99,"_total":49,"_eventCache":{"change":[[null,null]]}}]
I've tried several approaches with json_decode, but I'm not getting the result I seek.
I would like to split that array into, for example:
item_name=x;
item_image=x;
quantity=x;
Can someone pin point me into the right direction? Sorry for asking this, I'm new in this languages and despite my research I did not manage to get to a solution on my own.
Thank you in advance.
So you have <input type="hidden" name="cart" value="showcart" /> so you need to set the value of the field. That value does not magically reference the JavaScript value you created.
Give the input element an id, so it is easier to reference
<input type="hidden" name="cart" id="cart" />
and set the value of the input in your method that is called on submit
function showcartitems() {
document.getElementById('cart').value = JSON.stringify(paypal.minicart.cart.items());
}
After #epascarello solution, to be able to decode de JSON string, I've followed #RaymondMutyaba suggestion which works pretty properly:
$item_name = json_decode($_POST['cart'], true)[0]["_data"]["item_name"];
$array_of_items_in_cart = json_decode($_POST['cart'], true);
$first_item_in_cart = array_of_items_in_cart[0];
$name_of_first_item_in_cart = first_item_in_cart["_data"]["item_name"];
$first_item_discount = first_item_in_cart["_discount"];
I have first, last name, and email input fields. The requirement from the customer is to have email field required only if either first or last name value exist. If there is no value entered in either of those two fields email should not be required. I have developed this code that works assuming that form is always empty. The problem is the case when user already has data entered in there and user is loading the form. In that case my code is not useful. Here is working example of what I have so far.
$(".check").on("keyup blur", function() {
var fldVal = $(this).val();
if (fldVal) {
$("#email").closest("div").addClass("required");
$("#email").prop("required", true);
} else {
$("#email").closest("div").removeClass("required");
$("#email").prop("required", false);
}
});
.row {padding: 3px;}
.required label:after { content: " *"; color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="text" id="test">
<div class="row">
<label>First Name:</label>
<input type="text" name="first" id="first" class="check">
</div>
<div class="row">
<label>Last Name:</label>
<input type="text" name="last" id="last" class="check">
</div>
<div class="row">
<label>Email:</label>
<input type="email" name="email" id="email">
</div>
<button type="button" name="submit" id="submit">Submit</button>
</form>
I would like for my code to check all the requirements on page load. If possible that should all be done with one function. I can't think of a good way to achieve that. Please let me know if you have any ideas.
I have a onepage site and I found this code for a domain checker, it works great but the problem is that when I submit the search it goes to the top of the page and I need to scroll back down to see the result. Can anyone help me so when I do the search it stays at the same place? I think ajax will do the job but I have not knowledge about it, and don't even know how to search for what I need.
<div class="container">
<div class="row">
<div class="col-md-6">
<h3>Check Domain Name Availability</h3>
<form action="" method="GET">
<div id="custom-search-input">
<div class="input-group col-md-24" >
<input type="text" name="domain" class="form-control input-lg" placeholder="Example.com or Example.in etc." />
<span class="input-group-btn">
<button type="submit" class="glyphicon glyphicon-search"></button>
</span>
</div>
</div>
</form>
<?php
error_reporting(0);
if(isset($_GET['domain'])){
$domain = $_GET['domain'];
$godaddycheck = 'https://in.godaddy.com/domains/searchresults.aspx?checkAvail=1&tmskey=&domainToCheck='.$domain.'';
$namecomcheck = 'https://www.name.com/domain/search/'.$domain.'';
$registercomcheck = 'http://www.register.com/domain/search/wizard.rcmx?searchDomainName='.$domain.'&searchPath=Default&searchTlds=';
if ( gethostbyname($domain) != $domain ) {
echo "<h1>Already Registered!</h1>";
}
else {
echo "<h3>$domain</h3><h2><br>Not Taken, you can register it.
</h2>";
}
}
?>
</div>
</div>
</div>
I did a form validation where server checks submitted data. If data passes validation, then redirect to previous page, if not, a modal box will pop up tells user wrong username/password.
The question is, after user resubmit the form, redierct function doesn't work. It only works when user first time successfully input those fields. Can anyone help me with this please?
The html code:
<div id="formInfo">
<form method="post" action="<?PHP echo $_SERVER['PHP_SELF'];?>">
<span class="title">Sign in to urmajesty.net</span><br><br>
<div id="input_container">
<input type="text" name="username" placeholder="username"><img src="Images/icons/user.png" id="input_img"><br><br>
</div>
<div id="psw_container">
<input type="password" name="psw" placeholder="password"><img src="Images/icons/key.png" id="key_img"><br><br>
</div>
<div id="checkbox">
<input type="checkbox" name="RememberUsername" value="Yes"><span class="checkboxtxt">Remember my username</span>
</div><br><br>
<div id="Forget">
<span class="forget1">Forget Password</span><span class="forget2">Forget Username</span>
</div>
<input type="submit" class="button" value="SIGN IN"><br><br>
<div id="hispan"><p class="hip"><span class="hi">New to urmajesty.net?</span></p></div><br>
<input class='disable_me' name="referer" type="hidden" value="<?php echo urlencode($_SERVER['HTTP_REFERER'])?> " />
<button type="button" class="button" id="btn">CREATE ACCOUNT</button>
</form>
</div>
<div id='modal_box'>
<div class='modal_content'>
<span class='close'>×</span><p>username/password is wrong. Please try again!</p>
</div>
</div>
The php code:
if($_SERVER["REQUEST_METHOD"]=="POST"){
$validation=Login();
if($validation==false){
echo"<script>
var modal_box=document.getElementById('modal_box');
modal_box.style.display='block';
modal_box.style.backgroundColor='rgba(0,0,0,0.4)';
var close=document.getElementsByClassName('close')[0];
close.onclick=function(){
modal_box.style.display='none';
}
window.onclick=function(event){
if(event.target==modal_box){
modal_box.style.display='none';
}
}
</script>";
}
else{
if(!empty($_POST['referer']) && !is_array($_POST['referer'])){
header("Location: ".urldecode($_POST['referer']));
exit();
}
}
}
?>
Use javascript to redirect.
if(!empty($_POST['referer']) && !is_array($_POST['referer'])){ ?>
//header("Location: ".urldecode($_POST['referer']));
<script>
window.location.href = "<?=$_POST['referer']?>";
</script>
<?php
exit();
} ?>
Apart from using header() to redirect, you can use meta refresh method, or JS window.location method.
<script>
window.location = "<?=$_POST['referer']?>"
</script>
[edited] I'm trying to include an email signup box on my website with Parse on the backend.
Problem: The email ids entered aren't getting saved into the Parse database.
here's the HTML with corresponding JS. I'm still learning JS so I might have picked up some code from here and there.
<form id="signup-form" class="align-center">
<div class="form-group align-center" style="width: 90%;">
<input type="text" class="form-control" name="email" id="email" placeholder="get informed when it's online!" style="font-size: 1em; width: 100%; float: left;"/>
<button id="signup-button" class="btn btn-default" style="margin-top: 10px;" >count me in!</button>
</div>
</form>
Javascript:
$(document).ready(function() {
$("#signup-button").click(function() {
Parse.$ = jQuery;
Parse.initialize("n7cfi9v9FErlM13bSC4qb6obHr0c9lSNmEgyBGTB", "NHA6CciUx6xXCvrQwHGTG48D7ggItvUlrYE36mTT");
var SignupList = Parse.Object.extend("signup_list");
var signup = new SignupList();
signup.save({email: $('#email').val()}).then(function(object) {
alert($('#email' + " enrolled in list!").val());
});
});
});
Don't know what I'm doing wrong. Help.