简体   繁体   中英

redirect to a page on a button click without submitting a form in PHP

I am trying to redirect my page to a location ( addparty.php?edit=true&id=1 ) with a button click. I am using JavaScript for a static value. Here is the code:

Here is my PHP code and JavaScript function.

$result=mysql_query("SELECT * FROM party ORDER BY partyName");
echo mysql_error();
while($note = mysql_fetch_array($result))
{
  echo "<tr>";
  echo "<td><label>$note[partyId]</label></td>";
  echo "<td><label>$note[partyName]</label></td>";
  echo "<td><label>$note[partyAddress]</label></td>";
  echo "<td><label>$note[partyBankAcc]</label></td>";
  echo "<td><label>$note[partyBankName]</label></td>";
  echo "<td><label>$note[partyRTGSCode]</label></td>";
  echo "<td><label>$note[partyEmail]</label></td>";
  echo '<td><button name="change" id="change" value="$note[partyId]" 
                    onClick="check();">Edit</button>
  echo "</tr>";
}

Javascript code:

function check()
{
    window.location.href="addparty.php?edit=true&id=1";
}

Here I am getting all the values from party table. I want to got to addparty.php?edit=true&id=$note[partyId] .If you see the onClick event it calls check() function. Now my id=1 is static and it works, but I want that id=$note[partyId] (which is unique party id of each party).

Any suggestions? Thanks in advance.

Change the button to this:

echo '<td><button name="change" id="change" onClick="check(' . $note['partyId'].');">Edit</button>';

Change javascript to this:

function check(id)
{
    window.location.href="addparty.php?edit=true&id=" + id;
}

Try adding $note[partyId] as a parameter to your check() -

function check(partyId)
{
    window.location.href="addparty.php?edit=true&id="+partyid;
}

and

echo '<td><button name="change" id="change" onClick="check('.$note['partyId'].');">Edit</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM