Как говорится в названии,Показать и добавить форму в div, когда пользователь нажимает кнопку
Когда пользователь нажимает кнопку, я хочу отобразить форму в тегах div.
HTML
<div id="newaccount_form">
<!--add form here-->
</div>
PHP
echo "<form>";
echo "<input type='submit' value='New Account' name='newaccount' onclick='newAccount(this)'/>";
echo "</form>";
JS/AJAX
function newAccount(obj){
alert("test");
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("newaccount_form").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","newaccount.php", true);
xmlhttp.send();
Теперь, когда кнопка "Новая учетная запись" получает щелкнул Я хочу форму на newaccount.php для отображения в пределах newaccount_form
divs.
newaccount.php
if (isset($_POST['submit'])) {
// Grab the profile data from the POST
$accountname = mysqli_real_escape_string($dbc, trim($_POST['accountname']));
if (!empty($accountname)) {
// Make sure that this account name doesnt already exist
$query = "SELECT * FROM account WHERE account_name = '$accountname'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) {
// The account name is unique, so insert the data into the database
$query = "INSERT INTO account (account_name) VALUES ('$accountname')";
mysqli_query($dbc, $query);
// Confirm success with the user
echo '<p>You have succesfully created a new account. Please go back to the<a href="login.php">admin panel</a>.</p>';
}
else {
//Account already exists with the name
echo '<p class="error">An account already exists with this name, Please try another account name.</p>';
$accountname = "";
}
}
else {
echo '<p class="error">Please enter in all fields.</p>';
}
}
echo "<p>test</p>";
?>
<form method="post" action="newaccount.php">
<fieldset>
<legend>New Account</legend>
<ul>
<li>
<label for="accountname">Account name: </label>
<input id="accountname" type="text" name="accountname"/>
</li>
</ul>
<input type="submit" value="Create" name="submit"/>
<input type="button" value="Cancel" name="cancel"/>
</fieldset>
</form>
1. Почему вы не используете такую структуру, как JQuery? 2. Это не связано с PHP, это все HTML. 3. [Если вы настаиваете на том, чтобы сделать это необоснованным способом, посмотрите здесь.] (Https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild) Удачи. – miken32
1. Как я могу использовать JQuery здесь? 2. Это не имеет ничего общего с HTML, и я не понимаю, как вы думаете об этом. 3. Уверен, что плохо смотрю на это спасибо. – Zoxac