How can i get HTML input to PHP without submitting? - javascript

Basically, i got a form/submit form, but it doesn't post to my site since it's a paypal payment gateway but i need a value from one of the text input fields how can i accomplish this without post/get? I heard you can do it with JS, i tried some JS code but it didn't really work
PAYPAL_URL is a defined variable in config_payment.php that's just a PayPal URL.
My code:
<form action="<?php echo PAYPAL_URL; ?>" method="post">
<div class="inform">
<fieldset>
<div class="fakeform">
<table class="aligntop">
<tbody>
<tr>
<th scope="row">Username</th>
<td><input class="payment-field" id="username" type="text" name="username" value="" maxlength="80"></td>
</tr>
<tr>
<th scope="row">User ID</th>
<td><input class="payment-field" id="userid" type="text" name="userid" value="<?php echo $pun_user['id'] ?>" maxlength="80"></td>
</tr>
<tr>
<th scope="row">Email</th>
<td><input class="payment-field" id="email" type="email" name="email" value="" maxlength="80"></td>
</tr>
<tr>
<th scope="row">Plan</th>
<td>
<select name="validity" onchange="getSubsPrice(this);" class="payment-field">
<option value="1" selected="selected">1 Month - $20</option>
</select>
</td>
</tr>
<script type="text/javascript">
var getuserid=document.getElementById('userid').value;
</script>
<?php
$phpVar = "<script>document.writeln(getuserid);</script>";
?>
<!-- Identify your business so that you can collect the payments -->
<input type="hidden" name="business" value="<?php echo PAYPAL_ID; ?>">
<!-- Specify a subscriptions button. -->
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<!-- Specify details about the subscription that buyers will purchase -->
<input type="hidden" name="item_name" value="<?php echo $itemName; ?>">
<input type="hidden" name="item_number" value="<?php echo $itemNumber; ?>">
<input type="hidden" name="currency_code" value="<?php echo PAYPAL_CURRENCY; ?>">
<input type="hidden" name="a3" id="paypalAmt" value="<?php echo $itemPrice; ?>">
<input type="hidden" name="p3" id="paypalValid" value="1">
<input type="hidden" name="t3" value="M">
<!-- Custom variable user ID -->
<input type="hidden" name="custom" value="<?php echo $phpVar; ?>">
<!-- Specify urls -->
<input type="hidden" name="cancel_return" value="<?php echo PAYPAL_CANCEL_URL; ?>">
<input type="hidden" name="return" value="<?php echo PAYPAL_RETURN_URL; ?>">
<input type="hidden" name="notify_url" value="<?php echo PAYPAL_NOTIFY_URL; ?>">
</div>
</tbody>
</table>
<div class="centered">
<input class="buy-btn" type="submit" value="Buy Subscription">
</div>
</div>
</fieldset>
</div>
</div>
</div>
</form>
As you can see i tried some JS and PHP merging, but it's still 0 when i retrieve it and post it
<script type="text/javascript">
var getuserid=document.getElementById('userid').value;
</script>
<?php
$phpVar = "<script>document.writeln(getuserid);</script>";
?>
Please if anyone can point out what im doing wrong, im eternally greatful thanks.

PHP runs before any JavaScript executes so what you want to do ins not possible.
Seems like you want the value to update another field if it changes.
<input type="hidden" name="custom" value="<?php echo $pun_user['id'] ?>">
and add a simple script
document.querySelector("#userid").addEventListener("change", function(){
document.querySelector('[name="custom"]').value = this.value;
});
Simpler solution, get rid of the hidden element and rename the userid field.
<td><input class="payment-field" id="userid" type="text" name="custom" value="<?php echo $pun_user['id'] ?>" maxlength="80"></td>

