I'm new in Ajax..
It's for a URL Shorten.
Upon form submit, I need to do that stuff, to short' the URL.. And I do that on a separate page, using php POST.
When that script is done and the shortened URL is generated, I need to present that to the user. (We have ti as a php variable, let's say $shorturl).
I know that it is possible to submit forms without page reloading using Ajax, but what I can't figure out is how to get a php variable from the page that processed the form, and present it to the user.
All this without refreshing the page, of course.
Anybody can tell me how to do this?
Your problem is the <Form> has the action and method property defined, and you have a submit button. No matter what if you click the submit button the form will do to that page you defined in the "action" property.
If you want what was typed into the text box. You can just get the .value property in Javascript through your updated code below... -
<html><head>
-
<script type="text/javascript">
-
function clicked() {
-
alert(document.form1.shorturl.value);
-
}
-
</script>
-
-
</head>
-
<body>
-
-
<form name="form1">
-
<input type="text" size="12" name="shorturl">
-
<button onClick="clicked();">Submit</button>
-
</form>
-
-
<div id="htmldiv"></div>
-
-
</body>
-
</html>
11 2935
I have looked to the tutorial..
As far as I understand, it is possible for the server to send the '$shorturl' but I don't know how the code for that works..
jQuery. Get it. - <script>
-
$("#htmldiv").load("your-script.php");
-
</script>
-
-
<div id="htmldiv"></div>
if I remember correctly... the .load function will call the script, and place the returned output into the container #htmldiv.
Now jQuery can push content into a HTML tag via a class property, or id property, or, well, alot more methods.
jQuery is an JavaScript coder's best friend.
thank you for your help but I it's not working...
See my code:
Here's page.php: - <html><head>
-
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
-
-
<script type="text/javascript">
-
$("#htmldiv").load("process.php");
-
</script>
-
-
</head>
-
<body>
-
-
<form action="process.php" method="POST">
-
<input type="text" size="12" name="shorturl">
-
<input type="submit" value="Submit">
-
</form>
-
-
<div id="htmldiv"></div>
-
-
</body>
-
</html>
And here's process.php: - <?php
-
-
$shorturl = $_POST["shorturl"];
-
-
?>
I made it simpler, let's say I want to display $shorturl on page.php (without page reloading).
Can you help me?
I guess you simply need to add
echo $shorturl;
at the end of your code in process.php.
it's not working...
the form just submits normally and goes to process.php.
What's wrong?
Your problem is the <Form> has the action and method property defined, and you have a submit button. No matter what if you click the submit button the form will do to that page you defined in the "action" property.
If you want what was typed into the text box. You can just get the .value property in Javascript through your updated code below... -
<html><head>
-
<script type="text/javascript">
-
function clicked() {
-
alert(document.form1.shorturl.value);
-
}
-
</script>
-
-
</head>
-
<body>
-
-
<form name="form1">
-
<input type="text" size="12" name="shorturl">
-
<button onClick="clicked();">Submit</button>
-
</form>
-
-
<div id="htmldiv"></div>
-
-
</body>
-
</html>
Thanks ! That helped a lot!
- <html><head>
-
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
-
<script type="text/javascript">
-
function clicked() {
-
$("#htmldiv").load("page.php");
-
}
-
</script>
-
-
</head>
-
<body>
-
<button onClick="clicked();">Submit</button>
-
-
<div id="htmldiv"></div>
-
-
</body>
-
</html>
@Samishii23
Oh, thanks
But one question: What is the Javascript code for making the -content of a text input- a php variable?
To make a form element into a PHP variable, thats what a typical form would be: - <form method="post">
-
<input type="text" name="Text1">
-
</form>
-
<form method="get">
-
<input type="text" name="Text2">
-
</form>
-
<?php
-
echo $_POST['Text1'];
-
echo $_GET['Text2'];
-
?>
It should be noted, that the GET is what is in the URL bar in the browser. So for example. The above $_GET variable could be set by loading this URL http://localhost/page.php?Text1=data.
GET is not secure, and should be used like, more then one page per file. http://localhost/page.php?page=home or http://localhost/page.php?page=login
Using JavaScript to set a PHP variable unless you want to do it via Ajax, is kinda pointless, unless you want to set a form behind the scenes which I wouldn't do personally. If you want to pass variables through JavaScript the best way is Ajax, and is what I showed you in post #10. Loading a php script with GET variables set like mentioned above.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Vibhu |
last post by:
If a website is developed using ASPX, the page is submitted using the
Javascript function
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
I am building a small application that...
|
by: jayamca |
last post by:
I have a form which is submitted via ajax. This form contains simple input elements and a field which needs to be a file upload. I came to know that we can do this by using hidden IFrame technique....
|
by: lucyh3h |
last post by:
Hi,
I am trying to use XMLHttpRequest to do server side validation.
I have several fields on a form and a submit button. The submit button
has an event assocated with it when clicked. The...
|
by: jad2006 |
last post by:
Hi
i have a requirement to pass the parameters say account No to a javaScript when the user clicks a link , The form action will also be set in JavaScript, and form is submitted in...
|
by: dmorand |
last post by:
I've looked far and wide around the internet trying to find a decent tutorial on how to start using ajax to control my form posting.
I'd like to have it so my users don't have to go to a new...
|
by: mrbadboy |
last post by:
Hi,
I need to submit multiple forms with using single button. I've mentioned my code below.
<?
if(isset($_POST)){
print_r($_POST);
}
?>
<script type="text/javascript">
|
by: rizwan6feb |
last post by:
Hi, everyone! I am learning AJAX these days. I want to validate a form with Ajax in such a way that every field is validated on the "onBlur" event. There are fields like Username, First Name, Last...
|
by: vetrib2w |
last post by:
Hi ,
In short:
How to change the form action attribute value using javascript and that need to supported in IE also ?
BRIEF:
I am using ajax function to submit the form.
I am also...
|
by: ghjk |
last post by:
I' developing web site with php and postgres. I want to validate php forms using ajax. I'm new to ajax and tried to several examples. But still I couldn't get it. Could someone please tell me a...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |