Mistake in javascript conditional if statement - javascript

I get an insure after uploading my PHP project into an internet host(000webhost.com). It works fine at my localhost but doesn't at internet host. Here is my code:
//view.php (use a hidden iframe to received data after submitting)
<form action="model.php" method="POST" target="my_iframe" id="my_form" style="display: none;">
<input type="hidden" name="user" id="user" value=""/>
<input type="hidden" name="user_lastname" id="user_lastname" value=""/>
<input type="hidden" name="user_firstname" id="user_lastname" value=""/>
</form>
<iframe name="my_iframe" id="my_iframe" style="display: none;"></iframe>
<div id="sent">Sent</div>
<srcipt type="text/javascript">
jQuery('#sent').unbind('click').click(funtion(){
jQuery('#user').val("an user name");
jQuery('#user_lastname').val("a last name");
jQuery('#user_firstname').val("a first name");
jQuery('#my_form').submit();
jQuery('#my_iframe').unbind('load').load(function(){
if(jQuery(this).contents().text()!='success')
alert('Update failed');
else
alert('Update successful');
});
});
</script>
//model.php
if(UpdateUser($_REQUEST['user'],$_REQUEST['user_firstname']),$_REQUEST['user_lastname'])==true)
echo 'success';
else
echo 'Fail in updating';
At my localhost, i get an message "Update Successful" but at my internet host, i get an message "Update failed". Thank for advances. Sorry because of my bad English.
---EDIT----
I change style="display:block" of iframe to show its content. I see that iframe's content is "success" but i still get message "Update failed".it means:

I corrected the misspellings (there was another: function spelled "funtion" in the first line of the script).
The jQuery script is working so the problem must be with the PHP page. Maybe 000webhost puts some output before your PHP page's output or maybe that update is failing. Check the output of the PHP page.

Related

Submit form using anchor tag

I'm currently working on my webpage where I'm using an anchor tag to process the request. Here is the portion of my code:
I AGREE
It works okay. However, I added an input field and I have to include that in my process on "?redirect=Pro". My input field value is not being processed because the form is not being submitted/processed. I already tried the:
<form id="discount">
<input type="hidden" id="coupon_applied">
</form>
<a id = "agree" onclick="document.getElementById('discount').submit()" href="?redirect=Pro" target="_blank">I AGREE</a>
But I still can't get it to work.
I can't remove the ?redirect=Pro because that's where all my process is happening.
Any advice on how I i will modify my code to accommodate this would be much appreciated.
EDIT: This is a preview of my pricing.php file for more info:
<?php
session_start();
$link=$_GET['redirect'];
$discount_amount = $_POST['coupon_applied'];
if ($link == 'Pro')
{
echo "<script type='text/javascript'>alert('$discount_amount')</script>";
}
?>
<form id="discount" action="pricing.php" method="post">
<input type="hidden" id="coupon_applied">
</form>
<a onclick="document.getElementById('discount').submit()" href="?redirect=Pro" target="_blank">I AGREE</a>
With my current code, echo is returning blank/null.
You should use a method and action in your form like: <form action="/action_page.php" method="post">

php alert not defined or fails to display

