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