0

I am aware that there are several questions dealing with how to pass querystring to HTML page but I have not been able to find one that resolves my issue.

We have a php page called viewrecs.php. This page queries the database and returns several records including recordId. The value of this id is passed to an HTML page called edit.html as shown below:

<a href="edit.html?recid='.$row['recordId'].'">Edit</a>

I am trying to grab the value of this recordId and store it as hidden field in edit.html page.

I have tried modifying the following JavaScript that I found on this forum to solve this problem but the value of recordId comes back blank.

 <script type="text/javascript">
    $(document).ready(function () {
    var x = getParameterByName("recid"); ///get url parameter value
    alert("recid : " + x);
    function getParameterByName(id) {
            id = id.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regex = new RegExp("[\\?&]" + id + "=([^&#]*)"),
                results = regex.exec(location.search);
            return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
    });
</script>

Then HTML:

 <input type="hidden" name="recid"/>

How can I retrieve the value of the querystring from php page and passed to HTML page and store it in a hidden form field?

CREDIT: The Javascript I modified above originally belongs to Mahmoude Elghandour

0

2 Answers 2

1

In your edit.html page you can use inside head tag

onload = function () {
    const urlParams = new URLSearchParams(window.location.search);
    const recordId = urlParams.get('recid');
    document.getElementById("recId").value = recordId;
}

Then, in your html you can write:

<input type="hidden" id="recId" name="recid">
Sign up to request clarification or add additional context in comments.

Comments

0

Hope you don't mind adding ID in your HTML elements..

var request_url=document.getElementById("recordId").href;
var request_url=(request_url).match(/[recid][=]([0-9]+)[&]*/i);
console.log(request_url[1]);
document.getElementById("recid").value = request_url[1];
<!--<a id="recordId" href="edit.html?recid='.$row['recordId'].'">Edit</a>-->
<a id="recordId" href="edit.html?recid=8888">Edit</a>
<input type="hidden" name="recid" id="recid"/>

And you can also use a javascript function

var url_string =document.getElementById("recordId").href; 
var url = new URL(url_string);
var c = url.searchParams.get("recid");
document.getElementById("recid").value = c;
//Or jquery $('#recid').val(c);
console.log(c);
<!--<a id="recordId" href="edit.html?recid='.$row['recordId'].'">Edit</a>-->
<a id="recordId" href="edit.html?recid=88588&somethingelse=123">Edit</a>
<input type="hidden" name="recid" id="recid"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.