Getting form field values in php and then disabling it - javascript

i am using form in my page like this
<form method='post' action='' class='myform'>
<input type='text' name='name'>
<input type='text' name='email'>
<input class='btn' type='submit' name='submit' >
</form>
what i want i will get the values of form and then disable all its fields , now this is my code to disable all fields when form is submitted
$(document).on('click', '.btn', function(event) {
var currentForm = $(this).closest('form');
currentForm.find('input').prop('disabled', true);
});
and before this i'm trying to get values from $_POST like this
if(isset($_POST["submit"])){
$name= $_POST["name"];
$email= $_POST["email"];
}
but problem is that as i press submit button fields get disabled and i dont get any posted values , how do i get values first and then disable fields ?

Your mistake - You are disabling the input field by
currentForm.find('input').prop('disabled', true);
So when you submit the form, the field will be in disabled state and will not be posted.
Solution - please change the line to
currentForm.find('input').prop('readonly', true);

look this example or try with it....
if(isset($_POST["submit"]))
{
$name= $_POST["name"];
$email= $_POST["email"];
}
?>
<form method='post' action='' class='myform'>
<?php
if($name != '' && $email != '')
{
?>
<input type='hidden' name='name' value="<?php echo $name?>">
<input type='hidden' name='email' value="<?php echo $email?>">
<?php
}
?>
<input type='text' name="<?php echo ($name != ''?'hidden_name':'name')?>" <?php echo ($name != ''?'disabled':'')?> value="<?php echo $name?>">
<input type='text' name='<?php echo ($email != ''?'hidden_email':'email')?>' <?php echo ($email != ''?'disabled':'')?> value="<?php echo $email?>">
<input class='btn' type='submit' name='submit' >
</form>

You can use ajax for that like this:
$('.btn').bind('click', function() {
$.ajax({
url: "your page url here",
type: 'POST',
async:false,
success: function(response) {
$('input').prop('disabled', true);
}
});
});

you don't pass submit parameter to formdata, that's why you can't get it.
try this
<?php if(isset($_POST['name'])){ echo $_POST['name'] }?>

Related

Trying to echo a column from a table that is displayed via for each

I am using this table and trying to echo the "branchid" in an alert
There are two tables:
orders_address.php
<?php
session_start();
require_once('orders_address.vc.php');
?>
Here is a snippet of my for each table, the 'branchid' and the assign button is only the concern here'
<td>
<a href="order_address.vc.php<?php echo '?branchid='.$rowAddress['branchid']; ?>">
<input type="submit" class="btn button-color-blue font-color-white full_width" name="assign" value="ASSIGN">
</a>
</td>
<td class="table-text-center">
<?php
echo($rowAddress['branchid']);
?>
</td>
orders_address.vc.php
Below is the code when the button is clicked
if (isset($_POST['assign']) && $_POST['assign'] == 'ASSIGN')
{
$branchid = $_GET ['branchid'];
echo "<script type='text/javascript'>alert('$branchid');</script>";
}
Currently I am getting an undefined index and the alert box is empty. the $_GET ['branchid'] does not seem to retrieve the column I want.
Thank you for any help.
<td>
<?php
$branchid = isset($rowAddress['branchid']) ? $rowAddress['branchid'] : 0;
?>
<form method="POST" action="order_address.vc.php">
<input type="hidden" name="branchid" value="<?php echo $branchid ?>" />
<input type="hidden" name="assign" value="ASSIGN" />
<input type="submit" value="ASSIGN" class="btn button-color-blue font-color-white full_width" />
</form>
</td>
<td class="table-text-center">
<?php echo $branchid; ?>
</td>
orders_address.vc.php:
if(isset($_POST['assign']) && $_POST['assign'] === 'ASSIGN') {
echo '<script type="text/javascript">';
echo 'alert('.$_POST['branchid'].')';
echo '</script>';
}
I am not sure why you have a form submit button wrapped around an anchor tag. When the submit tag is clicked I honestly don't know which one takes precedence, the anchor tag or the form submission.
Assuming that you have the table wrapped in an HTML form and a POST request is made. I suggest adding the branchid as the value of the submit button. Ex:
<input type="submit" class="..." name="assign" value="<?=$rowAddress['branchid']?>">
In receiving end you can then get branchid from $_POST['assign']
if (! empty($_POST['assign']))
{
$branchid = $_POST['assign'];
echo "<script type='text/javascript'>alert('$branchid');</script>";
}
Its because you have $_GET['branchid'] in POST handle
if (isset($_POST['assign']) && $_POST['assign'] == 'ASSIGN') ...
But you send it as normal GET a href link so condition with POST is never true.
if (isset($_POST['assign']) && $_POST['assign'] == 'ASSIGN'){
$branchid = $_GET ['branchid'];
echo "<script type='text/javascript'>alert('$branchid');</script>";
}
HTML should be as below:
<form action="order_address.vc.php" method="POST">
<input type="hidden" name="branchid" value="<?php echo $branchid ?>" />
<input type="submit" name="assign" value="ASSIGN" />
</form>

