Beste heren en dames

ik heb een javascript die resultaten haalt uit een database zodat je keuzes krijgt als je het formulier aan het invullen bent

nu wil ik een javascript variable omzetten naar een php variable zodat ik meet gegevens uit de database kan halen

iemand een idee hoe ik dit kan doen zonder de pagina te vernieuwen..

ik had al iets gevonden maar wil tot nu toe niet echt werken


This worked for me.

I didnt wanna do a page refresh. 
I wanted to pass the value of a JS variable to a php function on the same page without having a page refresh or calling a 2nd php script
<script type='text/javascript'>

<?php $abc ?> = document.write(document.getElementById('report_nam e').value ); <?php ;?>;

</script>
<?php $obj1->getItem( $abc); ?>

$abc is the variable in which i get the JS variable value. 
JS variable value is read using "document.getElementById"

I then pass the value of $abc in a php function as:-
<?php $obj1->getItem( $abc); ?>

Javascript is een client-side scripting-taal. PHP is server-side. Dit betekent dat wanneer je een pagina opvraagt op de server, deze pagina wordt gegenereerd door de PHP parser. Het resultaat hiervan wordt vervolgens naar de browser gestuurd. In de browser wordt de javascript uitgevoerd. Andersom kan niet. De enige manier om een waarde mee te geven aan PHP is door een AJAX-request te doen naar een PHP-bestand met bijvoorbeeld die waarde in de querystring. Deze waarde zou je vervolgens (onder water) in een sessie of een database kunnen zetten.
Op zich heb ik de waardes al opgehaald heb alleen geen idee hoe ik ze in mijn formulier krijg

rpc.php


<?php
	
	// PHP5 Implementation - uses MySQLi.
	// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
	$db = new mysqli('localhost', 'User' ,'pass', 'db');
	
	if(!$db) {
		// Show error if we cannot connect.
		echo 'ERROR: Could not connect to the database.';
	} else {
		// Is there a posted query string?
		if(isset($_POST['queryString'])) {
			$queryString = $db->real_escape_string($_POST['queryString']);
			
			// Is the string length greater than 0?
			
			if(strlen($queryString) >0) {
				// Run the query: We use LIKE '$queryString%'
				// The percentage sign is a wild-card, in my example of countries it works like this...
				// $queryString = 'Uni';
				// Returned data = 'United States, United Kindom';
				
				// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
				// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
				
				$query = $db->query("SELECT * FROM HWS_Orders WHERE opdrachtgever LIKE '$queryString%' GROUP BY opdrachtgever DESC");
				if($query) {
					// While there are results loop through them - fetching an Object (i like PHP5 btw!).
					while ($result = $query ->fetch_object()) {
						// Format the results, im using <li> for the list, you can change it.
						// The onClick function fills the textbox with the result.
						
						// YOU MUST CHANGE: $result->value to $result->your_colum
	         			echo '<li onClick="fill(\''.$result->opdrachtgever.'\');">'.$result->opdrachtgever.'</li>';
					$adres = "$result->adres1";
	         		}
				} else {
					echo 'ERROR: There was a problem with the query.';
				}
			} else {
				// Dont do anything.
			} // There is a queryString.
		} else {
			echo 'There should be no direct access to this script!';
		}
	}
?>


dan de pagina met het formulier

orders.php


<html>

<head>
  <script type="text/javascript">
  function toonVensterNieuweOrder()
    {
      document.getElementById("EE").style.display = "block";
      var randomWaarde = Math.floor(10000 * Math.random() );

      document.getElementById("mijnd").src = "http://www.hws-koeriers.nl/hws/alerts.php&random_waarde="+ randomWaarde; 
      document.getElementById("mijnd").style.visibility = 'hidden';
    }
  function verbergVensterNieuweOrder()
    {
      document.getElementById("EE").style.display = "none";
    }
  </script>

