Get form attribute value with PHP - php

Is there a way to get submitted form's attribute value using PHP, when the submit button is clicked?
I have a form which has attribute data-id: someid, I need to get this value.

There is no direct way to get the data attribute value in PHP.
You have to first get data-attribute, set this value to hidden field and then use PHP to get it.

If You Wants Data id on submit
$(document).ready(function(){
$('#formid').on('submit', function(){
var id = $(this).data("id")
alert(id);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="POST" name="myformName" id="formid" data-id="someid">
<input type="submit" name="submit">
</form>

You can achieve this in php with two ways:
1. Either pass your 'someid' in input with hidden type
<input type="hidden" name="data-id" value="someid" />
2. Just pass your 'someid' in action attribute
<form name="" action="filename.php?data-id=someid">

Related

How to get dropdown value based on class name

I want to get the dropdown value based on class name
<select name="changeStatus" class="changeStatus1" id="changeStatus1" onChange="business_type('1')">
Because of some reason i don't want to get the value through the name($_POST['changeStatus'];).
I want to get through class name.
Here is how I would do it, add a hidden input field and when the form is submit, modify the value of the hidden input field.
<form id="myForm">
<input type="hidden" name="hidden_input" id="hidden_input">
</form>
jQuery
$('#myForm').on('submit', function(){
$('#hidden_input').val($('#changeStatus1').attr('class'));
});
Then on the backend, you can get the class of the posted input (PHP for example)
$className = $_POST['hidden_input'];
switch($className){
// do Stuff...
}

Send jQuery array on php form submit

Right now I am updating user_list List using jquery and submitting the form data using Ajax
now I want to submit that form like normal form submit
Here is the problem I am unable to pass the JQuery updated user_list along form ?
Is There any way to do this ?
What I have tried
<script>
var user_names=[];
function check_selected(id){
user_names.push(id);
$("#username").val(user_name);
}
</script>
Where username is a form field like
<input type ="hidden" val = "" id="username" name ="username">
val is not a valid attribute for the <input /> element. Change it to value
<input type="hidden" value="" id="username" name="username" />
I am not sure if this will help or not, but you have used a variable that may have not been initialized
$("#username").val(user_name);
should be
$("#username").val(user_names); //missing s
besides, if you are after sending an array to the server, and the server is php, I would suggest adding [] square brackets at the end of the field name, this is a clean way of sending the list and the server can build the array and you would be able to access it as a list
$_POST['list_name'][index]
Try this
<script>
var user_names=[];
function check_selected(id){
user_names.push(id);
$("#username").val(user_names.join(","));
}
</script>

Setting value of hidden element in php

I have a hidden field in form. On form submit I call a database function to add the inputs to database and return result of query. I wish to set the value returned to hidden field. But when I am not able to assign a value to it
<form name="frmAddBook" id="frmAddBook" method="post">
<input type="hidden" name="actionResult" id="actionResult">
PHP
echo '
<script type="text/javascript">
alert("hello");
document.getElementById("actionResult").value=1;
alert("2222222");
alert(document.frmAddBook.getElementById("actionResult").value);
</script>';
you need to specify the value of that hidden input field, so if you wanted to do this in PHP it'd look like this:
<input type="hidden" name="actionResult" id="actionResult" value="<?php echo $result; ?>"/>

Passing ID value in autocomplete in Scriptaculous

I have the scriptaculous autocompleter working. It pops up the data and I can select it, but it does not transfer the ID number in the form I'm using. I just cannot get the ID number. I can show the ID number on the screen using the following command:
alert (li.id);
I have used the following but no luck:
document.getElementById('inv_id').value=li.id;
and
$("#inv_id").val(li.id);
How do I pass that ID number to a value in the form? Here is the form code...
<form action="page2.php" method="post" name="Add_Form">
<input type="hidden" name="inv_id" value="0">
<input type="text" id="autocomplete" name="autocomplete_parameter"/>
<div id="autocomplete_choices" class="autocomplete"></div>
</form>
<script type="text/javascript">
new Ajax.Autocompleter("autocomplete", "autocomplete_choices", "ajax_inv.php", {
afterUpdateElement : getSelectedId
});
function getSelectedId(text, li) {
alert (li.id);
document.getElementById('inv_id').value=li.id;
or
$("#inv_id").val(li.id);
}
</script>
Where is the inv_id field? Make it a hidden input if you don't have it existing already, then document.getElementById('inv_id').value=li.id; should work

Trouble passing slider value to php mail script

I want to email the value of the slider along with some things they entered into my form to my php email script. This is how the form looks that will pass data to the POST method of mailer.php:
<form method="POST" action="mailer.php">
Name:
<input type="text" name="name" size="19"><br>
<br>
E-Mail:
<input type="text" name="email" size="19"><br>
<input type="hidden" name="slider_value" size="19" value=document.getElementById('sliderValue').value><br>
<br>
<input type="submit" value="Submit" name="submit">
</form>
document.getElementById('sliderValue').value is the call I make to get the value of the slider but when I pass this to the value of my hidden input slider_value I get back "document.getElementById('sliderValue').value" in the email that is sent by my php script. How would I pass the value instead?
document.getElementById is a Javascript function. It probably won't do what you want it to unless you put it in a place where Javascript is accepted.
One way to pass the slider value is by putting the sliderValue element in the form that you are submitting.
If that is not an option, you can set the value of the slider_value element with Javascript. Either set an onsubmit attribute to the form like this:
<form method="POST" action="mailer.php"
onsubmit="this.slider_value.value = document.getElementById('sliderValue').value">
or add an event listener to do it:
var form = document.forms[0]; //this may need to be changed depending on your form
function addSliderValue() {
this.slider_value.value = document.getElementById('sliderValue').value;
}
if (form.addEventListener) { //most browsers
form.addEventListener("submit", addSliderValue, false);
} else if (form.attachEvent) { //IE
form.onsubmit = addSliderValue;
}
Bind a function to the sliders "onSlide/onChange/whatever it's called" event that will update the hidden input field with the slider value. Also set the input value to the initial slider value on slider init. That should do the trick.

Categories