Answer the question
In order to leave comments, you need to log in
Ajax and PHP How to set up correctly?
Good afternoon.
I'm new to website programming. I've been struggling all day to get the PHP script output right on the page without refreshing it. What I just did not try to change already. I ask for your help, how to properly configure the output of the result on the same page?
index.php :
<html>
<body>
<center>
<form method="POST" action="getlink.php">
Ссылка:
<input type="url" name="site" required>
<br>
Кол-во символов:
<input type="number" name="name" value="3" min="1" max="5" required>
<br>
<input type="submit" name="okbutton" value="OK">
</form>
</center>
</body>
</html>
<?
$err_message = ''; // Присваивает пустое значение переменной сообщения
$url = $_POST['site'] ;
$max = $_POST['name'] ;;
if (empty($url)) {
$err_message .= "<center>Нет ссылки. <br> <a href='http://site.ru/l/'>Создать новую ссылку.</a></center>";
}
if (empty($err_message)) {
$chars = 'qazxswedcvfrtgbnhyujmkiolp1234567890QAZXSWEDCVFRTGBNHYUJMKIOLP';
$size = StrLen($chars)-1;
$password = null;
while($max--)
$password.= $chars[rand(0,$size)];
echo
"<center>
Ссылка:
<hr><font face=verdana color=red size=7><b><a href='http://site.ru/l/".$password."' target='_blank'>http://siteru/l/".$password."</a></b></font><hr>
<a href='http://site.ru/l/'>Создать новую ссылку.</a></center>";
$file = fopen ("links.ini","a+");
$c = ' = ';
$f = $password . $c . '"' . $url . '"';
fwrite ( $file, "\n".$f);
echo nl2br(file_get_contents('links.ini'));
fclose($file);
}
// Выдает сообщения о пустых полях, если такие есть
else {
print $err_message;
}
?>
Answer the question
In order to leave comments, you need to log in
Thank you, I figured it out.
Index.php :
<html>
<head>
<title>Укорачиватель ссылок</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript" language="javascript">
function call() {
var msg = $('#formx').serialize();
$.ajax({
type: 'POST',
url: 'getlink.php',
data: msg,
success: function(data) {
$('#results').html(data);
},
error: function(xhr, str){
alert('Возникла ошибка: ' + xhr.responseCode);
}
});
}
</script>
</head>
<body>
<br>
<br>
<center>
<form method="POST" id="formx" action="javascript:void(null);" onsubmit="call()">
<legend>Укорачиватель</legend>
<label for="name">Ссылка:</label><input id="name" name="site" value="" type="url" required>
<br>
<br>
<label for="email">Кол-во символов:</label><input id="email" name="name" value="3" type="number" min="1" max="5" required>
<br>
<br>
<input value="Укоротить!" type="submit">
</form>
<br>
<br>
<br>
<div id="results"></div>
</center>
</body>
</html>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question