<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
	function lookup(inputString) {
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("rpc.php", {queryString: ""+inputString+""}, function(data){

				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
	} // lookup
	
	function fill(thisValue) {
             
		<?php $abc ?> = document.write(thisValue); <?php ;?>
		
		<?php

		$order = "SELECT * FROM HWS_Orders WHERE opdrachtgever = '$abc' GROUP BY opdrachtgever DESC "; 
		$order = mysql_query($order);
		$order1 = mysql_fetch_array($order);


		?>

		$('#inputString').val(thisValue);
		$('#inputString1').val("<?php echo $order1["adres1"]; ?>");
		setTimeout("$('#suggestions').hide();", 200);
	}
</script>

<style>


	h3 {
		margin: 0px;
		padding: 0px;	
	}

	.suggestionsBox {
		position: absolute;
		left: 90px;
		top: 60px;
		margin: 10px 0px 0px 0px;
		width: 200px;
		background-color: #ffffff;
		-moz-border-radius: 7px;
		-webkit-border-radius: 7px;
		border: 1px solid #4368AD;	
		color: #4368AD;
	}
	
	.suggestionList {
		margin: 0px;
		padding: 0px;
	}
	
	.suggestionList li {
		
		margin: 0px 0px 3px 0px;
		padding: 3px;
		cursor: pointer;
	}
	
	.suggestionList li:hover {
		background-color: #659CD8;
	}


#EE {background-color: transparant; position:absolute; top:30%; left:35%; display: none;}


</style>
</head>
<body>

    <div id="EE" align=center>
      <table width="365" height="185">
	<form action=orders.php?nieuweorder=1 method=post AUTOCOMPLETE = "off">

	<tr bgcolor=#4368AD><td>
        <table width="370" class="tblBaseTblData" height="190">
          <thead><tr NOWRAP><th>&nbsp;&nbsp;Nieuwe Order</th><th><img src=images/orders.png border=0 valign=right align=right>&nbsp;&nbsp;</th></tr></thead>
       	  <tr class="evenRow"><td>&nbsp;&nbsp;Opdrachtgever</td><td>&nbsp;&nbsp;<input type=text name=opdrachtgever size=40 value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD;  font-style: normal; font-family: calibri; font-size: 12;">

	    <div class="suggestionsBox" id="suggestions" style="display: none;">
	    <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
	    <div class="suggestionList" id="autoSuggestionsList">
	    </div></div></td></tr>


	  <tr class="oddRow"><td>&nbsp;&nbsp;Adres</td><td>&nbsp;&nbsp;<input type=text name=adres1 size=40 value="" id="inputString1" onblur="fill();" style="outline: none; background-color: #ffffff; border:0; color: #4368AD;  font-style: normal; font-family: calibri; font-size: 12;"></td></td></tr>
          <tr class="evenRow"><td>&nbsp;&nbsp;Postcode</td><td>&nbsp;&nbsp;<input type=text name=postcode1 size=40 value="" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD;  font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
          <tr class="oddRow"><td>&nbsp;&nbsp;Plaats</td><td>&nbsp;&nbsp;<input type=text name=plaats1 size=40 value="" style="outline: none; background-color: #ffffff; border:0; color: #4368AD;  font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
          <tr class="evenRow"><td>&nbsp;&nbsp;Land</td><td>&nbsp;&nbsp;<input type=text name=land1 size=40 value="" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD;  font-style: normal; font-family: calibri; font-size: 12;"></td></td></tr>
          <tr class="oddRow"><td>&nbsp;&nbsp;Telefoon</td><td>&nbsp;&nbsp;<input type=text name=tel1 size=40 value="" style="outline: none; background-color: #ffffff; border:0; color: #4368AD;  font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
          <tr class="evenRow"><td>&nbsp;&nbsp;Contact</td><td>&nbsp;&nbsp;<input type=text name=contact1 size=40 value="" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD;  font-style: normal; font-family: calibri; font-size: 12;"></td></td></tr>
          <tr class="oddRow"><td>&nbsp;&nbsp;Email </td><td>&nbsp;&nbsp;<input type=text name=email1 size=40 value="" style="outline: none; background-color: #ffffff; border:0; color: #4368AD;  font-style: normal; font-family: calibri; font-size: 12;"></td></tr>

          <tr class="evenRow"><td></td><td><center><input type=submit value=Opslaan style="color: #4368AD;  font-style: normal; font-family: calibri; font-size: 11;">&nbsp;&nbsp;
          <button type=reset onClick="javascript:verbergVensterNieuweAlert();" style="color: #4368AD;  font-style: normal; font-family: calibri; font-size: 11;">Annuleer</button></center></td></tr>
      

	</form></table></td></tr></table>


    </div>


</body>
</html>


De opdrachtgever vult hij nu automatisch in als je deze aanklikt

maar nu is het dus eigenlijk de bedoeling dat hij dit ook doet voor adres, postcode, plaats enz...
ik ben inmiddels een stap verder alleen heb ik nu een probleem met het selecteren van de laatste rij in de database

Als ik een opdrachtgever aan klik pakt hij altijd de eerste ingevoegde rij terwijl ik wil dat hij de laatste rij pakt aangezien de adres gegevens zijn gewijzigd

het maakt niet uit of ik asc of desc gebruik hij blijft de eerste rij uit de database gebruiken

iemand een idee hoe ik dit kan oplossen


<?php
	
	// PHP5 Implementation - uses MySQLi.
	// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
	$db = new mysqli('localhost', 'user' ,'pass', 'db');
	
	if(!$db) {
		// Show error if we cannot connect.
		echo 'ERROR: Could not connect to the database.';
	} else {
		// Is there a posted query string?
		if(isset($_POST['queryString'])) {
			$queryString = $db->real_escape_string($_POST['queryString']);
			
			// Is the string length greater than 0?
			
			if(strlen($queryString) >0) {
				// Run the query: We use LIKE '$queryString%'
				// The percentage sign is a wild-card, in my example of countries it works like this...
				// $queryString = 'Uni';
				// Returned data = 'United States, United Kindom';
				
				// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
				// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
				
				$query = $db->query("SELECT * FROM HWS_Orders WHERE opdrachtgever LIKE '$queryString%' GROUP BY opdrachtgever ORDER BY id DESC LIMIT 8");
				if($query) {
					// While there are results loop through them - fetching an Object (i like PHP5 btw!).
					while ($result = $query ->fetch_object()) {
						// Format the results, im using <li> for the list, you can change it.
						// The onClick function fills the textbox with the result.
						
						// YOU MUST CHANGE: $result->value to $result->your_colum
	         			echo '<li onClick="fill(\''.$result->opdrachtgever.'|'.$result->adres1.'|'.$result->postcode1.'|'.$result->plaats1.'|'.$result->land1.'|'.$result->tel1.'|'.$result->contact1.'|'.$result->email.'\');">'.$result->opdrachtgever.'</li>';
					


	         		}
				} else {
					echo 'ERROR: There was a problem with the query.';
				}
			} else {
				// Dont do anything.
			} // There is a queryString.
		} else {
			echo 'There should be no direct access to this script!';
		}
	}
?>


alvast bedankt
Zolang de id AUTO_INCREMENT is, moet ORDER BY id DESC gewoon altijd beginnen bij laatst toegevoegde rijen. De fout zal ergens anders moeten zitten lijkt me. (Correct me if i'm wrong)
Hij staat inderdaad op AUTO_INCREMENT

en als ik de GROUP BY eruit haal dan staat hij inderdaad boven aan alleen staat dan een opdrachtgever er meer als 1 keer in
Maar je wilt maar 1 rij terug hebben? Dan doe je toch LIMIT 1, zonder de GROUP BY?
Dan laat hij maar 1 opdrachtgever zien terwijl je een keuze zou kunnen moeten maken uit een van de opdrachtgevers dus als je de L intikt laat hij alle opdrachtgevers zien dien beginnen met een L

Maar volgens mij heb ik een oplossing gevonden


$query = $db->query("SELECT * FROM (SELECT * FROM HWS_Orders WHERE opdrachtgever LIKE '$queryString%' ORDER BY id DESC) AS s GROUP BY opdrachtgever LIMIT 8");
Je zou ook nog DISTINCT kunnen gebruiken (is wel traag)

Reageren