I am indexing the value of the below html input line (it is in a loop). I want to be able to identify which one a user selects. This is output to a jquery window, if that matters. A user will select a button in the window: Select 1, Select 2, etc. The variable "$i" is being indexed.
<td><p><input type="submit" method="post" name="submit" value="Select <?php echo $i;?>" class="submit" id="select_$i" /></p>
Here is my first attempt to grab $i after a user selects "Select $i" (Select 2, for example):
if (isset ($_POST['submit'])){
$int[$i] = $_POST['submit'][$i];
}
My idea is to separate the variable from the word "Select". It is that number that I really need assigned to $int. I am purposely including minimal code, but can include lots more if required. By the way, I assume the "if" statement needs to be in the same loop?
Note: I already have been trying for the past week to extract this value in javascript and pass it to the server. I am now banging my head against the wall. I am only mentioning this in case someone recommends I use javascript and ajax. If I can do this in php only, then ignore the portions below. Otherwise, here is what I tried previously:
if(this.id.indexOf('select')>-1) {var id = (this.id.split("_"))[1]; console.log(id);}
bookSelect = id;
This gets the selected integer value from:
<td><p><?php echo "<input type='submit' method='post' name='submit' value='Select $i' class='submit' id='select_$i' />"; ?></p>
I have not been able to pass this value to the server in time to get it assigned to php. If you want to see more of that attempt, look for my other question titled, "ajax is passing variable to server, but php session is passing the previous value, not the updated value"
As per the recommendation of M.chaudhry, here is the brunt of the javascript code when I was pulling the integer from the select id value. bookSelect is the value I am trying to pass to the server. I am trying two simultaneous ajax calls, the second one works just fine.
<script>
$(function() {
$( "#dialog" ).dialog({
height: 550, width: 450});
$( ".submit" ).click(function(){
if(this.id.indexOf('select')>-1) {var id = (this.id.split("_"))[1]; console.log(id);}
bookSelect = id;
$.ajax({
type: "POST",
url: 'book-meta.php',
async:true,
dataType: 'json',
data: {bookSelect : bookSelect},
success: function(data)
{
alert(bookSelect);
},
error: function(errorThrown){
alert('error');
},
});
$.ajax({
type: "POST",
url: 'book-meta.php',
async:true,
dataType: 'json',
//assign values to the variables to be passed to the server via data
data: { cover : cover, title : title, author : author, published : published, ISBN : ISBN,
description : description, pages : pages, publisher : publisher},
success: function(data)
{
//identify the variables for unique handing on the server side, this is
//how javascript variables (client side) are passed to the server
$("input[name='bbp_extra_fieldc']").val(data.cover);
$("input[name='bbp_topic_title']").val(data.title);
$("input[name='bbp_extra_field1']").val(data.author);
$("input[name='bbp_extra_field2']").val(data.published);
$("input[name='bbp_extra_field3']").val(data.description);
$("input[name='bbp_extra_field4']").val(data.pages);
$("input[name='bbp_extra_field5']").val(data.publisher);
$("input[name='bbp_extra_field6']").val(data.ISBN);
//$("input[name='count']").val(data.bookSelect);
//$('#select').val(data.bookSelect);
//alert(data);
//alert(bookSelect);
},
error: function(errorThrown){
alert('error');
},
});
$( "#dialog" ).dialog( "close" );
}); });
</script>
Here is the book-meta.php file. One note, the line print $bookSelect; in the below code causes the function to throw an error in javasript:
if ( isset( $_POST['bookSelect'] )) {
$bookSelect = $_POST['bookSelect'];
}
print $bookSelect;
if ( isset( $_POST['cover'] )) {
$cover = $_POST['cover'];
}
if ( isset( $_POST['title'] )) {
$title = $_POST['title'];
}
if ( isset( $_POST['author'] )) {
$author = $_POST['author'];
}
if ( isset( $_POST['published'] )) {
$published = $_POST['published'];
}
if ( isset( $_POST['description'] )) {
$description = $_POST['description'];
}
if ( isset( $_POST['pages'] )) {
$pages = $_POST['pages'];
}
if ( isset( $_POST['publisher'] )) {
$publisher = $_POST['publisher'];
}
if ( isset( $_POST['ISBN'] )) {
$ISBN = $_POST['ISBN'];
}
$r=array("cover"=>$cover,"title"=>$title,"author"=>$author,"published"=>$published,"description"=>$description,
"pages"=>$pages,"publisher"=>$publisher,"ISBN"=>$ISBN);
print(json_encode($r));
Use this:
$('.submit').click(function(){
var selection= $(this).attr('id');
//Send this selection variable to php via ajax
});
In PHP code get this variable call explode on that. e.g:
$selection=explode('select', $name);
Now $selection[1]
has integer value and 'select' text is removed from that.
Using Hammad's variable idea, here is my ajax code:
$(function() {
$( "#dialog" ).dialog({
height: 550, width: 450});
$( ".submit" ).click(function(){
//if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);}
//selection = id;
var selection= $(this).attr('id');
$.ajax({
type: "POST",
url: 'book-meta.php',
async:true,
dataType: 'json',
data: {selection : selection},
success: function(data)
{
alert(selection);
},
error: function(errorThrown){
alert('error');
},
});
Here is a thought. I am using the same php file to send my ajax variables (book-meta.php). I have tried using a separate php file without success the past week. Perhaps I should try that again? Here is the first part of the code in book-meta.php:
if ( isset( $_POST['selection'] )) {
//$selection = $_POST['selection'];
$selection = explode('select',$name);
}