Jump to content

Surely this is SIMPLE!.. help


smallzoo

Recommended Posts

I have a function which posts data from a form ( a table.. )

function modifyAdjustments() {
    var formData = jQuery('#modadjustments').serialize();
    new Ajax.Request("/views/releases/modadjustments.php?fd="+formData, { method:'post',
	onSuccess: function(transport) {
		jQuery('#edit_adjustnments_div').dialog('close');				
		},
	onFailure: function(transport) {	
				}		  
     });			   	   
} 

When I look the form data it looks like this ;-

 

"defid=5&defid=6&defid=2"

 

 

All I want modadjustments.php to do is to parse this data and do a foreach loop so I can do something with the defid's

 

e.g.

 

e.g.

foreach...{

print_r (defid)

}

this will show of above example
5
6
7

 

I'm going mad with trying to do this ?

Link to comment
Share on other sites

URL parameters are stored in $_GET, not in $_POST.

 

You also haven't understood the reply. In PHP, you can't have duplicate parameter names. You do have duplicate names, because defid is appearing twice in the URL. To fix this, you need to change your form and append “[]” to the parameter:

<input type="text" name="defid[]">
<input type="text" name="defid[]">
...

Better yet use explicit indexes:

<input type="text" name="defid[0]">
<input type="text" name="defid[1]">
...
Edited by Jacques1
Link to comment
Share on other sites

The JavaScript code also looks fishy. Why are you sending an empty POST request with all parameters attached to the URL? Use the data attribute to include the parameters in the request body. And why do you prepend “fd=” to your URL-encoded parameters? This is likely to break parameter parsing altogether.

Link to comment
Share on other sites

I mean GET sorry

 

yes I forgot the fd=.. that was for testing

 

Not sure what you mean by empty post.. it posts the form data

 

When I use the correct function ( see below )

		function modifyAdjustments() {			    
			   	var formData = jQuery('#modadjustments').serialize();
                alert(formData);
			new Ajax.Request("/views/releases/modadjustments.php?"+formData, { method:'post',
				onSuccess: function(transport) {
					jQuery('#edit_adjustnments_div').dialog('close');				
			  	},
			  	onFailure: function(transport) {				
				}		  
			});			    
		}

and also add the [] parameter to the form variable ( see below ) 

‘ cut down version

<form name="modadjustments" id="modadjustments" action="" onsubmit="return false;">

<? foreach($adjustments as $a) { $i=0; ?>
    <input type="checkbox"  <?php echo ($a->active==1 ? 'checked' : '');?>
           value="<?php echo ($a->active==1 ? $a->default_id : 'no');?>"
           name="defid[]" id="defid[]" />						
<? } ?>

<button type="button" onclick="modifyAdjustments();">Save changes</button>

</form>

the format still looks like this ( see below )

 

 

defid%5B%5D=5&defid%5B%5D=6&defid%5B%5D=8

Link to comment
Share on other sites

This is clearly a different format, namely the correct one. If you're still having trouble, tell us exactly what that is.

 

Note that you cannot use implicit indices with checkboxes (which is why I recommended explicit indices). Since unchecked boxes aren't sent at all, you have no chance of figuring out which box belongs to which adjustment.

 

Not sure what you mean by empty post.. it posts the form data

 

You're sending a POST request, but all data is attached to the URL, and the request body is completely empty. That doesn't make a lot of sense. Put the parameters into the request body (with the already mentioned "data" property).

Link to comment
Share on other sites

Ok.. so like this

<? foreach($adjustments as $a) { $i=0; ?>
                            <input style="width: 20px;float: none;" type="checkbox" 
                            <?php echo ($a->active==1 ? 'checked' : '');?>
                            value="<?php echo ($a->active==1 ? $a->default_id : 'no');?>"
                            name="defid[<?=$i?>]" id="defid[<?=$i?>]" />						
<? } ?>

and

		function modifyAdjustments() {
			   	var formData = jQuery('#modadjustments').serialize();
			   	//data = new FormData();
                alert(formData);
			new Ajax.Request("modadjustments.php", { method:'post',
			    data: formData,
				onSuccess: function(transport) {
					jQuery('#edit_adjustnments_div').dialog('close');				
			  	},
			  	onFailure: function(transport) {
					
				}		  
			});			   
		}
Link to comment
Share on other sites

ok..finally I think I m getting there

<? $c=0;
  foreach($adjustments as $a) { $i=0; $c++; ?>
    <input style="width: 20px;float: none;" type="checkbox" 
       <?php echo ($a->active==1 ? 'checked' : '');?>
        value="<?php echo ($a->active==1 ? $a->default_id : 'no');?>"
         name="defid[<?php echo $c;?>]" id="defid[<?php echo $c;?>]" />		
<? } ?>

This gives me ( in debug mode )

<input style="width: 20px;float: none;" type="checkbox" 
 checked                            value="5"
 name="defid[1]" id="defid[1]" />

......
<input style="width: 20px;float: none;" type="checkbox" 
 checked                            value="6"
name="defid[2]" id="defid[2]" />
......