A better approach in my opinion would be to use your server as the middle man between the 'User' and 'PayPal'.
That is, instead of the User submitting directly to PayPal, you could have the User submit to your server instead. With that, you have access to that input field you really wanted and you're able to also validate that form.
Finally, your server would resubmit the same request to PayPal, receive the response, perform other necessary operations, and send it back to the User.
i.e
User -- PayPal request ---> Server
Server -- validate form, get that input field you want, resubmit request ---> PayPal
PayPal -- PayPal response---> Server
Server -- perform some operations i.e. saving to database, send PayPal response---> User

Related

How do I submit two forms with one button? (the solutions provided by other similar issues are not working for me)

I have 2 forms (and will have more in the future) that I need to submit. Right now, when the submit button is clicked, it will only submit the form in the same collapsible section. Hence, I need a "Submit All" button outside these sections to submit all the forms.
I've tried implementing AJAX and tried out the solution from this link Submit two forms with one button , but it's still not working for me.
submitForms = function(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();
}
<html>
<body>
<form id="form1" action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div style="background-color:lightblue" class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
<label><?php echo $question; ?></label>
<input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
<span class="help-block"><?php echo $answer_err;?></span>
<input type="hidden" name="consideration_no[]" value="<?php echo $consideration_no; ?>"/>
<input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
</div>
<input type="Submit" name = "$consideration_no[]" class="btn btn-primary" onclick="submitForms()" value="Submit">
Cancel
</form>
</div>
<form id="form2" action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
<label><?php echo $question; ?></label>
<input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
<span class="help-block"><?php echo $answer_err;?></span>
<input type="hidden" name="consideration_no[]" value="<?php echo $consideration_no; ?>"/>
<input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
</div>
<input type="Submit" name = "$consideration_no[]" class="btn btn-primary" onclick="submitForms()" value="Submit">
Cancel
</form>
</div>
<input type="button" value="Submit All!" onclick="submitForms()" />
</body>
</html>
When I click on the last button to Submit All, nothing happens.
The html forms are inside php if else and while loop scripts, but I doubt that affects anything?
(I welcome other methods and ideas of submitting these two forms from a single button.)
------------UPDATE------------------------
I tried removing the actions and used this:
submitForms = function(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();
}
It wouldn't work too. In my POST handler, it has a:
if($stmt->execute()){
//Records Submitd successfully. Redirect to landing page
header("location: home1.php?dm_no=".$_GET["dm_no"]);
exit();
Would the redirection have affected the AJAX not being able to submit two forms? But even if it doesn't and only is able to submit one, I would still redirect to another page. Right now, the button doesn't seem to do anything.
I think it's not possible submit two form on one click. because form redirect on given form action, so if you submitting two from with two action then system not able to redirect two different actions.
But you can achieve this by doing ajax call Like:
<input type="button" value="Submit All!" onclick="submitForms()" />
<script>
function submitForms() {
$("#form1").ajaxForm({url: 'server.php', type: 'post'})
$("#form2").ajaxForm({url: 'server.php', type: 'post'})
}
</script>

Goes back to modal after pressing submit

Hey guys I'm currently having problems with my Modal. As I register a member, it doesn't go back to the modal after successfully registering it. I believe it has something to do with scripts. Here's the code:
Add Member
<!-- Add Member Modal -->
<div id="addMemberModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add Member</h4>
</div>
<div class="modal-body">
<div class="col-md-6 col-sm-6 no-padng">
<div class="model-l">
<form name="registration" method="post">
<table border="0" width="500" align="center" class="demo-table">
<div class="message"><?php if(isset($message)) echo $message; ?></div>
<tr>
<td><p class="textColor">First Name:</p></td>
<td><input type="text" class="demoInputBox" name="firstName" placeholder="First Name" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>"; required></td>
</tr>
<tr>
<td><p class="textColor">Family Name:</td>
<td><input type="text" class="demoInputBox" name="lastName" placeholder="Family Name" value="<?php if(isset($_POST['lastName'])) echo $_POST['lastName']; ?>"; required></td>
</tr>
<tr>
<td><p class="textColor">Contact</td>
<td><input type="text" class="demoInputBox" name="contact" placeholder="Contact No." value="<?php if(isset($_POST['contact'])) echo $_POST['contact']; ?>"; required></td>
</tr>
<tr>
<td><p class="textColor">Email</td>
<td><input type="text" class="demoInputBox" name="email" placeholder="Email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"; required></td>
</tr>
<tr>
<td><p class="textColor">Gender</td>
<td><input type="radio" name="gender" value="M" <?php if(isset($_POST['gender']) && $_POST['gender']=="Male") { ?>checked<?php } ?>><p class="textColor"; required>Male</p>
<br><input type="radio" name="gender" value="F" <?php if(isset($_POST['gender']) && $_POST['gender']=="Female") { ?>checked<?php } ?>><p class="textColor"; required>Female
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
<div>
<input type="submit" name="submit" value="Add Member" class="btnRegister">
</div>
</form>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I'm open to receiving answers and learning lessons from you guys as well because Boostrap Modal isn't being taught in our school. I'll be very happy if somebody will help me with this. Thank you so much! :)
First thing:
I really don't understand why you have used value="<?php echo $_POST['value']?>" with every input type what it does is, it fills form input with the submitted data. If you remove this value="<?php echo $_POST["firstName"] ?>" from every input type, you will only need to trigger the modal using the following code.
<script type="text/javascript">
$(document).ready(function(){
//check whether user form submitted
var test ="<?php echo $_POST["submit"] ?>";
//check if form is submmited
if(test){
$("#addMemberModal").modal("show");
}
});
</script>
If You still don't want to remove value="<?php echo $_POST['value']?>".Then try the following:
<script type="text/javascript">
$(document).ready(function(){
//check whether user form submitted
var test ="<?php echo $_POST["submit"] ?>";
// function for clearing input
$.clearInput = function (modal) {
$(modal).find('input[type=text], input[type=radio]').val("");
};
//check if form is submmited
if(test){
// //show the modal
$("#addMemberModal").modal("show");
//on modal show clear the inputs
$("#addMemberModal").on("shown.bs.modal",function(){
$.clearInput(this);
});
}
});
</script>
For any of the above case to work you also need to include Jquery in head section:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
NOTE: Above code should work as per your requirement but this is not a recommended method. If you want to add new member information again and again, you should submit the data via JQuery or Ajax post and on successful submission should reset the form, instead of reloading the entire page.

Pass JavaScript values to PHP

I need to pass JavaScript values and insert them in database via PHP. I have tried over the URL but it does not work.
Two variables needs to be inserted.
All values from database are printed in table, then I make selection.
The name of this file is items.php.
<td><input type="text" class="form-control" id="quantity" placeholder="" ></td>
<td><center><button type="submit" onclick="myFunction()" value="<?php echo 'R-' . $id_print ;?>" id="item"> <i class="icon-cart"></i></button></center></td>
I use this script to get values:
<script type="text/javascript">
function myFunction() {
var x = document.getElementById("quantity").value;
var y = document.getElementById("item").value;
window.location.href = "items?w1=" + x + "&w2=" + y;
}
</script>
I do not know why script does not redirect to another URL with values from the script.
I get both values when debugging.
If a <button> element has type="submit", clicking it submits the form, and therefore prevents execution of JavaScript click event handler assigned to this element. You should change its type to button, so it won't submit the form.
Corrected code:
<td><input type="text" class="form-control" id="quantity" placeholder="" ></td>
<td><center><button type="button" onclick="myFunction()" value="<?php echo 'R-' . $id_print ;?>" id="item"> <i class="icon-cart"></i></button></center></td>
You should use html-forms.
Because input-values are sent automatically when submit.
Look like this (not tested):
<!-- your html -->
<form action="items" method="post">
<!-- your table -->
<td><input type="text" class="form-control" id="quantity" name="w1" /></td>
<td><input type="text" class="form-control" id="item" name="w2" value="<?php echo 'R-' . $id_print ;?>" onclick="this.form.submit()" /></td>
</form>

How to get value of an input (checkbox input) in auto generated forms that displayed as table rows?

I generate table from database, assign each cell as checkbox value, group each row as form and display them all as table refer to [this].1 How I can get particular cell value when i click on the button since each form (row) have a same id. I want to pass the value as query parameter to update the database table. I have searched for similar technique and still not find the solution.
This code is within a cursor fetching loop:
<form class="tr" id="barisn" method="post" action="">
<span class="td"><input type="checkbox" name="id" value="<?php $data[0]?>"/><label for="id"><?php echo $data[0]?></label></span>
<span class="td"><input type="checkbox" name="name" value="<?php $data[1]?>" /><label for="name"><?php echo $data[1]?></label></span>
<span class="td"><input type="checkbox" name="gender" value="<?php $gender?>" /><label for="gender"><?php echo $gender?></label></span>
<span class="td"><input type="checkbox" name="sender" value="<?php $data[8]?>" /><label for="sender"><?php echo $data[8]?></label></span>
<span class="td"><input type="checkbox" name="date" value="<?php $data[5]?>" /><label for="date"><?php echo $data[5]?></label></span>
<span class="td"><input type="checkbox" name="time" value="<?php $data[6]?>" /><label for="time"><?php echo $data[6]?></label></span>
<span class="td"><input type="checkbox" name="state" value="<?php $state?>" /><label for="state"><?php echo $state?></label></span>
<div class="td">
<button class="btn-rawuh" type="submit" form="barisn" value="Rawuh">Rawuh</button>
<button class="btn-tundha" type="submit" form="barisn" value="Tundha">Tundha</button>
<button class="btn-mlebet" type="submit" form="barisn" value="Mlebet">Mlebet</button>
</div>
</form>
You have to put anchor tag instead of button like this
Edit
when you click on edit button then page will redirect to edit.php(you have to create new php file) then get the id from url :
echo $_GET method
then u will get the details of that particular row using id (using query).

How to get values of radio button in php file

I am trying to modify a joomla component and more specific a questionnaire that contains radio buttons. In php file the code is :
<form id="form-table" action="<?php echo JRoute::_('index.php?option=com_bisbas&task=table.save'); ?>" method="post" class="form-validate" enctype="multipart/form-data" target="iframe1">
<ul>
<input type="hidden" name="jform[id]" value="<?php echo $this->item->id; ?>" />
<input type="hidden" name="jform[state]" value="<?php echo $this->item->state; ?>" />
<?php echo $this->form->getInput('timecreated'); ?> <div class="control-group"><br><br><br>
<div class="control-label"><?php echo $this->form->getLabel('question1'); ?></div>
<div class="controls"><?php echo $this->form->getInput('question1'); ?></div><br><br>
and in firebug code is :
<div class="controls">
<fieldset id="jform_question1" class="radio" aria-required="true" required="">
<input id="jform_question10" type="radio" aria-required="true" required="" value="Yes" name="jform[question1]">
<label for="jform_question10">Yes</label>
<input id="jform_question11" type="radio" value="No" name="jform[question1]">
<label for="jform_question11">No</label>
<input id="jform_question12" type="radio" value="I am not sure" name="jform[question1]">
<label for="jform_question12">I am not sure</label>
</fieldset>
</div
I want when the user clicks the answer of radio button to check the value and print an alert. But I don't know how to do this in php file. I understand that
<div class="controls"><?php echo $this->form->getInput('question1'); ?></div>
prints the radio values based in an xml file that contains the values but how can I get them and check the value when the user selects it?
If I well understood your query I think that You must use javascript.
If your Joomla version is above 3.0, You can do it with jQuery :
jQuery('#radio_selector').on('click', function(){//verification script});

Categories