I have made a simple form and php script which should read a variable from the html form text box and when the user clicks submit a simple message will be displayed saying 'You are searching for songs by artist_name' but when i try i get either a blank page or a message saying undefined. however if i just echo the php variable it displays the value correctly.
i have tried to just use
alert($artist_name) and alert('$artist_name')
But i get Uncaught ReferenceError: $artist_name is not defined. or the alert displays '$artist_name' instead of the value ?
However something like
<?php echo $_GET["artist"]; ?>
successfully get the text ???
and this works ok too.
elseif ($artist_name =="foo") { ?>
<script language="javascript" type="text/javascript">
alert("you enetered foo");
window.location = 'index.html';
</script>
<?php
}
so it is getting the variable ok but i just cant seem to include it in my alert message which is the entire aim of this code, Please can someone show me what i am doing wrong. Many thanks.
my code below:
the html
<form id="form1" action="searchresults.php" method="GET">
<div id="artform">
<fieldset>
<legend> Search for music </legend>
<p> <label> Artist (*): </label> <input type="text" name="artist" id="artist" placeholder="enter an artists" class="add1"></p>
</fieldset>
<input type="submit" class="button1">
<input type="reset" class="button2">
</div>
</form>
the php
<?php
$artist_name = $_GET['artist'];
if (empty($artist_name)) { ?>
<script language="javascript" type="text/javascript">
alert('Field blank !, please try again.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('You are searching for songs by' $artist_name); // the issue is here
window.location = 'index.html';
</script>
<?php
}
?>
You forgot to enclose $artist_name with PHP start and end tag.
alert('You are searching for songs by' $artist_name); // the issue is here
Should've been:
alert('You are searching for songs by <?php echo $artist_name; ?>'); // the issue is here
thank you i got it working, Kek i tried your method but i got an error uncaught SyntaxError: missing ) after argument list, But 125248 comment worked perfectly, i had to include the tags for it to work. Thank you for your time.

PHP/JavaScript form validation, invalid email check

So i'm working on form validation for a website.
What i have accomplished so far is when the form is submitted, validate.php checks if the email is in the correct format.
If it is, it proceeds in calling the sendMail() function included in the validate.php which proceeds in sending the email and then redirects the user back to the home page after 5 seconds. (This part works <-)
If it isn't, that's where i'm stuck.
What i'm trying to accomplish at this stage, is somehow get PHP to send a script tag, <script> emailError(); </script>, to the current page (contact.html) and execute the script which simply appends a paragraph saying that the email is incorrect. What i am getting, however is the validate.php has its own error in the else statement simply saying Error in a blank page (which is what i don't want, i put it there to see if the validation was working correctly).
I tried to add this (the script mentioned above) in the else statement in validate.php:
validate.php
<?php
include "mail.php";
$email = $_POST['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
sendMail();
}else {
echo("<script> emailError(); </script>");
}
?>
So what i was hoping it would do, is echo it into the page executing the JavaScript function but it didn't work and i think i know why.
PHP, to my knowledge, executes on request of user interaction (press button, click something, etc...) or upon page loading (Correct me if i'm wrong, still learning).
To make sure that the echo did not work, i inspected the page in real-time while using the form and didn't see any sign of the script tag being inserted into the HTML.
At this point, i have tried many alternative solutions, i don't even know where to begin. I could really use some help and any tips for improving my form.
This is the contact page that i'm working on. Feel free to try it out and see the results for yourself but Please don't spam!
Click Here
emailError.js
This is what i wanted to echo into the page with PHP.
function emailError(){
var targetElement = document.getElementById("emailform");
var errorElement = document.createElement("P");
var errorMessage = document.createTextNode("Invalid Email");
errorElement.appendChild(errorMessage);
errorElement.setAttribute("id","error");
document.body.appendChild(errorElement);
}
contact.html
This is just a segment of contact.html showing my form.
<form action="validate.php" method="post" id="emailform">
<required>* - Required</required><br>
<name>Name:* <input type="text" name="name" id="name" required /></name><br>
<email>Email:* <input type="text" name="email" id="email" required /></email> <br>
<message>Message:*<br><textarea rows="5" name="message" id="message" maxlength="1000" required></textarea></message><br>
<submit><input type="submit" value="Send" name="send" id="send" /></submit>
</form>
I'm not asking for something to copy n' paste, simply something to push me in the right direction.
Well, first of all, You are submitting your form to validate.php, and then never redirect back to contact.html, that's why you dont see the script tag appended. And from whay i see, that string 'Error' does not appear to be in your code, so I'm guessing that you did not paste the entire code. My suggestion, is to redirect to the contact form back again if the condition fails, and change contact.html to contact.php in order to be able to do a validation. Here is some rough code:
<?php
include "mail.php";
$email = $_POST['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
sendMail();
}else {
header('Location: contact.php?error=1');
}
?>
and then, in your contact.php file:
<form action="validate.php" method="post" id="emailform">
<required>* - Required</required><br>
<name>Name:* <input type="text" name="name" id="name" required /></name><br>
<email>Email:* <input type="text" name="email" id="email" required /></email> <br>
<message>Message:*<br><textarea rows="5" name="message" id="message" maxlength="1000" required></textarea></message><br>
<submit><input type="submit" value="Send" name="send" id="send" /></submit>
</form>
if ($_GET['error']) {
echo "<script> emailError(); </script>";
}
This is just a rough example, it can be more robust, with this solution, if somebody enters your contact form with the query param "error" he will get the javascript executed. But as I said, this is a concept and a rough solution to explain how you could solve your problem.
You can't execute java function like you are trying to.
If you use ajax, than in the success response function, set the respone to a div.
For example, if you have a success function that gets the data in an argument called data, than set an empty div with an ID like "emailResponse" in your page, and use this in the function:
var responseDiv = document.getElementById("emailResponse");
responseDiv.innerHtml = data;
Add this to your html:
<form action="validate.php" method="post" id="emailform">
<required>* - Required</required><br>
<name>Name:* <input type="text" name="name" id="name" required /></name><br>
<email>Email:* <input type="text" name="email" id="email" required /></email> <br>
<message>Message:*<br><textarea rows="5" name="message" id="message" maxlength="1000" required></textarea></message><br>
<submit><input type="submit" value="Send" name="send" id="send" /></submit>
<div id="emailResponse"></div>
</form>
And in your PHP, change to this:
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
sendMail();
echo "Your message was sent!";
}else {
echo "You email was invalid!";
}
UPDATE:
If you are not using ajax - you need to redirect to the contact page with some parameter in GET, or set some SESSION variable.