<input style="width: 20px;float: none;" type="checkbox" 
checked                            value="8"
name="defid[3]" id="defid[3]" />

so running the script

		function modifyAdjustments() {
			   	var formData = jQuery('#modadjustments').serialize();
                alert(formData);
			new Ajax.Request("/views/releases/modadjustments.php", { method:'post',
			    data: formData,
				onSuccess: function(transport) {
					jQuery('#edit_adjustnments_div').dialog('close');				
			  	},
			  	onFailure: function(transport) {
					
				}		  
			});			    
		}

gives me 

 

defid%5B1%5D=5&defid%5B2%5D=6&defid%5B3%5D=8

 

as the form data

 

...so how do I get these defid values in the mod adjustments php file ?

Link to comment
Share on other sites

Made some alterations to the form data so now sends distinct data:-

function modifyAdjustments() {
  var formData = jQuery('#modadjustments').serialize();
  new Ajax.Request("modadjustments.php", { method:'post',
        data: formData,
        onSuccess: function(transport) {
         jQuery('#edit_adjustnments_div').dialog('close');
        },
        onFailure: function(transport) {
        }	 
  });	   
}
Link to comment
Share on other sites

Made some alterations to the form data so now sends distinct data:-

function modifyAdjustments() {
  var formData = jQuery('#modadjustments').serialize();
  new Ajax.Request("modadjustments.php", { method:'post',
        data: formData,
        onSuccess: function(transport) {
         jQuery('#edit_adjustnments_div').dialog('close');
        },
        onFailure: function(transport) {
        }	 
  });	   
}
Link to comment
Share on other sites

..sorry made a mistake and hit send by accident

 

Anyway..made some alterations to the code

function modifyAdjustments() {
  	var formData = jQuery('#modadjustments').serialize();
        new Ajax.Request("modadjustments.php", { method:'post',
        data: formData,
        onSuccess: function(transport) {
          jQuery('#edit_adjustnments_div').dialog('close');
         },
        onFailure: function(transport) {
        }	 
        });	   
}

formData  is "defid_1=5&defid_2=6&defid_3=8”

 

mod adjustments.php code is

$i=1;
$conn->query($sql="DELETE FROM modified_adjustments where default_id<>''");
  
while (isset($_GET["defid_".$i])){
	   $did= $_GET["defid_".$i];
       $conn->query($sql="INSERT INTO modified_adjustments (default_id)
	   				  VALUES (:did)", $params=array(':did'=>$did))	;
    $i++;
}

If I run this using   ...modadjustments.php?defid_1=5&defid_2=6&defid_3=8   the three records are inserted !! Success

 

BUT

 

If I use the ajax.request function they are not

 

Any Clues

Link to comment
Share on other sites

formData  is "defid_1=5&defid_2=6&defid_3=8”

 

Unless I missed something in your flurry of recent posts, you had [correctly] changed the names of the checkboxes to be an array in the format: defid[1], defid[2], defid[3], etc. But, your code now is trying to reference non-array fields in the format defid_1, defid_2, etc.

 

You should simply need to iterate over the array using a foreach() loop. Something like:

 

if(isset($_GET['defid']))
{
    //Define the query ONE time - then run multiple times
    $sql="INSERT INTO modified_adjustments (default_id) VALUES (:did)";
    foreach($_GET['defid'] as $did)
    {
        $conn->query($sql, $params=array(':did'=>$did));
    }
}
Link to comment
Share on other sites

I have tried arrays and explicit i.e. defid_1,defid_2  and defid[1],defid[2] and running the modajustents.php on its own works great BUT from the ajax request nothing happens

 

ie modadjustments.php?defid%5B1%5D=5&defid%5B2%5D=6&defid%5B3%5D=8 works great

 

but using the ajax request and sending that form data doesn't !

Link to comment
Share on other sites

It's been a long day and I'm tired, but I think your prepared statement is malformed. When using a prepared statement, you only have to actually prepare it once. Try:

if(isset($_POST['defid'])){
	$sql = $conn->prepare("INSERT INTO modified_adjustments (default_id) VALUES (:did)";
	foreach($_POST['defid'] as $did){
		$sql->execute(array('did'=>$did));
	}
}

Of course, you'll want to check for errors and may want to wrap the whole thing in a transaction, but (assuming my brain hasn't broken), that should be the basic idea.

Link to comment
Share on other sites

Sorry for the dumb reply but I'm not sure what you mean

 

I put the var_dump in the called php file (modajustments), ran the script from the calling page but I dont see any response. All the ajax request does is run ( I presume ? ) close the popup and then return to the calling page ?

 

Cheers

Link to comment
Share on other sites

your current javascript code isn't doing anything when there's a failure of the php code to send a response back, which is more than likely what's happening, due to a fatal run-time error. the extra layer due to the ajax is just hindering any learning/development of the core code.

 

you need to get this working without the ajax first, then simply add/enable a general purpose event listener/ajax form submitter to switch to using ajax. your html form markup should be the same regardless of using ajax and it can and should work even if javascript is turned off.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.