how to read the contents of a php file using javascript - javascript

So let's say I have a form tag like so. And I want to print the contents of the file to screen using JavaScript. How do I go about doing that?
<form action="/missions/basic/3/index.php" method="post">
<input type="hidden" name="file" value="file.php" />
<input type="password" name="password" />
<br />
<br />
<input type="submit" value="submit" />
</form>
What I want is to use the alert() function to show the contents of file.php, even though the contents are hidden.

What I want is to use the alert() function to show the contents of file.php, even though the contents are hidden.
You just can't. Don't waste your time :)
If your web server is configured correctly it will never output PHP code even if you could request that from JavaScript.
You can grab and show the output of a PHP file on the client but not its source code like that, unless there is a mess up or it is by design.

PHP is server side programming. When you run, it'll return the output of server coding not the server code

Related

Form TextArea Losing Linebreaks

I am trying to repair an existing web form that submits a text area's contents to an external site's shopping cart service. The textarea is named "adtext" and upon submission it runs a few different scripts to calculate pricing, etc. It ultimately re-writes the ad content into a value named op31 (which is recognized by the shopping cart). The cart system recently got updated and it broke our script to convert line breaks in this text area into something that would be retained in that other site. I've tried looking at other sites, but it's over my head. I'm not particularly good at this stuff. I'm sure this isn't, and likely wasn't the best way to do it. I've seen CSS suggestions but don't understand it enough to actually implement them.
I've stripped out as much code as I comfortably could to clean it up, but still retain the issue. I'm wondering if someone could assist me with updating this function into something that would convert the "adtext" textarea's line breaks into something usable when written to "op31".
<script language="JavaScript" type="text/JavaScript">
function ConvertCarriageReturns(textarea, strReplace){
document.form.op31.value = escape(textarea.value)
for(i=0;i<document.form.op31.value.length;i++){
if(document.form.op31.value.indexOf("%0D%0A") > -1 ){
document.form.op31.value = document.form.op31.value.replace("%0D%0A",strReplace)
}
}
document.form.op31.value = unescape(document.form.op31.value)}
</script>
<form
action="https://(cart's url)/addtocart.aspx"
method="post"
name="form">
<textarea name="adtext" rows="12"></textarea>
<input alt="Add To Cart" name="add"
onclick="ConvertCarriageReturns(this.form.adtext,'<br>');
return checkwords(this)" src="https://....Add-To-Cart.gif"
type="image" />
<input name="item" type="hidden" value="(misc cart parameters" />
<input name="op31" readonly="readonly" type="hidden" />
</form>
You can use this native PHP function called nl2br
Like this:
$text = nl2br(this.form.adtext);

How with javascript change html code

I need the following change using javascript:
from:
<input id="innn" value="" />
to:
<input id="innn" value="SOME_VALUE" />
I tried this:
document.getElementById("innn").setAttribute("value", "189");
and this:
document.getElementById('innn').value = '152';
it worked but it changed only visual on page not the html code and i need to change the code as shown below:
<input id="innn" value="" /> --> <input id="innn" value="125" /> --><input id="innn" value="158" />
Please help or I must use php like : <input id="innn" value="<? php ... ?>" /> ????
HTML is what is rendered in order for your browser to know what to show on your page. Once sent by the server it will not change (at least not with basic JavaScript). There are more advanced ways of doing things, but they are not simple. When you call a JavaScript function to change something, it changes it in the DOM, so you will see the change on your screen, but when you click view-source your browser is fetching the original page again from the server to show you. In some browsers they have something called Inspect mode which allows you to see what the HTML looks like right now for your page. In that case it will show the updated code. If you want that when someone clicks view-source and sees SOME_VALUE in the value for id="innn", then you would need to use PHP.
document.getElementById("innn").value = somevalue

How to change iFrame results to JavaScript alert box?