Javascript redirect based on form input

I need to redirect one page to another page using the form value.
I have this code, which i think is fine for first page and what should i put in the other page where i want to show the data ??
<meta http-equiv='refresh' content='0;url=http://site.com/page.php'>
<form action="http://site.com/page.php" method="post" name="myform">
<input type="hidden" name="url" value="<?php echo $url; ?>">
<script language="JavaScript">document.myform.submit();</script>
</form>
Regards
You can't mix a meta-refresh redirect with a form submission per se.
Also, meta-refreshes are terrible anyway. Since you are already in control of the receiving page, and it's using PHP, use that to accomplish the redirect. Try this:
<form action="http://site.com/page.php" method="post" name="myform">
<input type="submit" value="Go!" />
</form>
Then, in page.php:
<?php
// Act on the input, store it in the database or whatever. Then do the redirect using an HTTP 302.
header('Location: http://example.com');
?>
If you need the form to pass the destination along to page.php, you'll want to sanitize it to prevent a LOT of security problems. Here's a rough outline.
<form action="http://site.com/page.php" method="post" name="myform">
<input type="hidden" name="destination" value="http://example.com" />
<input type="submit" value="Go!" />
</form>
Then, in page.php (copied re-encoding from answer https://stackoverflow.com/a/5085981/198299):
<?php
$destination = $_POST['destination'];
$url_parsed = parse_url($destination);
$qry_parsed = array();
parse_str($url_parsed['query'], $qry_parsed);
// Check that $destination isn't completely open - read https://www.owasp.org/index.php/Open_redirect
$query = parse_url($destination);
$destination = "{$url_parsed['scheme']}{$url_parsed['host']}{$url_parsed['path']}?" . http_build_query($query);
header('Location: ' . $destination);
?>
I haven't double-checked that code (just wrote it here in the browser), but it should suffice as a rough sketch.
in site.com/page.php
<script>window.location.href = 'newPage.php';</script>
You will have to write this outside the php tags though.
To redirect a page in PHP, use:
<?php
header('Location: url/file.php');
?>
To refresh to a different page in HTML, use:
<meta http-equiv='refresh' content='0;url=http://url/file.php'>
In the content attribute, 0 is the amount of seconds to wait.
To refresh to a different page in JavaScript, use:
window.location.href = 'url/file.php';
When none of these work, follow an anchor link, using HTML:
Click here to go now!
To answer your question, it can be done several ways:
1) Very bad, requires two files, super redundant
HTML file:
<form action="http://site.com/page.php" method="post" name="myform">
<input type="hidden" name="url" value="<?php=$url?>">
</form>
<script type="text/javascript">
// Submit the form
document.forms['myform'].submit();
</script>
Page.php:
<?php
// Catch url's value, and send a header to redirect
header('Location: '.$_POST['url']);
?>
2) Slightly better, still not recommended
<form action="http://site.com/page.php" method="post" name="myform">
<input type="hidden" name="url" value="<?php=$url?>">
</form>
<script type="text/javascript">
// Set form's action to that of the input's value
document.forms['myform'].action = document.forms['myform'].elements['url'].value;
// Submit the form
document.forms['myform'].submit();
</script>
3) Still very redundant, but we're getting better
<form action="http://site.com/page.php" method="post" name="myform">
<input type="hidden" name="url" value="<?php=$url?>">
</form>
<script type="text/javascript">
// Simply refresh the page to that of input's value using JS
window.location.href = document.forms['myform'].elements['url'].value;
</script>
4) Much better, save yourself a lot of trouble and just use JS in the first place
<?php
// Start with a PHP refresh
$url = 'url/file.php'; // Variable for our URL
header('Location: '.$url); // Must be done before ANY echo or content output
?>
<!-- fallback to JS refresh -->
<script type="text/javascript">
// Directly tell JS what url to refresh to, instead of going through the trouble to get it from an input
window.location.href = "<?php=$url?>";
</script>
<!-- meta refresh fallback, incase of no JS -->
<meta http-equiv="refresh" content="0;url=<?php=$url?>">
<!-- fallback if both fail (very rare), just have the user click an anchor link -->
<div>You will be redirected in a moment, or you may redirect right away.</div>
Save that with a .php extension, and you should be good to go.

