HTML button that runs some MySQL code then opens a link

时间:2017-12-17 08:10:56

标签: javascript php html mysql

I was put in the sin-bin for a week after my last question, so if anyone has problems with this one, please let me know first and I'll do my best to clear things up before I get put on hold.

Anyway, on to my problem:

I have a page that has a text box for a reference number. What I want is to run a mysql query that will check if that value exists on a table, if it doesn't, create a new line for it. I don't think I'll have too much trouble with this, I've managed to get the code to write to the table (although, at the moment that's regardless of whether it exists or not).

What's really got me stumped is how do I include their entered reference in a URL that should load after the query has been completed. Would I need to somehow pass something from the PHP to javascript?

The intended URL will be something along the lines of www.foo.com/bar?=REF <--ref being the user-entered value.

As requested, the code I've used so far:

<html>
<head>
<meta charset="UTF-8">
<title>Application Form Prototype</title>

</head>

<hr width="100%" STYLE="position:absolute; top:220px">
<!--Breadcrumb Text-->
<hr width="100%" STYLE="position:absolute; top:260px">
<body>
<div style="position: absolute; TOP: 300px; left:100px; width: 75%">
<h1 class="heading-xlarge" style="Padding-bottom: 0px; margin-bottom: 30px; margin-top: 30px">Welcome to the prototype for the application form</h1>
<p class="lede">Please be aware that this prototype is <b>not</b> secure and as such, please enter only non-sensitive, non-personal data</p>
<p class="lede">Pleae enter a memorable reference for your application in the box below. If you are returning to a previous session, please enter the reference you assigned at that point</p>
<h1></h1>
<form action="" method="post">
    <label>Reference</label>
    <input type="text" name="ref" id="ref" required="required" placeholder="Please Enter reference"/><br /><br />
    <input type="submit" value=" Submit " name="submit" /><br />
</form>

<?php
if(array_key_exists('ref', $_POST)) {
    include 'connection.php';
    $ref = $_POST['ref'];
    $query = mysqli_query($mysqli, 'INSERT INTO answers (strRef) VALUES (\'' .$ref . '\');');
}
?>

</div>
</body>
</html>

I did try a bit of javascript, but calling an onclick event seemed to bypass my php, is that right? or was my DB just playing up?

1 个答案:

答案 0 :(得分:0)

<?php
if(array_key_exists('ref', $_POST)) {
    include 'connection.php';
    $ref = $_POST['ref'];
    $query = mysqli_query($mysqli, 'INSERT INTO answers (strRef) VALUES (\'' .$ref . '\');');
}

header('Location: http://foo.com/bar?=REF'.$ref.');
?>

尝试这样的事情