hi

i am creating a webpage with a menu down the left hand side. depending on which link in the menu you click a table on the right of will show the relavent information. the table will show the relavent information from a mysql table based on the link clicked.

i have found that i can do this using javascript. my form looks like this:

<form name="form" method="post" action="processpage.php" >
                <li> <h3> links</h3>
                    <ul>
                        <li><a name="link1" href="javascript&#058; void(1);" onClick="submitForm('link1');">like1</a></li>

this javascript code submits the form to the process page:

function submitform()
            {
                document.form.submit();
            }

but on the process page i do to make sure the link that is being clicked a passed to the process page: <?php print $_POST["form"]; ?>

put it doesnt print anything i get an error:

Notice: Undefined index: submit in C:\wamp\www\processpage.php on line 31

Dani AI

Generated

A few quick points that explain what happened for and follow up on 's suggestion.

JavaScript is case-sensitive and functions need the expected arguments. In this thread the submit call and the function signature did not match, and a hidden field was either not set or was assigned an absent value. Assigning undefined to an input value becomes the literal string "undefined", and reading a missing $_POST key in PHP produces the "Undefined index" notice. Using a dedicated form id and a hidden field is the right idea; the missing step was reliably setting that field before submitting.

A simple, robust pattern (uses data attributes and event listeners rather than inline javascript: URIs):

<form id="menuForm" method="post" action="processpage.php">
<ul>
<li><a href="#" class="menu-link" data-key="link1">link1</a></li>
<li><a href="#" class="menu-link" data-key="link2">link2</a></li>
</ul>
<input type="hidden" name="selectedItem" id="selectedItem" value="">
</form>

<script>
(function(){
var form = document.getElementById('menuForm');
var hidden = document.getElementById('selectedItem');
var links = document.querySelectorAll('.menu-link');
Array.prototype.forEach.call(links, function(link){
link.addEventListener('click', function(e){
e.preventDefault();
hidden.value = link.getAttribute('data-key');
form.submit();
}, false);
});
})();
</script>

On the PHP side always check the variable before using it to avoid notices and escape output when printing:

<?php
$val = isset($_POST['selectedItem']) ? $_POST['selectedItem'] : '';
echo htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
?>

For the submit behavior see the browser API documentation for form submission (HTMLFormElement.submit).

Recommended Answers

All 3 Replies

Try this and in your php look for "myvar"

<form name="form" method="post" action="processpage.php" >
<li> <h3> links</h3>
<ul>
<li><a name="link1" href="javascript: void(1);" onClick="submitForm('link1');">like1</a></li>
<input type="hidden" name="myvar" id="myvar" />
</form>
<script>
function submitform(txt)
{
    document.getElementById("myvar").value = txt;
    document.form.submit();
}
</script>

nothing happened when I clicked on the link.

I changed the href bit to: href="javascript:submitform(); and then something happened. I know get the word: undefined on the php page. i did this code on the php page:

<?php
		print $_POST["myvar"];
?>

any ideas to fix this error?

i have come up with a solution

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.