Javascript form not posting to IE 9

I've got a very simple Javascript form that posts to a PHP page. This page is launching from a CRM system and the page itself can only be an HTML page, so I can't use PHP for the form. The form posts the user id (which is generated by the CRM system) over to a PHP page and then does a load of stuff based on the UserID.
The problem, however, is that some users now have IE9 and it doesn't seem to work with that! IE 8 is absolutely fine, but IE9 just doesn't seem to post the userid.
The form within the CRM system is as follows:
<form action="http://intranet-srv02/reports/contact.php" method="post" onsubmit="target_popup(this)">
<input name="userid" type="hidden" value="[userid]" />
<input type="submit" value="Reports" />
</form>
<script language="JavaScript1.2">
function target_popup(form) {
window.open('', 'formpopup', 'width=1100,height=750,resizeable,scrollbars');
form.target = 'formpopup';
}
</script>
And when, on the contact.php page put
<?php
$userid = $_POST['userid'];
echo $userid;
?>
Nothing echos on IE9 (but on IE8 and others it does)
Any help much appreciated!
EDIT: I've updated the deprecated language attribute, but still having the same issue. The form now reads:
<form action="http://intranet-srv02/reports/contact.php" method="post" onsubmit="target_popup(this)">
<input name="userid" type="hidden" value="[userid]" />
<input type="submit" value="Reports" />
</form>
<script type="text/javascript">
function target_popup(form) {
window.open('', 'formpopup', 'width=1100,height=750,resizeable,scrollbars');
form.target = 'formpopup';
}
</script>
Any other ideas?!
So it turns out that there is a (currently unknown exactly what) problem with IE9 and user profiles.
Possibly the problem is when users were on XP and then they started working on a Windows 7 box. Ultimately we haven't completely narrowed it down yet, but re-creating the user's profile seems to fix the issue!
The joys of Windows...

Categories

Resources