Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

AJAX onreadystate Parameter übergeben.

Empfohlene Antworten

Veröffentlicht

Hallo,

habe folgendes Problem: onreadystate kann man nur eine Funktion übergeben die aber ohne Parameter sein muss.

Wie kann ich jetzt in meiner onreadystate Funktion ein div dynamisch z.B. aus einem Parameter der Ajax Funktion ansprechen?

Klingt kompliziert ... hier mein Code als Beispiel:

index.html

<html>

<head>

<title>Ajax Test</title>


<script type="text/javascript">

<!--


var xmlHttp = null;


function doAjax(iValue)

{

	if(iValue > 0)

	{

		xmlHttp = GetXmlHttpObject();


		if(xmlHttp == null)

		{

		alert("Browser does not support AJAX");

		}

		else

		{

			document.getElementById("ajax" + iValue).innerHTML = "<img src=\"gfx/loading.gif\" width=\"45\" height=\"45\" alt=\"loading ...\" />";


			var url = "ajax.php";

			url += "?sid=" + Math.random();


			var post = "iValue=" + iValue;


			xmlHttp.onreadystatechange = doAjaxState;

			xmlHttp.open("POST",url,true);

			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");

			xmlHttp.send(post);

		}

	}

}


function doAjaxState()

{

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")

	{

		document.getElementById("ajax1").innerHTML = xmlHttp.responseText;

	}

}


function GetXmlHttpObject()

{ 

	var objXMLHttp = null;


	if(window.ActiveXObject)

	{

		//alert("ActiveXObject");

		try

		{

			objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP")

		}

		catch(e)

		{

			try

			{

				objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")

			}

			catch(e)

			{


			}

		}

	}


	if(window.XMLHttpRequest && objXMLHttp == null)

	{

		objXMLHttp = new XMLHttpRequest()

	}


	return objXMLHttp;

}


-->

</script>


</head>

<body>


<select name="strSelect" size="1" onchange="doAjax(this.value)">

<option value="-1">- - -</option>

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

</select>


<hr />


<div id="ajax1" style="border: 1px solid #000000"></div><br />

<div id="ajax2" style="border: 1px solid #000000"></div><br />

<div id="ajax3" style="border: 1px solid #000000"></div><br />


</body>

</html>
ajax.php
<?php


$i = 0;

$j = 0;

$k = 0;


$iNumber = 0;


for($i = 0;$i < 250;$i++)

{

	for($j = 0;$j < 100;$j++)

	{

		for($k = 0;$k < 100;$k++)

		{

			$iNumber = ($i * $j) + $k;

		}

	}

}


echo $_POST["iValue"] . "<br />\n";

echo rand(0,10000);


?>

Ändert man den Wert der Selectbox wird zwar der richtige div mit dem "Laden Bild" befüllt aber das Ergebnis wird immer in ajax1 angezeigt.

Ich weiss, ich könnte auch eine Globale Variable machen, in der ich das richtige div speicher aber das muss doch auch anders gehen.

Irgendeine Idee?

Gruß Scratch


document.getElementById(changeelement).innerHTML = 'please wait...';

     objxml.onreadystatechange = function(){

        if (objxml.readyState == 4) {

	   document.getElementById(changeelement).innerHTML = objxml.responseText;

	}else{

	   document.getElementById(changeelement).innerHTML = document.getElementById(changeelement).innerHTML+'....';

	}//if ready state 4

    }// readystate function

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.