Note that there are some explanatory texts on larger screens.

plurals
  1. PORun function when pressing button and insert into form
    primarykey
    data
    text
    <p>So I have this form, where I want to add a button which makes a function run.</p> <p>This is how my form looks like:</p> <pre><code>&lt;form action="" method="POST"&gt; CVR-nummer:&lt;br&gt; &lt;input name="cvr" id="cvr" type="text" value="" size="30"/&gt; &lt;button onclick="&lt;?php getinfo(); ?&gt;"&gt;Run the function&lt;/button&gt;&lt;br&gt; Navn:&lt;br&gt; &lt;input name="navn" type="text" value="&lt;?php if(isset($navn)) {echo $navn;} ?&gt;" size="30"/&gt;&lt;br&gt; Adresse:&lt;br&gt; &lt;input name="adresse" type="text" value="&lt;?php if(isset($adresse)) {echo $adresse;} ?&gt;" size="30"/&gt;&lt;br&gt; Postnummer:&lt;br&gt; &lt;input name="postnr" type="text" value="&lt;?php if(isset($postnr)) {echo $postnr;} ?&gt;" size="30"/&gt;&lt;br&gt; By:&lt;br&gt; &lt;input name="by" type="text" value="&lt;?php if(isset($by)) {echo $by;} ?&gt;" size="30"/&gt;&lt;br&gt; &lt;input type="submit" value="Registrer"/&gt; &lt;/form&gt; </code></pre> <p>And this is my function which uses an API</p> <pre><code>&lt;?php function getinfo() { $cvr = "12345678"; $api = json_decode(file_get_contents("http://cvrapi.dk/{$cvr}/"),true); $navn = $api['navn']; $adresse = $api['adresse']; $postnr = $api['postnr']; $by = $api['by']; } ?&gt; </code></pre> <p>When I click the button I want it to: 1. Get value of the cvr-field. 2. Validate cvr-field so it has exactly 8 numbers. 3. If validation OK it must set the variables. 4. The variables must be shown in the form.</p> <p>Any thoughts on how to do this?</p> <p><strong>UPDATE</strong></p> <pre><code>&lt;script type="text/javascript"&gt; function checkCVR() { var cvr = document.getElementById("cvr").value; var reg = new RegExp("^[0-9]{8}$"); if(!reg.test(cvr)) { document.getElementById("cvr_error").style.display = 'block'; document.getElementById("cvr_error").innerHTML = "CVR Error!"; } else { document.getElementById("cvr_error").style.display = 'none'; // var url = "http://cvrapi.dk/"+cvr+"/"; // var url = "http://haxify.org/result.php"; var url = "json.txt"; var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", url, true); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) { var json = JSON.parse(xmlhttp.responseText); document.getElementById("navn").value = json["navn"]; document.getElementById("adresse").value = json["adresse"]; document.getElementById("postnr").value = json["postnr"]; document.getElementById("by").value = json["by"]; } }; xmlhttp.send(null); } } &lt;/script&gt; &lt;form action="" method="POST"&gt; CVR-nummer:&lt;br&gt; &lt;input name="cvr" id="cvr" type="text" value="" size="30"/&gt; &lt;input type="button" onClick="checkCVR()" value="Run the function" /&gt;&lt;br /&gt; &lt;span id="cvr_error" style="color:red;display:none;"&gt;&lt;/span&gt; Navn:&lt;br /&gt; &lt;input name="navn" id="navn" type="text" value="" size="30"/&gt;&lt;br /&gt; Adresse:&lt;br /&gt; &lt;input name="adresse" id="adresse" type="text" value="" size="30"/&gt;&lt;br /&gt; Postnummer:&lt;br /&gt; &lt;input name="postnr" id="postnr" type="text" value="" size="30"/&gt;&lt;br /&gt; By:&lt;br /&gt; &lt;input name="by" id="by" type="text" value="" size="30"/&gt;&lt;br /&gt; &lt;input type="submit" value="Registrer"/&gt; &lt;/form&gt; </code></pre> <p>This one actually works on one website but does not work on my own site. Is there any requirements for any software or something, or what can be the reaseon for this to not work on my specific website?</p> <p>Test: <a href="http://haxify.org/form_v4.php" rel="nofollow">http://haxify.org/form_v4.php</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload