Send jQuery array on php form submit - php

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>

Related

Get form attribute value with 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">

how can we pass input type value in to an variable with out submit or click

i know the question doesn't looks good but am not getting any idea because am new to here.my question is can we store the value of an input type in to an variable.here in this case
<input type="text" name="statustochange">
having an value now i want to store that value in to an variable with out submit or click
i got the value in to
name="statustochange"
from this way
<script type="text/javascript">
$('.modal').modal({
ready: function(modal, trigger) {
modal.find('input[name="statustochange"]').val(trigger.data('status'))
}
});
</script>
Try this:
<input type="text" name="statustochange" value="<?php echo $str; ?>">
Into a js variable? Use a onChange event!
Into a php variable? Use a js onChange event to trigger a postback in Ajax (invisible to the user)
Edit: More realavent link

get custom attribute value in php

I am try to get the value of the input field with a custom attribute I have created using PHP. This is my code:
<form action="uploadform.php" method="post">
<input type="text" name="email" id="email" mynewattribute="myemail">
<input type="submit" name="submit">
</form>
//uploadform.php
<?php
//I know $name = $_POST['email']; will give me the value but I would like to get the value of the input field with "mynewattribute" and not name. Is it possible?
?>
The web browser doesn't know what to do with your custom attribute, so will simply ignore it. The only data sent when you submit the form is the values of "successful" elements. So your custom data will never be sent, and can never be read by the receiving script.
The best place to put such data is into hidden input fields. One possibility is to use names with square brackets in, which PHP automatically converts into arrays. e.g.
<input type="text" name="email[value]">
<input type="hidden" name="email[magic]" value="true">
Populates an array like this:
$_POST['email']['value'] = '';
$_POST['email']['magic'] = 'true';

Submitting a form without any variable names

I need to submit a form to the URL /search/, without any variable names. So that if I input into the form 'foobar', It will then submit the form in GET format to /search/foobar.
How can I achieve this? As far as I can see there is no way to do it with HTML, i'll have to use jQuery.
<form action = "/search/" onsubmit="this.action += encodeURIComponent(this.term.value); this.term.disabled = 'disabled'">
<input type="text" name="term">
</form>

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