I have access to a url runs a script to clear a users state.
Id like to produce a script that will run on a webpage, to do this.
The following script works but in Firefox its annoying for people to have to disable mixed content each time they come to the page:
<SCRIPT LANGUAGE="JavaScript">
function go(loc,loc2){
document.getElementById('userstate').src = loc +
document.getElementById('username').value + loc2;
}
</script>
<form onSubmit="go('http://mysiteurlomitted.com/userstate/?userId=','&app=cc'); return false;"/>
Username: <input type="text" id="username">
<input type="submit" value="Clear User State">
</form>
<iframe id="userstate" src="about:blank" width="470" height="30" frameborder="1" scrolling="no"></iframe>
The resulting URL produces a simple text string, and has no HTML on the results page, so I feel it should be pretty easy to read this URL as a file, and load the results into an alert box. This would avoid the iframe method, and get out of the mixed content situation. It would run without anyone needing to change anything. But I cannot figure out how to get this to work. I feel like FileReader() should be a good way to do it, but the URL has parameters... so the reader doesn't know what file type it is. Its just failing. There has to be an easier way to do this.
You shouldn't need any javascript for this kind of thing. Just add a name attribute to the username input, and that value will get passed along in the query string as "&username=Name". so:
<form action="/userstate?app=cc">
Username: <input type="text" id="username" name="username" />
<input type="submit" value="Clear User State" />
</form>
That would submit the form to "http://samedomain.wut/userstate?app=cc&username=", which would be available on the server side in the usual post data source.
There are other, better ways of doing this, but I don't know your setup. You should look into server-side management of cookies/session; the user shouldn't have to input anything except to log in initially.
Edit:
If you want to use this as an administration tool, you can do two things: use ajax (XMLHttpRequest or $.ajax from Jquery), or make your endpoint redirect back to (or serve) the form. You could have the form submit to its own url and in your server side scripting process the data, then output the html for the form again. Not a great pattern for actual applications, but it should work in this situation.

Javascript Form Input Retrieval

Why is it that in a form that contains a Text Box and a Submit Button, I can Alert what has been typed in the text box by the user, but can't print it on the page? What am I doing wrong?
Here's the code
<form name="Serb" action="" method="get">
<input name="Name" type="text" size="15" maxlength="20" />
<input name="Join" type="submit" value="Join" onClick="serb(this.form)" />
<script type="text/javascript">
function serb(form){
var x = document.Serb.Name.value;
alert(x); \\this alerts
document.write(x); \\this should print on page
}
</script>
For some reason, the alert works fine and displays exactly what the user typed in the username box after pressing 'Join'. However, it won't print the information on the page. Why is that?
It does work. The value in the textbox is printed on the page.
BUT:
\\ do not mean anything in Javascript. Comments begin with //. This is most likely the reason why you are not seeing the value being written
document.write replaces whatever is in the HTML page with its argument. (If it is called after the document is loaded). So unless you are trying to learn Javascript this is not a very good idea.
Actually it is not a very good idea to use it even when learning Javascript, unless you are trying to learn how document.write works.
There are flexible (and better) ways to manipulate the content of a page, starting from the humble getElementById to complex DOM manipulation
It is not a good idea to use document.write() after the page has been loaded/parsed. At that point, it will overwrite the page HTML with new content. document.write() is generally used while the page is being loaded to insert content at a particular point into the page as it's being loaded.
If you want to put the value into some item on the page, then you need to use appropriate DOM methods for that, putting the value into an input field, setting the innerHTML on a div, etc...
You can read about document.write here: https://developer.mozilla.org/en/document.write.
Here's an example of fetching the value from the field and putting it in another object on the page without using document.write(): http://jsfiddle.net/jfriend00/dU8Sr/.
HTML:
<form name="Serb" action="" method="get">
<input name="Name" type="text" size="15" maxlength="20" />
<input name="Join" type="button" value="Join" onClick="serb(this.form)" />
</form>
<br>
<br>Output: <span id="output"></span>
Javascript:
function serb(form) {
var x = document.Serb.Name.value;
document.getElementById("output").innerHTML = x;
}

What is Best way to obtain filename for HTML FILE Uploading on forms?

I need to obtain the String for the file that is being uploaded from forms to store in the database.
I am using the usual form input file element
input type="file" name="some_name"
I found a couple JS scripts that allow me to do useless things like display the string in a dialog box, etc.
I need this as an element on the request object or as a hidden field on my page when the form is posted.
You should be able to do something like this:
<form method="POST">
<input type="file" name="some_name"
onchange="document.getElementById('hidden_file').value = this.value;" />
<input type="hidden" id="hidden_file" value="" />
<input type="submit" />
</form>
I believe that will work in all browsers if you simply want to store the filename, and not the full path.
You won't get a very useful value. Some browsers will only give you the final name part of the file path, while IE will give you a path with a bogus directory name.
I think that the "safe" fragment of the file name should already be passed in to you as part of the part header in the multipart post body.

Categories