When I auto submit the from its create a infinity loop how can i stop

The from action must be the same page how can I just click once. And remove the loop
<form action="" name="formsajal" method="post" enctype="multipart/form-data" id="formsajal">
<?php
$execute = "<input id='submitted' type='submit' value='submit' title='Ctrl+Enter'>";
echo $execute;
?>
</form>
<?php echo "<script>document.getElementById('submitted').click();</script>"; ?>
Give your input a name, so that the form submits a value. Only inputs, selects and textareas with a name attribute is sent over POST/GET when the form is submitted.
You can now check if the form was sent, by checking if that name is present in the $_POST array. If it is present, don't re-submit the form.
<form action="" name="formsajal" method="post" enctype="multipart/form-data" id="formsajal">
<input id='submitted' name="submitted" type='submit' value='submit' title='Ctrl+Enter'>
</form>
<?php
if (!isset($_POST['submitted'])) {
echo "<script>document.getElementById('submitted').click();</script>";
}
?>
Here is the code have name attribute for your submit
<?php
if (isset($_POST['submitted'])) {
//Do your after submit stuffs here
} else {
?>
<form action="" name="formsajal" method="post" enctype="multipart/form-data" id="formsajal">
<?php
$execute = "<input name='submitted' id='submitted' type='submit' value='submit' title='Ctrl+Enter'>"; //set name attribute
echo $execute;
?>
</form>
<?php echo "<script>document.getElementById('submitted').click();</script>"; ?>
<?php } ?>

Codeigniter - form error display underneath the input field

I am trying to display a form error underneath the input fields but after I click the submit button it will redirect to another page...
Here's my code page controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller{
public function view($page = 'home'){
if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('template/header');
$this->load->view('pages/'.$page);
$this->load->view('template/footer');
}
public function login(){
echo $this->input->POST('username');
}
public function registercheck(){
echo $this->input->POST('username');
echo $this->input->POST('pwd');
echo $this->input->POST('pwd2');
echo $this->input->POST('fname');
echo $this->input->POST('position');
echo $this->input->POST('contactnumber');
$this->form_validation->set_rules('username', 'USERNAME', 'required|max_lenght[20]');
$this->form_validation->set_rules('pwd', 'USERNAME', 'required|max_lenght[20]');
$this->form_validation->set_rules('pwd2', 'UPPERCASE', 'required|max_lenght[20]');
$this->form_validation->set_rules('fname', 'USERNAME', 'required|max_lenght[20]');
$this->form_validation->set_rules('position', 'USERNAME', 'required|max_lenght[20]');
$this->form_validation->set_rules('contactnumber', 'USERNAME', 'required|max_lenght[20]');
if($this->form_validation->run() == false){
$this->load->view('pages/register');
} else{
echo 'register ok';
}
}
}
?>
And here is my views/pages/register.php
<form action="<?php echo base_url(); ?>pages/registercheck" method="post">
<input type="text" class="form-control" id="username" name="username" value="<?php echo set_value('username'); ?>" >
<?php if (form_error('username')) { ?>
<span class="help-block"><?php echo form_error('username'); ?> </span>
<?php } ?>
<input type="text" class="form-control" id="username" name="username" value="<?php echo set_value('username'); ?>" >
<?php if (form_error('username')) { ?>
<span class="help-block"><?php echo form_error('username'); ?> </span>
<?php } ?>
</form>
Please help me..
What am I doing wrong?
Thank you in advance!
You are in the right direction. But to achieve the desire functionality, you should do as so:
The form and the input validation should be in the same page (controller). In your example, both should be in register.php.
This basic pseudo code should do the trick:
On register page controller:
If method == get:
Display register form.
If method == post:
Check the form data:
If errors exists:
display register page with error.
else:
redirect to ....
Good Luck!

