Jump to content

Form post within switch/case: Help


robj

Recommended Posts

I'm pretty new to ajax, but I'm a quick learner and here is my test application.

 

To keep it short:

I have a simple main screen with 3 option. Add, Update, Delete.

 

You click Add and It loads a form under these 3 options. Pretty easy, but I don't know how to submit the form to that case. I had the form posting to itself originally, but once I load the form using a get ID tag, the form breaks unless the action is set to the file location. I want this form to submit within the page without redirecting.

 

Here's some relevant code:

index.php:

<body onload="init()">
<div id="load">     </div> 

<div>
<a href="#" class="tabs" id="additions">Additions</a><br /><br />

<a href="" class="tabs" id="update">Update Entries</a><br /><br />

<a href="" class="tabs" id="delete">Delete</a><br /><br />

</div>


<div id="Postjax" class="clearer"></div> 

</body>

 

javascript:

function init () {
var tabs = document.getElementsByClassName('tabs');
for (var i = 0; i < tabs.length; i++) {
	$(tabs[i].id).onclick = function () {
		getTabData(this.id);
	}
}
}

function getTabData(id) {
var url = 'ajaxSwitch.php';
var rand   = Math.random(9999);
var pars   = 'id=' + id + '&rand=' + rand;
var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: showResponse} );
}

function showLoad () {
$('load').style.display = 'block';
}

function showResponse (originalRequest) {
var newData = originalRequest.responseText;
$('load').style.display = 'none';
$('Postjax').innerHTML = newData;
}

 

Form.php:

<?php

if (isset($_POST['submit'])) {	

$con = mysql_connect([REMOVED], [REMOVED], [REMOVED]);

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db([REMOVED], $con);

mysql_query([REMOVED]);

mysql_close($con);	
?>

<div style="width:600px;">

Success

</div>

<?php

} else {

?>

<form method="post" action="form.php">

[iRRELEVANT INPUT BOXES]

<input type="submit" name="submit" value="Send">
</form>
</div>

<?php } ?>

 

Ajax Switch/Case:

<?php 

if ($_GET['id'] == 'additions') {

switch($_GET['id']) { 


	case 'additions' : include_once('Form.php'); 
		break; 

//case 'update' : include_once('upForm.php'); 
		break; 

//case 'delete' : include_once('delForm.php'); 
		break; 

	default: echo 'There was an error.'; 
		break; 
}		


} 

//print $content;
usleep(1000); 

?>

 

Any help, suggestions, and/or new methods would be greatly appreciated.

 

Thanks

 

rob

Link to comment
https://forums.phpfreaks.com/topic/118128-form-post-within-switchcase-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.