Can only post first result of while loop

I am using a while loop to display results from a query. The while loop is working fine. In hidden fields I would like to post the values of userID and accessID to the user details page. I am submitting the form using javascript to submit from a link. My problem is that regardless of the username I click I can only post the values for the first displayed record. What am I doing wrong?
The code:
<?php
while($row = $result->fetch_array()) { ?>
<form method="post" action="edit_user.php" id="userForm">
<tr>
<td>
<?php echo $row['firstname'].' '.$row['surname']; ?>
<input type="hidden" name="userID" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="accessID" value="<?php echo $row['accessID']; ?>" />
</td>
</tr>
</form>
<?php } ?>
The javascript used for submitting the form:
function submitForm() {
var form = document.getElementById("userForm");
form.submit();
}
Thank you.
EDIT - I don't want to pass the values in the url.
you are generating multiple <form>s inside loop, move your <form> outside while loop, like:
<form method="post" action="edit_user.php" id="userForm">
<?php
while($row = $result->fetch_array()) { ?>
<tr>
<td>
<?php echo $row['firstname'].' '.$row['surname']; ?>
<input type="hidden" name="userID[]" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="accessID[]" value="<?php echo $row['accessID']; ?>" />
</td>
</tr>
<?php } ?>
Submit
</form>
You're running into trouble because of this line
var form = document.getElementById("userForm");
In Javascript and HTML, an ID is supposed to be unique to a certain DOM element. In this case, you've got a whole load of form tags that have the same ID. You need to give each form a different ID, and then pass that ID to the submitForm function.
For example:
<?php
$id = 0;
while($row = $result->fetch_array()) { ?>
$id++;
<form method="post" action="edit_user.php" id="<?php echo "userForm".$id ?>">
<tr>
<td>
<?php echo $row['firstname'].' '.$row['surname']; ?>
<input type="hidden" name="userID" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="accessID" value="<?php echo $row['accessID']; ?>" />
</td>
</tr>
</form>
<?php } ?>
and then
function submitForm(id) {
var form = document.getElementById(id);
form.submit();
}
edit: how do I php? :D

Change form label text at runtime

I have an HTML label and I would like to change its text at runtime. I know I can use innerHTML, which works, but why does it just just append my message in front of the already-set label text.
PHP:
<label for="hp_display" id="addToHP_<?php echo $x; ?>"></label><?php echo $addTo; ?>homepage: </label><input type="checkbox" id="hp_display_<?php echo $x; ?>" <?php echo $check; ?> onclick="addToHome('<?php echo md5($product['product_id']); ?>','<?php echo $add; ?>','<?php echo $x; ?>')" />
JAVASCRIPT:
function addToHome(id,a,n){
$.ajax({
url:'func/addToSlider.php',
type:'POST',
data:{type:a,id:id},
beforeSend: function(){
document.getElementById('addToHP_'+n).innerHTML = '';
document.getElementById('addToHP_'+n).innerHTML = 'Updating';
},
success:function(e){
if(e === '1'){
document.getElementById('addToHP_'+n).innerHTML = '';
if(a === 'remove'){
document.getElementById('addToHP_'+n).innerHTML = 'Add to homepage';
}else{
document.getElementById('addToHP_'+n).innerHTML = 'Remove from homepage';
}
}else{
alert('There has been a server changing your file, please try again');
}
}
});
}
It looks complicated, but surely innerHTML or JQuery's .html('') should replace the text instead of appending the text. I could just refresh page on success of the ajax but I think changing the label text is more user-friendly.
Your html markup is wrong, you have two closing label elements
<label for="hp_display" id="addToHP_<?php echo $x; ?>"></label><?php echo $addTo; ?>homepage: </label>
^^^^^^^^ ^^^^^^^
without all the php mark up it is
<label>X</label>Y</label>
You have too many </label>
<label for="hp_display" id="addToHP_<?php echo $x; ?>"></label><?php echo $addTo; ?>homepage: </label>
so while the label looks like it is being appended it is actually getting filled for the first time. remove the first </label> and problem solved.
should be:
<label for="hp_display" id="addToHP_<?php echo $x; ?>"><?php echo $addTo; ?>homepage: </label>

Categories